/** * Applies an assigner to the method delegation that is used for assigning method return and parameter types. * * @param assigner The assigner to apply. * @return A method delegation implementation that makes use of the given designer. */ public Implementation.Composable withAssigner(Assigner assigner) { return new MethodDelegation(implementationDelegate, parameterBinders, ambiguityResolver, terminationHandler, bindingResolver, assigner); }
/** * {@inheritDoc} */ public Implementation andThen(Implementation implementation) { return new Compound(new MethodDelegation(implementationDelegate, parameterBinders, ambiguityResolver, MethodDelegationBinder.TerminationHandler.Default.DROPPING, bindingResolver, assigner), implementation); }
/** * {@inheritDoc} */ public Composable andThen(Composable implementation) { return new Compound.Composable(new MethodDelegation(implementationDelegate, parameterBinders, ambiguityResolver, MethodDelegationBinder.TerminationHandler.Default.DROPPING, bindingResolver, assigner), implementation); }
/** * Delegates any intercepted method to invoke a method on an instance that is returned by a parameterless method of the * given name. To be considered a valid delegation target, a method must be visible and accessible to the instrumented type. * This is the case if the method's declaring type is either public or in the same package as the instrumented type and if * the method is either public or non-private and in the same package as the instrumented type. Private methods can only * be used as a delegation target if the delegation is targeting the instrumented type. * * @param name The name of the method that returns the delegation target. * @param methodGraphCompiler The method graph compiler to use. * @return A delegation that redirects invocations to the return value of a method that is declared by the instrumented type. */ public MethodDelegation toMethodReturnOf(String name, MethodGraph.Compiler methodGraphCompiler) { return new MethodDelegation(new ImplementationDelegate.ForMethodReturn(name, methodGraphCompiler, parameterBinders, matcher), parameterBinders, ambiguityResolver, bindingResolver); } }
/** * Delegates any intercepted method to invoke a non-{@code static} method on the instance of the supplied field. To be * considered a valid delegation target, a method must be visible and accessible to the instrumented type. This is the * case if the method's declaring type is either public or in the same package as the instrumented type and if the method * is either public or non-private and in the same package as the instrumented type. Private methods can only be used as * a delegation target if the delegation is targeting the instrumented type. * * @param name The field's name. * @param fieldLocatorFactory The field locator factory to use. * @param methodGraphCompiler The method graph compiler to use. * @return A delegation that redirects invocations to a method of the specified field's instance. */ public MethodDelegation toField(String name, FieldLocator.Factory fieldLocatorFactory, MethodGraph.Compiler methodGraphCompiler) { return new MethodDelegation(new ImplementationDelegate.ForField.WithLookup(name, methodGraphCompiler, parameterBinders, matcher, fieldLocatorFactory), parameterBinders, ambiguityResolver, bindingResolver); }
/** * Delegates any intercepted method to invoke a non-{@code static} method that is declared by the supplied type's instance or any * of its super types. To be considered a valid delegation target, a method must be visible and accessible to the instrumented type. * This is the case if the method's declaring type is either public or in the same package as the instrumented type and if the method * is either public or non-private and in the same package as the instrumented type. Private methods can only be used as * a delegation target if the delegation is targeting the instrumented type. * * @param target The target instance for the delegation. * @param type The most specific type of which {@code target} should be considered. Must be a super type of the target's actual type. * @param fieldName The name of the field that is holding the {@code target} instance. * @param methodGraphCompiler The method graph compiler to use. * @return A method delegation that redirects method calls to a static method of the supplied type. */ public MethodDelegation to(Object target, Type type, String fieldName, MethodGraph.Compiler methodGraphCompiler) { TypeDescription.Generic typeDescription = TypeDefinition.Sort.describe(type); if (!typeDescription.asErasure().isInstance(target)) { throw new IllegalArgumentException(target + " is not an instance of " + type); } return new MethodDelegation(new ImplementationDelegate.ForField.WithInstance(fieldName, methodGraphCompiler, parameterBinders, matcher, target, typeDescription), parameterBinders, ambiguityResolver, bindingResolver); }
/** * Delegates any intercepted method to invoke a {@code static} method that is declared by the supplied type. To be considered * a valid delegation target, the target method must be visible and accessible to the instrumented type. This is the case if * the target type is either public or in the same package as the instrumented type and if the target method is either public * or non-private and in the same package as the instrumented type. Private methods can only be used as a delegation target if * the delegation is targeting the instrumented type. * * @param typeDescription The target type for the delegation. * @return A method delegation that redirects method calls to a static method of the supplied type. */ public MethodDelegation to(TypeDescription typeDescription) { if (typeDescription.isArray()) { throw new IllegalArgumentException("Cannot delegate to array " + typeDescription); } else if (typeDescription.isPrimitive()) { throw new IllegalArgumentException("Cannot delegate to primitive " + typeDescription); } return new MethodDelegation(ImplementationDelegate.ForStaticMethod.of(typeDescription.getDeclaredMethods().filter(isStatic().and(matcher)), TargetMethodAnnotationDrivenBinder.of(parameterBinders)), parameterBinders, ambiguityResolver, bindingResolver); }
/** * Delegates any intercepted method to invoke a constructor of the supplied type. To be considered a valid delegation target, * a constructor must be visible and accessible to the instrumented type. This is the case if the constructor's declaring type is * either public or in the same package as the instrumented type and if the constructor is either public or non-private and in * the same package as the instrumented type. Private constructors can only be used as a delegation target if the delegation is * targeting the instrumented type. * * @param typeDescription The type to construct. * @return A delegation that redirects method calls to a constructor of the supplied type. */ public MethodDelegation toConstructor(TypeDescription typeDescription) { return new MethodDelegation(ImplementationDelegate.ForConstruction.of(typeDescription, typeDescription.getDeclaredMethods().filter(isConstructor().and(matcher)), TargetMethodAnnotationDrivenBinder.of(parameterBinders)), parameterBinders, ambiguityResolver, bindingResolver); }