/** * <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)); }
/** * <p> * Replaces any interaction with a matched byte code element by an interaction with the specified field. If a field * is replacing a method or constructor invocation, it is treated as if it was a field getter or setter respectively. * </p> * <p> * A replacement can only be applied if the field is compatible to the original byte code element, i.e. consumes an * instance of the declaring type if it is not {@code static} as an argument and consumes or produces an instance of * the field's type. * </p> * * @param fieldDescription The field to access instead of interacting with any of the matched byte code elements. * @return A member substitution that replaces any matched byte code element with an access of the specified field. */ public MemberSubstitution replaceWith(FieldDescription fieldDescription) { return replaceWith(new Substitution.ForFieldAccess.OfGivenField(fieldDescription)); }
/** * <p> * Replaces any interaction with a matched byte code element by an interaction with the specified field. If a field * is replacing a method or constructor invocation, it is treated as if it was a field getter or setter respectively. * </p> * <p> * A replacement can only be applied if the field is compatible to the original byte code element, i.e. consumes an * instance of the declaring type if it is not {@code static} as an argument and consumes or produces an instance of * the field's type. * </p> * * @param field The field to access instead of interacting with any of the matched byte code elements. * @return A member substitution that replaces any matched byte code element with an access of the specified field. */ public MemberSubstitution replaceWith(Field field) { return replaceWith(new FieldDescription.ForLoadedField(field)); }
/** * <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> * * @param method 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(Method method) { return replaceWith(new MethodDescription.ForLoadedMethod(method)); }
/** * Replaces any interaction with a matched byte code element with a non-static method access on the first * parameter of the matched element. When matching a non-static field access or method invocation, the * substituted method is located on the same receiver type as the original access. For static access, the * first argument is used as a receiver. * * @param matcher A matcher for locating a method on the original interaction's receiver type. * @param methodGraphCompiler The method graph compiler to use for locating a method. * @return A member substitution that replaces any matched byte code element with an access of the matched method. */ public MemberSubstitution replaceWithMethod(ElementMatcher<? super MethodDescription> matcher, MethodGraph.Compiler methodGraphCompiler) { return replaceWith(new Substitution.ForMethodInvocation.OfMatchedMethod(matcher, methodGraphCompiler)); }
/** * Subs any interaction with a matched byte code element. Any value read from the element will be replaced with the stubbed * value's default, i.e. {@code null} for reference types and the specific {@code 0} value for primitive types. Any written * value will simply be discarded. * * @return A member substitution that stubs any interaction with a matched byte code element. */ public MemberSubstitution stub() { return replaceWith(Substitution.Stubbing.INSTANCE); }
/** * Replaces any interaction with a matched byte code element with an invocation of the instrumented * method. This can cause an infinite recursive call if the arguments to the method are not altered. * * @return A member substitution that replaces any matched byte code element with an invocation of the * instrumented method. */ public MemberSubstitution replaceWithInstrumentedMethod() { return replaceWith(Substitution.ForMethodInvocation.OfInstrumentedMethod.INSTANCE); }
/** * Replaces any interaction with a matched byte code element with a non-static field access on the first * parameter of the matched element. When matching a non-static field access or method invocation, the * substituted field is located on the same receiver type as the original access. For static access, the * first argument is used as a receiver. * * @param matcher A matcher for locating a field on the original interaction's receiver type. * @return A member substitution that replaces any matched byte code element with an access of the matched field. */ public MemberSubstitution replaceWithField(ElementMatcher<? super FieldDescription> matcher) { return replaceWith(new Substitution.ForFieldAccess.OfMatchedField(matcher)); }