@Override protected boolean isRepresentable(MethodDescription instrumentedMethod) { return instrumentedMethod.isMethod(); } },
@Override protected boolean isSort(MethodDescription target) { return target.isMethod(); } },
/** * {@inheritDoc} */ public boolean isMethod() { return methodDescription.isMethod(); }
/** * <p> * Replaces any interaction with a matched byte code element by an invocation of the specified method. If a method * is replacing a field access, it is treated as if it was replacing an invocation of the field's getter or setter respectively. * </p> * <p> * A replacement can only be applied if the method is compatible to the original byte code element, i.e. consumes compatible * arguments and returns a compatible value. If the method is not {@code static}, it is treated as if {@code this} was an implicit * first argument. * </p> * <p> * <b>Important</b>: It is not allowed to specify a constructor or the static type initializer as a replacement. * </p> * * @param methodDescription The method to invoke instead of interacting with any of the matched byte code elements. * @return A member substitution that replaces any matched byte code element with an invocation of the specified method. */ public MemberSubstitution replaceWith(MethodDescription methodDescription) { if (!methodDescription.isMethod()) { throw new IllegalArgumentException("Cannot use " + methodDescription + " as a replacement"); } return replaceWith(new Substitution.ForMethodInvocation.OfGivenMethod(methodDescription)); }
/** * Returns an auxiliary type for loading the supplied method description as a constant. * * @param methodDescription The method description to represent as a constant. * @return An appropriate auxiliary type. */ public static AuxiliaryType of(MethodDescription methodDescription) { if (methodDescription.isConstructor()) { return methodDescription.isPublic() ? FOR_PUBLIC_CONSTRUCTOR : FOR_DECLARED_CONSTRUCTOR; } else if (methodDescription.isMethod()) { return methodDescription.isPublic() ? FOR_PUBLIC_METHOD : FOR_DECLARED_METHOD; } else { throw new IllegalStateException("Cannot load constant for type initializer: " + methodDescription); } }
/** * {@inheritDoc} */ protected StackManipulation resolve(FieldDescription.InDefinedShape target, FieldDescription fieldDescription, TypeDescription instrumentedType, MethodDescription instrumentedMethod) { if (fieldDescription.isFinal() && instrumentedMethod.isMethod()) { throw new IllegalArgumentException("Cannot set final field " + fieldDescription + " from " + instrumentedMethod); } return new StackManipulation.Compound( FieldAccess.forField(target).read(), assigner.assign(TypeDescription.ForLoadedType.of(value.getClass()).asGenericType(), fieldDescription.getType(), typing) ); }
if (!instrumentedMethod.isMethod()) { throw new IllegalArgumentException(instrumentedMethod + " does not describe a field getter or setter"); ); } else if (instrumentedMethod.getReturnType().represents(void.class) && instrumentedMethod.getParameters().size() == 1) { if (fieldDescription.isFinal() && instrumentedMethod.isMethod()) { throw new IllegalStateException("Cannot set final field " + fieldDescription + " from " + instrumentedMethod);
if (!target.getType().asErasure().isAssignableFrom(Method.class)) { throw new IllegalStateException("Cannot assign Method type to " + target); } else if (source.isMethod()) { TypeDescription typeDescription = annotation.getValue(TARGET_TYPE).resolve(TypeDescription.class); Implementation.SpecialMethodInvocation specialMethodInvocation = (typeDescription.represents(void.class)
return new MethodDelegationBinder.ParameterBinding.Anonymous(ClassConstant.of(implementationTarget.getOriginType().asErasure())); } else if (parameterType.represents(Method.class)) { return source.isMethod() ? new MethodDelegationBinder.ParameterBinding.Anonymous(methodConstant(annotation.loadSilent(), source.asDefined())) : MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
/** * {@inheritDoc} */ public MethodDelegationBinder.ParameterBinding<?> bind(final AnnotationDescription.Loadable<SuperMethod> annotation, MethodDescription source, ParameterDescription target, Implementation.Target implementationTarget, Assigner assigner, Assigner.Typing typing) { if (!target.getType().asErasure().isAssignableFrom(Method.class)) { throw new IllegalStateException("Cannot assign Method type to " + target); } else if (source.isMethod()) { Implementation.SpecialMethodInvocation specialMethodInvocation = annotation.loadSilent().fallbackToDefault() ? implementationTarget.invokeDominant(source.asSignatureToken()) : implementationTarget.invokeSuper(source.asSignatureToken()); if (specialMethodInvocation.isValid()) { return new MethodDelegationBinder.ParameterBinding.Anonymous(new DelegationMethod(specialMethodInvocation, annotation.loadSilent().cached(), annotation.loadSilent().privileged())); } else if (annotation.loadSilent().nullIfImpossible()) { return new MethodDelegationBinder.ParameterBinding.Anonymous(NullConstant.INSTANCE); } else { return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE; } } else if (annotation.loadSilent().nullIfImpossible()) { return new MethodDelegationBinder.ParameterBinding.Anonymous(NullConstant.INSTANCE); } else { return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE; } }