• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

raphw / byte-buddy / #801

27 Oct 2025 09:37AM UTC coverage: 84.715% (-0.4%) from 85.118%
#801

push

raphw
Fix imports.

29586 of 34924 relevant lines covered (84.72%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.47
/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/bind/annotation/DefaultCallHandle.java
1
/*
2
 * Copyright 2014 - Present 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.build.HashCodeAndEqualsPlugin;
19
import net.bytebuddy.description.annotation.AnnotationDescription;
20
import net.bytebuddy.description.method.MethodDescription;
21
import net.bytebuddy.description.method.MethodList;
22
import net.bytebuddy.description.method.ParameterDescription;
23
import net.bytebuddy.description.type.TypeDefinition;
24
import net.bytebuddy.description.type.TypeDescription;
25
import net.bytebuddy.description.type.TypeList;
26
import net.bytebuddy.implementation.Implementation;
27
import net.bytebuddy.implementation.bind.MethodDelegationBinder;
28
import net.bytebuddy.implementation.bytecode.StackManipulation;
29
import net.bytebuddy.implementation.bytecode.assign.Assigner;
30
import net.bytebuddy.implementation.bytecode.collection.ArrayFactory;
31
import net.bytebuddy.implementation.bytecode.constant.IntegerConstant;
32
import net.bytebuddy.implementation.bytecode.constant.NullConstant;
33
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
34
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
35
import net.bytebuddy.utility.JavaType;
36
import org.objectweb.asm.Opcodes;
37

38
import java.lang.annotation.Documented;
39
import java.lang.annotation.ElementType;
40
import java.lang.annotation.Retention;
41
import java.lang.annotation.RetentionPolicy;
42
import java.lang.annotation.Target;
43
import java.util.ArrayList;
44
import java.util.List;
45

46
import static net.bytebuddy.matcher.ElementMatchers.named;
47

48
/**
49
 * A parameter with this annotation is assigned a method handle for invoking a default method that fits the intercepted method.
50
 * If no suitable default method for the intercepted method can be identified, the target method with the annotated
51
 * parameter is considered to be unbindable, unless a {@code null} value is requested by setting a property.
52
 *
53
 * @see net.bytebuddy.implementation.MethodDelegation
54
 * @see TargetMethodAnnotationDrivenBinder
55
 */
56
@Documented
57
@Retention(RetentionPolicy.RUNTIME)
58
@Target(ElementType.PARAMETER)
59
public @interface DefaultCallHandle {
60

61
    /**
62
     * If this parameter is not explicitly set, a parameter with the {@link DefaultCallHandle} is only bound to a
63
     * source method if this source method directly represents an unambiguous, invokable default method. On the other
64
     * hand, if a method is not defined unambiguously by an interface, not setting this parameter will exclude
65
     * the target method with the annotated parameter from a binding to the source method.
66
     * <p>&nbsp;</p>
67
     * If this parameter is however set to an explicit interface type, a default method is always invoked on this given
68
     * type as long as this type defines a method with a compatible signature. If this is not the case, the target
69
     * method with the annotated parameter is no longer considered as a possible binding candidate of a source method.
70
     *
71
     * @return The target interface that a default method invocation is to be defined upon. If no such explicit target
72
     * is set, this parameter should not be defined as the predefined {@code void} type encodes an implicit resolution.
73
     */
74
    Class<?> targetType() default void.class;
75

76
    /**
77
     * Assigns {@code null} to the parameter if it is impossible to invoke the super method or a possible dominant default method, if permitted.
78
     *
79
     * @return {@code true} if a {@code null} constant should be assigned to this parameter in case that a legal binding is impossible.
80
     */
81
    boolean nullIfImpossible() default false;
82

83
    /**
84
     * A binder for handling the {@link DefaultCallHandle} annotation.
85
     *
86
     * @see TargetMethodAnnotationDrivenBinder
87
     */
88
    enum Binder implements TargetMethodAnnotationDrivenBinder.ParameterBinder<DefaultCallHandle> {
1✔
89

90
        /**
91
         * The singleton instance.
92
         */
93
        INSTANCE;
1✔
94

95
        /**
96
         * A reference to the target type method of the default call annotation.
97
         */
98
        private static final MethodDescription.InDefinedShape TARGET_TYPE;
99

100
        /**
101
         * A reference to the null if possible method of the default call annotation.
102
         */
103
        private static final MethodDescription.InDefinedShape NULL_IF_IMPOSSIBLE;
104

105
        /*
106
         * Looks up method constants of the default call annotation.
107
         */
108
        static {
109
            MethodList<MethodDescription.InDefinedShape> annotationProperties = TypeDescription.ForLoadedType.of(DefaultCallHandle.class).getDeclaredMethods();
1✔
110
            TARGET_TYPE = annotationProperties.filter(named("targetType")).getOnly();
1✔
111
            NULL_IF_IMPOSSIBLE = annotationProperties.filter(named("nullIfImpossible")).getOnly();
1✔
112
        }
1✔
113

114
        /**
115
         * {@inheritDoc}
116
         */
117
        public Class<DefaultCallHandle> getHandledType() {
118
            return DefaultCallHandle.class;
1✔
119
        }
120

121
        /**
122
         * {@inheritDoc}
123
         */
124
        public MethodDelegationBinder.ParameterBinding<?> bind(AnnotationDescription.Loadable<DefaultCallHandle> annotation,
125
                                                               MethodDescription source,
126
                                                               ParameterDescription target,
127
                                                               Implementation.Target implementationTarget,
128
                                                               Assigner assigner,
129
                                                               Assigner.Typing typing) {
130
            if (!target.getType().asErasure().isAssignableFrom(JavaType.METHOD_HANDLE.getTypeStub())) {
1✔
131
                throw new IllegalStateException("Cannot assign MethodHandle type to " + target);
1✔
132
            } else if (source.isConstructor()) {
1✔
133
                return annotation.getValue(NULL_IF_IMPOSSIBLE).resolve(Boolean.class)
1✔
134
                        ? new MethodDelegationBinder.ParameterBinding.Anonymous(NullConstant.INSTANCE)
135
                        : MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
136
            }
137
            TypeDescription typeDescription = annotation.getValue(TARGET_TYPE).resolve(TypeDescription.class);
1✔
138
            Implementation.SpecialMethodInvocation specialMethodInvocation = (typeDescription.represents(void.class)
1✔
139
                    ? DefaultMethodLocator.Implicit.INSTANCE
140
                    : new DefaultMethodLocator.Explicit(typeDescription)).resolve(implementationTarget, source).withCheckedCompatibilityTo(source.asTypeToken());
1✔
141
            StackManipulation stackManipulation;
142
            if (specialMethodInvocation.isValid()) {
1✔
143
                List<StackManipulation> stackManipulations = new ArrayList<StackManipulation>(3 + source.getParameters().size() * 3);
1✔
144
                stackManipulations.add(specialMethodInvocation.toMethodHandle().toStackManipulation());
1✔
145
                stackManipulations.add(MethodVariableAccess.loadThis());
1✔
146
                stackManipulations.add(MethodInvocation.invoke(new MethodDescription.Latent(JavaType.METHOD_HANDLE.getTypeStub(), new MethodDescription.Token("bindTo",
1✔
147
                        Opcodes.ACC_PUBLIC,
148
                        JavaType.METHOD_HANDLE.getTypeStub().asGenericType(),
1✔
149
                        new TypeList.Generic.Explicit(TypeDefinition.Sort.describe(Object.class))))));
1✔
150
                if (!source.getParameters().isEmpty()) {
1✔
151
                    List<StackManipulation> values = new ArrayList<StackManipulation>(source.getParameters().size());
×
152
                    for (ParameterDescription parameterDescription : source.getParameters()) {
×
153
                        values.add(parameterDescription.getType().isPrimitive() ? new StackManipulation.Compound(MethodVariableAccess.load(parameterDescription), assigner.assign(parameterDescription.getType(),
×
154
                                parameterDescription.getType().asErasure().asBoxed().asGenericType(),
×
155
                                typing)) : MethodVariableAccess.load(parameterDescription));
×
156
                    }
×
157
                    stackManipulations.add(IntegerConstant.forValue(0));
×
158
                    stackManipulations.add(ArrayFactory.forType(TypeDescription.ForLoadedType.of(Object.class).asGenericType()).withValues(values));
×
159
                    stackManipulations.add(MethodInvocation.invoke(new MethodDescription.Latent(JavaType.METHOD_HANDLES.getTypeStub(), new MethodDescription.Token("insertArguments",
×
160
                            Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
161
                            JavaType.METHOD_HANDLE.getTypeStub().asGenericType(),
×
162
                            new TypeList.Generic.Explicit(JavaType.METHOD_HANDLE.getTypeStub(), TypeDefinition.Sort.describe(int.class), TypeDefinition.Sort.describe(Object[].class))))));
×
163
                }
164
                stackManipulation = new StackManipulation.Compound(stackManipulations);
1✔
165
            } else if (annotation.getValue(NULL_IF_IMPOSSIBLE).resolve(Boolean.class)) {
1✔
166
                stackManipulation = NullConstant.INSTANCE;
×
167
            } else {
168
                return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
1✔
169
            }
170
            return new MethodDelegationBinder.ParameterBinding.Anonymous(stackManipulation);
1✔
171
        }
172

173
        /**
174
         * A default method locator is responsible for looking up a default method to a given source method.
175
         */
176
        protected interface DefaultMethodLocator {
177

178
            /**
179
             * Locates the correct default method to a given source method.
180
             *
181
             * @param implementationTarget The current implementation target.
182
             * @param source               The source method for which a default method should be looked up.
183
             * @return A special method invocation of the default method or an illegal special method invocation,
184
             * if no suitable invocation could be located.
185
             */
186
            Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source);
187

188
            /**
189
             * An implicit default method locator that only permits the invocation of a default method if the source
190
             * method itself represents a method that was defined on a default method interface.
191
             */
192
            enum Implicit implements DefaultMethodLocator {
1✔
193

194
                /**
195
                 * The singleton instance.
196
                 */
197
                INSTANCE;
1✔
198

199
                /**
200
                 * {@inheritDoc}
201
                 */
202
                public Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source) {
203
                    return implementationTarget.invokeDefault(source.asSignatureToken());
1✔
204
                }
205
            }
206

207
            /**
208
             * An explicit default method locator attempts to look up a default method in the specified interface type.
209
             */
210
            @HashCodeAndEqualsPlugin.Enhance
211
            class Explicit implements DefaultMethodLocator {
212

213
                /**
214
                 * A description of the type on which the default method should be invoked.
215
                 */
216
                private final TypeDescription typeDescription;
217

218
                /**
219
                 * Creates a new explicit default method locator.
220
                 *
221
                 * @param typeDescription The actual target interface as explicitly defined by
222
                 *                        {@link DefaultCallHandle#targetType()}.
223
                 */
224
                public Explicit(TypeDescription typeDescription) {
1✔
225
                    this.typeDescription = typeDescription;
1✔
226
                }
1✔
227

228
                /**
229
                 * {@inheritDoc}
230
                 */
231
                public Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source) {
232
                    if (!typeDescription.isInterface()) {
1✔
233
                        throw new IllegalStateException(source + " method carries default method call parameter on non-interface type");
1✔
234
                    }
235
                    return implementationTarget.invokeDefault(source.asSignatureToken(), typeDescription);
1✔
236
                }
237
            }
238
        }
239
    }
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc