com.fitbur.bytebuddy.implementation.bind.annotation.SuperCall Maven / Gradle / Ivy
package com.fitbur.bytebuddy.implementation.bind.annotation;
import com.fitbur.bytebuddy.description.annotation.AnnotationDescription;
import com.fitbur.bytebuddy.description.method.MethodDescription;
import com.fitbur.bytebuddy.description.method.ParameterDescription;
import com.fitbur.bytebuddy.description.type.TypeDescription;
import com.fitbur.bytebuddy.implementation.Implementation;
import com.fitbur.bytebuddy.implementation.auxiliary.MethodCallProxy;
import com.fitbur.bytebuddy.implementation.bind.MethodDelegationBinder;
import com.fitbur.bytebuddy.implementation.bytecode.assign.Assigner;
import java.lang.annotation.*;
import java.util.concurrent.Callable;
/**
* Parameters that are annotated with this annotation will be assigned a proxy for calling the instrumented method's
* {@code super} implementation. If a method does not have a super implementation, calling the annotated proxy will
* throw an exception.
*
* The proxy will both implement the {@link java.util.concurrent.Callable} and the {@link java.lang.Runnable} interfaces
* such that the annotated parameter must be assignable to any of those interfaces or be of the {@link java.lang.Object}
* type.
*
* @see com.fitbur.bytebuddy.implementation.MethodDelegation
* @see com.fitbur.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface SuperCall {
/**
* Determines if the generated proxy should be {@link java.io.Serializable}.
*
* @return {@code true} if the generated proxy should be {@link java.io.Serializable}.
*/
boolean serializableProxy() default false;
/**
* Determines if the injected proxy should invoke the default method to the intercepted method if a common
* super method invocation is not applicable. For this to be possible, the default method must not be ambiguous.
*
* @return {@code true} if the invocation should fall back to invoking the default method.
*/
boolean fallbackToDefault() default true;
/**
* A binder for handling the
* {@link com.fitbur.bytebuddy.implementation.bind.annotation.SuperCall}
* annotation.
*
* @see TargetMethodAnnotationDrivenBinder
*/
enum Binder implements TargetMethodAnnotationDrivenBinder.ParameterBinder {
/**
* The singleton instance.
*/
INSTANCE;
@Override
public Class getHandledType() {
return SuperCall.class;
}
@Override
public MethodDelegationBinder.ParameterBinding> bind(AnnotationDescription.Loadable annotation,
MethodDescription source,
ParameterDescription target,
Implementation.Target implementationTarget,
Assigner assigner) {
TypeDescription targetType = target.getType().asErasure();
if (!targetType.represents(Runnable.class) && !targetType.represents(Callable.class) && !targetType.represents(Object.class)) {
throw new IllegalStateException("A super method call proxy can only be assigned to Runnable or Callable types: " + target);
} else if (source.isConstructor()) {
return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
}
Implementation.SpecialMethodInvocation specialMethodInvocation = annotation.loadSilent().fallbackToDefault()
? implementationTarget.invokeDominant(source.asSignatureToken())
: implementationTarget.invokeSuper(source.asSignatureToken());
return specialMethodInvocation.isValid()
? new MethodDelegationBinder.ParameterBinding.Anonymous(new MethodCallProxy
.AssignableSignatureCall(specialMethodInvocation, annotation.loadSilent().serializableProxy()))
: MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
}
@Override
public String toString() {
return "SuperCall.Binder." + name();
}
}
}