1 /*
2  * Copyright 2014 - 2020 Rafael Winterhalter
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.bytebuddy.implementation.bind.annotation;
17
18 import net.bytebuddy.description.annotation.AnnotationDescription;
19 import net.bytebuddy.description.method.MethodDescription;
20 import net.bytebuddy.description.method.ParameterDescription;
21 import net.bytebuddy.description.type.TypeDescription;
22 import net.bytebuddy.implementation.Implementation;
23 import net.bytebuddy.implementation.auxiliary.MethodCallProxy;
24 import net.bytebuddy.implementation.bind.MethodDelegationBinder;
25 import net.bytebuddy.implementation.bytecode.StackManipulation;
26 import net.bytebuddy.implementation.bytecode.assign.Assigner;
27 import net.bytebuddy.implementation.bytecode.constant.NullConstant;
28
29 import java.lang.annotation.*;
30 import java.util.concurrent.Callable;
31
32 /**
33  * Parameters that are annotated with this annotation will be assigned a proxy for calling the instrumented method's
34  * {@code super} implementation. If a method does not have a super implementation, calling the annotated proxy will
35  * throw an exception.
36  * <p>&nbsp;</p>
37  * The proxy will both implement the {@link java.util.concurrent.Callable} and the {@link java.lang.Runnable} interfaces
38  * such that the annotated parameter must be assignable to any of those interfaces or be of the {@link java.lang.Object}
39  * type.
40  *
41  * @see net.bytebuddy.implementation.MethodDelegation
42  * @see net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder
43  */

44 @Documented
45 @Retention(RetentionPolicy.RUNTIME)
46 @Target(ElementType.PARAMETER)
47 public @interface SuperCall {
48
49     /**
50      * Determines if the generated proxy should be {@link java.io.Serializable}.
51      *
52      * @return {@code trueif the generated proxy should be {@link java.io.Serializable}.
53      */

54     boolean serializableProxy() default false;
55
56     /**
57      * Determines if the injected proxy should invoke the default method to the intercepted method if a common
58      * super method invocation is not applicable. For this to be possible, the default method must not be ambiguous.
59      *
60      * @return {@code trueif the invocation should fall back to invoking the default method.
61      */

62     boolean fallbackToDefault() default true;
63
64     /**
65      * Assigns {@code null} to the parameter if it is impossible to invoke the super method or a possible dominant default method, if permitted.
66      *
67      * @return {@code trueif a {@code null} constant should be assigned to this parameter in case that a legal binding is impossible.
68      */

69     boolean nullIfImpossible() default false;
70
71     /**
72      * A binder for handling the
73      * {@link net.bytebuddy.implementation.bind.annotation.SuperCall}
74      * annotation.
75      *
76      * @see TargetMethodAnnotationDrivenBinder
77      */

78     enum Binder implements TargetMethodAnnotationDrivenBinder.ParameterBinder<SuperCall> {
79
80         /**
81          * The singleton instance.
82          */

83         INSTANCE;
84
85         /**
86          * {@inheritDoc}
87          */

88         public Class<SuperCall> getHandledType() {
89             return SuperCall.class;
90         }
91
92         /**
93          * {@inheritDoc}
94          */

95         public MethodDelegationBinder.ParameterBinding<?> bind(AnnotationDescription.Loadable<SuperCall> annotation,
96                                                                MethodDescription source,
97                                                                ParameterDescription target,
98                                                                Implementation.Target implementationTarget,
99                                                                Assigner assigner,
100                                                                Assigner.Typing typing) {
101             TypeDescription targetType = target.getType().asErasure();
102             if (!targetType.represents(Runnable.class) && !targetType.represents(Callable.class) && !targetType.represents(Object.class)) {
103                 throw new IllegalStateException("A super method call proxy can only be assigned to Runnable or Callable types: " + target);
104             } else if (source.isConstructor()) {
105                 return annotation.load().nullIfImpossible()
106                         ? new MethodDelegationBinder.ParameterBinding.Anonymous(NullConstant.INSTANCE)
107                         : MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
108             }
109             Implementation.SpecialMethodInvocation specialMethodInvocation = (annotation.load().fallbackToDefault()
110                     ? implementationTarget.invokeDominant(source.asSignatureToken())
111                     : implementationTarget.invokeSuper(source.asSignatureToken())).withCheckedCompatibilityTo(source.asTypeToken());
112             StackManipulation stackManipulation;
113             if (specialMethodInvocation.isValid()) {
114                 stackManipulation = new MethodCallProxy.AssignableSignatureCall(specialMethodInvocation, annotation.load().serializableProxy());
115             } else if (annotation.load().nullIfImpossible()) {
116                 stackManipulation = NullConstant.INSTANCE;
117             } else {
118                 return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
119             }
120             return new MethodDelegationBinder.ParameterBinding.Anonymous(stackManipulation);
121         }
122     }
123 }
124