• 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.43
/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/bind/annotation/Default.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.TypeProxy;
26
import net.bytebuddy.implementation.bind.MethodDelegationBinder;
27
import net.bytebuddy.implementation.bytecode.assign.Assigner;
28

29
import java.lang.annotation.Documented;
30
import java.lang.annotation.ElementType;
31
import java.lang.annotation.Retention;
32
import java.lang.annotation.RetentionPolicy;
33
import java.lang.annotation.Target;
34

35
import static net.bytebuddy.matcher.ElementMatchers.named;
36

37
/**
38
 * Parameters that are annotated with this annotation are assigned an instance of an auxiliary proxy type that allows calling
39
 * any default method of an interface of the instrumented type where the parameter type must be an interface that is
40
 * directly implemented by the instrumented type. The generated proxy will directly implement the parameter's
41
 * interface. If the interface of the annotation is not implemented by the instrumented type, the method with this
42
 * parameter is not considered as a binding target.
43
 *
44
 * @see net.bytebuddy.implementation.MethodDelegation
45
 * @see net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder
46
 */
47
@Documented
48
@Retention(RetentionPolicy.RUNTIME)
49
@Target(ElementType.PARAMETER)
50
public @interface Default {
51

52
    /**
53
     * Determines if the generated proxy should be {@link java.io.Serializable}. If the annotated type
54
     * already is serializable, such an explicit specification is not required.
55
     *
56
     * @return {@code true} if the generated proxy should be {@link java.io.Serializable}.
57
     */
58
    boolean serializableProxy() default false;
59

60
    /**
61
     * Determines the type that is implemented by the proxy. When this value is set to its default value
62
     * {@code void}, the proxy is created as an instance of the parameter's type. It is <b>not</b> possible to
63
     * set the value of this property to {@link net.bytebuddy.dynamic.TargetType} as a interface cannot implement itself.
64
     *
65
     * @return The type of the proxy or an indicator type, i.e. {@code void}.
66
     */
67
    Class<?> proxyType() default void.class;
68

69
    /**
70
     * A binder for the {@link net.bytebuddy.implementation.bind.annotation.Default} annotation.
71
     */
72
    enum Binder implements TargetMethodAnnotationDrivenBinder.ParameterBinder<Default> {
1✔
73

74
        /**
75
         * The singleton instance.
76
         */
77
        INSTANCE;
1✔
78

79
        /**
80
         * A method reference to the serializable proxy property.
81
         */
82
        private static final MethodDescription.InDefinedShape SERIALIZABLE_PROXY;
83

84
        /**
85
         * A method reference to the proxy type property.
86
         */
87
        private static final MethodDescription.InDefinedShape PROXY_TYPE;
88

89
        /*
90
         * Extracts method references of the default annotation.
91
         */
92
        static {
93
            MethodList<MethodDescription.InDefinedShape> annotationProperties = TypeDescription.ForLoadedType.of(Default.class).getDeclaredMethods();
1✔
94
            SERIALIZABLE_PROXY = annotationProperties.filter(named("serializableProxy")).getOnly();
1✔
95
            PROXY_TYPE = annotationProperties.filter(named("proxyType")).getOnly();
1✔
96
        }
1✔
97

98
        /**
99
         * {@inheritDoc}
100
         */
101
        public Class<Default> getHandledType() {
102
            return Default.class;
1✔
103
        }
104

105
        /**
106
         * {@inheritDoc}
107
         */
108
        public MethodDelegationBinder.ParameterBinding<?> bind(AnnotationDescription.Loadable<Default> annotation,
109
                                                               MethodDescription source,
110
                                                               ParameterDescription target,
111
                                                               Implementation.Target implementationTarget,
112
                                                               Assigner assigner,
113
                                                               Assigner.Typing typing) {
114
            TypeDescription proxyType = TypeLocator.ForType.of(annotation.getValue(PROXY_TYPE).resolve(TypeDescription.class)).resolve(target.getType());
1✔
115
            if (!proxyType.isInterface()) {
1✔
116
                throw new IllegalStateException(target + " uses the @Default annotation on an invalid type");
1✔
117
            }
118
            if (source.isStatic() || !implementationTarget.getInstrumentedType().getInterfaces().asErasures().contains(proxyType)) {
1✔
119
                return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
1✔
120
            } else {
121
                return new MethodDelegationBinder.ParameterBinding.Anonymous(new TypeProxy.ForDefaultMethod(proxyType,
1✔
122
                        implementationTarget,
123
                        annotation.getValue(SERIALIZABLE_PROXY).resolve(Boolean.class)));
1✔
124
            }
125
        }
126

127
        /**
128
         * Locates the type which should be the base type of the created proxy.
129
         */
130
        protected interface TypeLocator {
131

132
            /**
133
             * Resolves the target type.
134
             *
135
             * @param parameterType The type of the target parameter.
136
             * @return The proxy type.
137
             */
138
            TypeDescription resolve(TypeDescription.Generic parameterType);
139

140
            /**
141
             * A type locator that yields the target parameter's type.
142
             */
143
            enum ForParameterType implements TypeLocator {
1✔
144

145
                /**
146
                 * The singleton instance.
147
                 */
148
                INSTANCE;
1✔
149

150
                /**
151
                 * {@inheritDoc}
152
                 */
153
                public TypeDescription resolve(TypeDescription.Generic parameterType) {
154
                    return parameterType.asErasure();
1✔
155
                }
156
            }
157

158
            /**
159
             * A type locator that returns a given type.
160
             */
161
            @HashCodeAndEqualsPlugin.Enhance
162
            class ForType implements TypeLocator {
163

164
                /**
165
                 * The type to be returned upon resolution.
166
                 */
167
                private final TypeDescription typeDescription;
168

169
                /**
170
                 * Creates a new type locator for a given type.
171
                 *
172
                 * @param typeDescription The type to be returned upon resolution.
173
                 */
174
                protected ForType(TypeDescription typeDescription) {
1✔
175
                    this.typeDescription = typeDescription;
1✔
176
                }
1✔
177

178
                /**
179
                 * Resolves a type locator based upon an annotation value.
180
                 *
181
                 * @param typeDescription The annotation's value.
182
                 * @return The appropriate type locator.
183
                 */
184
                protected static TypeLocator of(TypeDescription typeDescription) {
185
                    if (typeDescription.represents(void.class)) {
1✔
186
                        return ForParameterType.INSTANCE;
1✔
187
                    } else if (!typeDescription.isInterface()) {
1✔
188
                        throw new IllegalStateException("Cannot assign proxy to " + typeDescription);
1✔
189
                    } else {
190
                        return new ForType(typeDescription);
1✔
191
                    }
192
                }
193

194
                /**
195
                 * {@inheritDoc}
196
                 */
197
                public TypeDescription resolve(TypeDescription.Generic parameterType) {
198
                    if (!typeDescription.isAssignableTo(parameterType.asErasure())) {
1✔
199
                        throw new IllegalStateException("Impossible to assign " + typeDescription + " to parameter of type " + parameterType);
×
200
                    }
201
                    return typeDescription;
1✔
202
                }
203
            }
204
        }
205
    }
206
}
207

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