• 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

96.77
/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/bind/annotation/DefaultCall.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.TypeDescription;
24
import net.bytebuddy.implementation.Implementation;
25
import net.bytebuddy.implementation.auxiliary.MethodCallProxy;
26
import net.bytebuddy.implementation.bind.MethodDelegationBinder;
27
import net.bytebuddy.implementation.bytecode.StackManipulation;
28
import net.bytebuddy.implementation.bytecode.assign.Assigner;
29
import net.bytebuddy.implementation.bytecode.constant.NullConstant;
30

31
import java.lang.annotation.Documented;
32
import java.lang.annotation.ElementType;
33
import java.lang.annotation.Retention;
34
import java.lang.annotation.RetentionPolicy;
35
import java.lang.annotation.Target;
36
import java.util.concurrent.Callable;
37

38
import static net.bytebuddy.matcher.ElementMatchers.named;
39

40
/**
41
 * A parameter with this annotation is assigned a proxy for invoking a default method that fits the intercepted method.
42
 * If no suitable default method for the intercepted method can be identified, the target method with the annotated
43
 * parameter is considered to be unbindable, unless a {@code null} value is requested by setting a property.
44
 *
45
 * @see net.bytebuddy.implementation.MethodDelegation
46
 * @see net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder
47
 */
48
@Documented
49
@Retention(RetentionPolicy.RUNTIME)
50
@Target(ElementType.PARAMETER)
51
public @interface DefaultCall {
52

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

68
    /**
69
     * Determines if the generated proxy should be {@link java.io.Serializable}.
70
     *
71
     * @return {@code true} if the generated proxy should be {@link java.io.Serializable}.
72
     */
73
    boolean serializableProxy() default false;
74

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

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

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

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

99
        /**
100
         * A reference to the serializable proxy method of the default call annotation.
101
         */
102
        private static final MethodDescription.InDefinedShape SERIALIZABLE_PROXY;
103

104
        /**
105
         * A reference to the null if possible method of the default call annotation.
106
         */
107
        private static final MethodDescription.InDefinedShape NULL_IF_IMPOSSIBLE;
108

109
        /*
110
         * Looks up method constants of the default call annotation.
111
         */
112
        static {
113
            MethodList<MethodDescription.InDefinedShape> annotationProperties = TypeDescription.ForLoadedType.of(DefaultCall.class).getDeclaredMethods();
1✔
114
            TARGET_TYPE = annotationProperties.filter(named("targetType")).getOnly();
1✔
115
            SERIALIZABLE_PROXY = annotationProperties.filter(named("serializableProxy")).getOnly();
1✔
116
            NULL_IF_IMPOSSIBLE = annotationProperties.filter(named("nullIfImpossible")).getOnly();
1✔
117
        }
1✔
118

119
        /**
120
         * {@inheritDoc}
121
         */
122
        public Class<DefaultCall> getHandledType() {
123
            return DefaultCall.class;
1✔
124
        }
125

126
        /**
127
         * {@inheritDoc}
128
         */
129
        public MethodDelegationBinder.ParameterBinding<?> bind(AnnotationDescription.Loadable<DefaultCall> annotation,
130
                                                               MethodDescription source,
131
                                                               ParameterDescription target,
132
                                                               Implementation.Target implementationTarget,
133
                                                               Assigner assigner,
134
                                                               Assigner.Typing typing) {
135
            TypeDescription targetType = target.getType().asErasure();
1✔
136
            if (!targetType.represents(Runnable.class) && !targetType.represents(Callable.class) && !targetType.represents(Object.class)) {
1✔
137
                throw new IllegalStateException("A default method call proxy can only be assigned to Runnable or Callable types: " + target);
1✔
138
            } else if (source.isConstructor()) {
1✔
139
                return annotation.getValue(NULL_IF_IMPOSSIBLE).resolve(Boolean.class)
1✔
140
                        ? new MethodDelegationBinder.ParameterBinding.Anonymous(NullConstant.INSTANCE)
141
                        : MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
142
            }
143
            TypeDescription typeDescription = annotation.getValue(TARGET_TYPE).resolve(TypeDescription.class);
1✔
144
            Implementation.SpecialMethodInvocation specialMethodInvocation = (typeDescription.represents(void.class)
1✔
145
                    ? DefaultMethodLocator.Implicit.INSTANCE
146
                    : new DefaultMethodLocator.Explicit(typeDescription)).resolve(implementationTarget, source).withCheckedCompatibilityTo(source.asTypeToken());
1✔
147
            StackManipulation stackManipulation;
148
            if (specialMethodInvocation.isValid()) {
1✔
149
                stackManipulation = new MethodCallProxy.AssignableSignatureCall(specialMethodInvocation, annotation.getValue(SERIALIZABLE_PROXY).resolve(Boolean.class));
1✔
150
            } else if (annotation.getValue(NULL_IF_IMPOSSIBLE).resolve(Boolean.class)) {
1✔
151
                stackManipulation = NullConstant.INSTANCE;
×
152
            } else {
153
                return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
1✔
154
            }
155
            return new MethodDelegationBinder.ParameterBinding.Anonymous(stackManipulation);
1✔
156
        }
157

158
        /**
159
         * A default method locator is responsible for looking up a default method to a given source method.
160
         */
161
        protected interface DefaultMethodLocator {
162

163
            /**
164
             * Locates the correct default method to a given source method.
165
             *
166
             * @param implementationTarget The current implementation target.
167
             * @param source               The source method for which a default method should be looked up.
168
             * @return A special method invocation of the default method or an illegal special method invocation,
169
             * if no suitable invocation could be located.
170
             */
171
            Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source);
172

173
            /**
174
             * An implicit default method locator that only permits the invocation of a default method if the source
175
             * method itself represents a method that was defined on a default method interface.
176
             */
177
            enum Implicit implements DefaultMethodLocator {
1✔
178

179
                /**
180
                 * The singleton instance.
181
                 */
182
                INSTANCE;
1✔
183

184
                /**
185
                 * {@inheritDoc}
186
                 */
187
                public Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source) {
188
                    return implementationTarget.invokeDefault(source.asSignatureToken());
1✔
189
                }
190
            }
191

192
            /**
193
             * An explicit default method locator attempts to look up a default method in the specified interface type.
194
             */
195
            @HashCodeAndEqualsPlugin.Enhance
196
            class Explicit implements DefaultMethodLocator {
197

198
                /**
199
                 * A description of the type on which the default method should be invoked.
200
                 */
201
                private final TypeDescription typeDescription;
202

203
                /**
204
                 * Creates a new explicit default method locator.
205
                 *
206
                 * @param typeDescription The actual target interface as explicitly defined by
207
                 *                        {@link DefaultCall#targetType()}.
208
                 */
209
                public Explicit(TypeDescription typeDescription) {
1✔
210
                    this.typeDescription = typeDescription;
1✔
211
                }
1✔
212

213
                /**
214
                 * {@inheritDoc}
215
                 */
216
                public Implementation.SpecialMethodInvocation resolve(Implementation.Target implementationTarget, MethodDescription source) {
217
                    if (!typeDescription.isInterface()) {
1✔
218
                        throw new IllegalStateException(source + " method carries default method call parameter on non-interface type");
1✔
219
                    }
220
                    return implementationTarget.invokeDefault(source.asSignatureToken(), typeDescription);
1✔
221
                }
222
            }
223
        }
224
    }
225
}
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