• 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

97.56
/byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDefinition.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.description.type;
17

18
import net.bytebuddy.build.AccessControllerPlugin;
19
import net.bytebuddy.description.ModifierReviewable;
20
import net.bytebuddy.description.NamedElement;
21
import net.bytebuddy.description.field.FieldList;
22
import net.bytebuddy.description.method.MethodList;
23
import net.bytebuddy.implementation.bytecode.StackSize;
24
import net.bytebuddy.utility.dispatcher.JavaDispatcher;
25
import net.bytebuddy.utility.nullability.MaybeNull;
26
import net.bytebuddy.utility.nullability.UnknownNull;
27

28
import java.lang.reflect.AnnotatedElement;
29
import java.lang.reflect.GenericArrayType;
30
import java.lang.reflect.ParameterizedType;
31
import java.lang.reflect.Type;
32
import java.lang.reflect.TypeVariable;
33
import java.lang.reflect.WildcardType;
34
import java.security.PrivilegedAction;
35
import java.util.Iterator;
36
import java.util.NoSuchElementException;
37

38
/**
39
 * Implementations define a type, either as a {@link TypeDescription} or as a {@link TypeDescription.Generic}.
40
 */
41
public interface TypeDefinition extends NamedElement, ModifierReviewable.ForTypeDefinition, Iterable<TypeDefinition> {
42

43
    /**
44
     * <p>
45
     * If this property is set to {@code true}, non-generic {@link TypeDefinition}s do no longer resolve their referenced
46
     * generic types when traversing type hierarchies. Setting this property can cause unexpected side effects such as
47
     * {@link ClassCastException}s from overridden methods as type variables are resolved to their erasures where a method
48
     * might return that is unexpected by the callee. Setting this property also makes type annotations unavailable using
49
     * such type navigation.
50
     * </p>
51
     * <p>
52
     * Setting this property can be useful if generic type information is not required in order to avoid bugs in
53
     * implementations of the JVM where processing generic types can cause segmentation faults. Byte Buddy will undertake
54
     * a best effort to retain the generic type information and information about type annotations within the redefined
55
     * types' class files. Typically, this property can be meaningful in combination with a Java agent that only changes
56
     * byte code without changing a class type's structure.
57
     * </p>
58
     */
59
    String RAW_TYPES_PROPERTY = "net.bytebuddy.raw";
60

61
    /**
62
     * Returns this type definition as a generic type.
63
     *
64
     * @return This type definition represented as a generic type.
65
     */
66
    TypeDescription.Generic asGenericType();
67

68
    /**
69
     * Returns the erasure of this type. Wildcard types ({@link TypeDescription.Generic.Sort#WILDCARD})
70
     * do not have a well-defined erasure and cause an {@link IllegalStateException} to be thrown.
71
     *
72
     * @return The erasure of this type.
73
     */
74
    TypeDescription asErasure();
75

76
    /**
77
     * Returns the super class of this type. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
78
     * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types. Interface types
79
     * and the {@link Object} class do not define a super class where {@code null} is returned. Array types define {@link Object}
80
     * as their direct super class.
81
     *
82
     * @return The super class of this type or {@code null} if no super class exists for this type.
83
     */
84
    @MaybeNull
85
    TypeDescription.Generic getSuperClass();
86

87
    /**
88
     * Returns the interfaces that this type implements. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
89
     * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types.
90
     *
91
     * @return The interfaces that this type implements.
92
     */
93
    TypeList.Generic getInterfaces();
94

95
    /**
96
     * Returns the fields that this type declares. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
97
     * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types. Generic array
98
     * types never define fields and the returned list is always empty for such types.
99
     *
100
     * @return The fields that this type declares. A super type is only defined for non-generic types ({@link Sort#NON_GENERIC}),
101
     * parameterized types ({@link Sort#PARAMETERIZED}) or generic array types ({@link Sort#GENERIC_ARRAY}) types. Generic array
102
     * types never define methods and the returned list is always empty for such types.
103
     */
104
    FieldList<?> getDeclaredFields();
105

106
    /**
107
     * Returns the methods that this type declares.
108
     *
109
     * @return The methods that this type declares.
110
     */
111
    MethodList<?> getDeclaredMethods();
112

113
    /**
114
     * <p>
115
     * Returns the component type of this type.
116
     * </p>
117
     * <p>
118
     * Only non-generic types ({@link TypeDescription.Generic.Sort#NON_GENERIC}) and generic array types
119
     * {@link TypeDescription.Generic.Sort#GENERIC_ARRAY}) define a component type. For other
120
     * types, an {@link IllegalStateException} is thrown.
121
     * </p>
122
     *
123
     * @return The component type of this type or {@code null} if this type does not represent an array type.
124
     */
125
    @MaybeNull
126
    TypeDefinition getComponentType();
127

128
    /**
129
     * Returns the list of record components that are declared by this type. If this type is not
130
     * a record, the returned list is empty.
131
     *
132
     * @return A list of record components that this type declares.
133
     */
134
    RecordComponentList<?> getRecordComponents();
135

136
    /**
137
     * Returns the sort of the generic type this instance represents.
138
     *
139
     * @return The sort of the generic type.
140
     */
141
    Sort getSort();
142

143
    /**
144
     * Returns the name of the type. For generic types, this name is their {@link Object#toString()} representations. For a non-generic
145
     * type, it is the fully qualified binary name of the type.
146
     *
147
     * @return The name of this type.
148
     */
149
    String getTypeName();
150

151
    /**
152
     * Returns the size of the type described by this instance. Wildcard types
153
     * ({@link TypeDescription.Generic.Sort#WILDCARD} do not have a well-defined a stack size and
154
     * cause an {@link IllegalStateException} to be thrown.
155
     *
156
     * @return The size of the type described by this instance.
157
     */
158
    StackSize getStackSize();
159

160
    /**
161
     * Checks if the type described by this entity is an array.
162
     *
163
     * @return {@code true} if this type description represents an array.
164
     */
165
    boolean isArray();
166

167
    /**
168
     * Checks if this type is a Java record.
169
     *
170
     * @return {@code true} if this type is a Java record.
171
     */
172
    boolean isRecord();
173

174
    /**
175
     * Checks if the type described by this entity is a primitive type.
176
     *
177
     * @return {@code true} if this type description represents a primitive type.
178
     */
179
    boolean isPrimitive();
180

181
    /**
182
     * Checks if the type described by this instance represents {@code type}.
183
     *
184
     * @param type The type of interest.
185
     * @return {@code true} if the type described by this instance represents {@code type}.
186
     */
187
    boolean represents(Type type);
188

189
    /**
190
     * Represents a {@link TypeDescription.Generic}'s form.
191
     */
192
    enum Sort {
1✔
193

194
        /**
195
         * Represents a non-generic type.
196
         */
197
        NON_GENERIC,
1✔
198

199
        /**
200
         * Represents a generic array type.
201
         */
202
        GENERIC_ARRAY,
1✔
203

204
        /**
205
         * Represents a parameterized type.
206
         */
207
        PARAMETERIZED,
1✔
208

209
        /**
210
         * Represents a wildcard type.
211
         */
212
        WILDCARD,
1✔
213

214
        /**
215
         * Represents a type variable that is attached to a {@link net.bytebuddy.description.TypeVariableSource}.
216
         */
217
        VARIABLE,
1✔
218

219
        /**
220
         * Represents a type variable that is merely symbolic and is not attached to a {@link net.bytebuddy.description.TypeVariableSource}
221
         * and does not defined bounds.
222
         */
223
        VARIABLE_SYMBOLIC;
1✔
224

225
        /**
226
         * A dispatcher for interacting with {@code java.lang.reflect.AnnotatedType}.
227
         */
228
        private static final AnnotatedType ANNOTATED_TYPE = doPrivileged(JavaDispatcher.of(AnnotatedType.class));
1✔
229

230
        /**
231
         * A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
232
         *
233
         * @param action The action to execute from a privileged context.
234
         * @param <T>    The type of the action's resolved value.
235
         * @return The action's resolved value.
236
         */
237
        @AccessControllerPlugin.Enhance
238
        private static <T> T doPrivileged(PrivilegedAction<T> action) {
239
            return action.run();
×
240
        }
241

242
        /**
243
         * Describes a loaded generic type as a {@link TypeDescription.Generic}.
244
         *
245
         * @param type The type to describe.
246
         * @return A description of the provided generic type.
247
         */
248
        public static TypeDescription.Generic describe(Type type) {
249
            return describe(type, TypeDescription.Generic.AnnotationReader.NoOp.INSTANCE);
1✔
250
        }
251

252
        /**
253
         * Describes a loaded {@code java.lang.reflect.AnnotatedType} as a {@link TypeDescription.Generic}.
254
         *
255
         * @param annotatedType The {@code java.lang.reflect.AnnotatedType} to describe.
256
         * @return A description of the provided generic type.
257
         */
258
        public static TypeDescription.Generic describeAnnotated(AnnotatedElement annotatedType) {
259
            if (!ANNOTATED_TYPE.isInstance(annotatedType)) {
1✔
260
                throw new IllegalArgumentException("Not an instance of AnnotatedType: " + annotatedType);
1✔
261
            }
262
            return describe(ANNOTATED_TYPE.getType(annotatedType), new TypeDescription.Generic.AnnotationReader.Delegator.Simple(annotatedType));
1✔
263
        }
264

265
        /**
266
         * Describes the generic type while using the supplied annotation reader for resolving type annotations if this
267
         * language feature is available on the current JVM.
268
         *
269
         * @param type             The type to describe.
270
         * @param annotationReader The annotation reader for extracting type annotations.
271
         * @return A description of the provided generic annotated type.
272
         */
273
        protected static TypeDescription.Generic describe(Type type, TypeDescription.Generic.AnnotationReader annotationReader) {
274
            if (type instanceof Class<?>) {
1✔
275
                return new TypeDescription.Generic.OfNonGenericType.ForLoadedType((Class<?>) type, annotationReader);
1✔
276
            } else if (type instanceof GenericArrayType) {
1✔
277
                return new TypeDescription.Generic.OfGenericArray.ForLoadedType((GenericArrayType) type, annotationReader);
1✔
278
            } else if (type instanceof ParameterizedType) {
1✔
279
                return new TypeDescription.Generic.OfParameterizedType.ForLoadedType((ParameterizedType) type, annotationReader);
1✔
280
            } else if (type instanceof TypeVariable) {
1✔
281
                return new TypeDescription.Generic.OfTypeVariable.ForLoadedType((TypeVariable<?>) type, annotationReader);
1✔
282
            } else if (type instanceof WildcardType) {
1✔
283
                return new TypeDescription.Generic.OfWildcardType.ForLoadedType((WildcardType) type, annotationReader);
1✔
284
            } else {
285
                throw new IllegalArgumentException("Unknown type: " + type);
1✔
286
            }
287
        }
288

289
        /**
290
         * Describes the generic type while using the supplied annotation reader for resolving type annotations if this
291
         * language feature is available on the current JVM. This method applies a check for null values as malformed signatures
292
         * might cause incorrectly formatted results. This might also be caused by obfuscation tools.
293
         *
294
         * @param type             The type to describe.
295
         * @param annotationReader The annotation reader for extracting type annotations.
296
         * @return A description of the provided generic annotated type.
297
         */
298
        protected static TypeDescription.Generic describeOrNull(@MaybeNull Type type, TypeDescription.Generic.AnnotationReader annotationReader) {
299
            if (type == null) {
1✔
300
                throw new TypeNotPresentException("<unknown>", null);
1✔
301
            }
302
            return describe(type, annotationReader);
1✔
303
        }
304

305
        /**
306
         * Checks if this type sort represents a non-generic type.
307
         *
308
         * @return {@code true} if this sort form represents a non-generic.
309
         */
310
        public boolean isNonGeneric() {
311
            return this == NON_GENERIC;
1✔
312
        }
313

314
        /**
315
         * Checks if this type sort represents a parameterized type.
316
         *
317
         * @return {@code true} if this sort form represents a parameterized type.
318
         */
319
        public boolean isParameterized() {
320
            return this == PARAMETERIZED;
1✔
321
        }
322

323
        /**
324
         * Checks if this type sort represents a generic array.
325
         *
326
         * @return {@code true} if this type sort represents a generic array.
327
         */
328
        public boolean isGenericArray() {
329
            return this == GENERIC_ARRAY;
1✔
330
        }
331

332
        /**
333
         * Checks if this type sort represents a wildcard.
334
         *
335
         * @return {@code true} if this type sort represents a wildcard.
336
         */
337
        public boolean isWildcard() {
338
            return this == WILDCARD;
1✔
339
        }
340

341
        /**
342
         * Checks if this type sort represents a type variable of any form.
343
         *
344
         * @return {@code true} if this type sort represents an attached type variable.
345
         */
346
        public boolean isTypeVariable() {
347
            return this == VARIABLE || this == VARIABLE_SYMBOLIC;
1✔
348
        }
349

350
        /**
351
         * A proxy for interacting with {@code java.lang.reflect.AnnotatedType}.
352
         */
353
        @JavaDispatcher.Proxied("java.lang.reflect.AnnotatedType")
354
        protected interface AnnotatedType {
355

356
            /**
357
             * Returns {@code true} if the supplied value is an instance of {@code java.lang.reflect.AnnotatedType}.
358
             *
359
             * @param value The instance to consider.
360
             * @return {@code true} if the supplied instance is of type {@code java.lang.reflect.AnnotatedType}.
361
             */
362
            @JavaDispatcher.Instance
363
            boolean isInstance(AnnotatedElement value);
364

365
            /**
366
             * Resolves the supplied {@code java.lang.reflect.AnnotatedType}'s type.
367
             *
368
             * @param value The {@code java.lang.reflect.AnnotatedType} to resolve.
369
             * @return The annotated type's type.
370
             */
371
            Type getType(AnnotatedElement value);
372
        }
373
    }
374

375
    /**
376
     * An iterator that iterates over a type's class hierarchy.
377
     */
378
    class SuperClassIterator implements Iterator<TypeDefinition> {
379

380
        /**
381
         * The next class to represent.
382
         */
383
        @UnknownNull
384
        private TypeDefinition nextClass;
385

386
        /**
387
         * Creates a new iterator.
388
         *
389
         * @param initialType The initial type of this iterator.
390
         */
391
        public SuperClassIterator(TypeDefinition initialType) {
1✔
392
            nextClass = initialType;
1✔
393
        }
1✔
394

395
        /**
396
         * {@inheritDoc}
397
         */
398
        public boolean hasNext() {
399
            return nextClass != null;
1✔
400
        }
401

402
        /**
403
         * {@inheritDoc}
404
         */
405
        public TypeDefinition next() {
406
            if (!hasNext()) {
1✔
407
                throw new NoSuchElementException("End of type hierarchy");
1✔
408
            }
409
            try {
410
                return nextClass;
1✔
411
            } finally {
412
                nextClass = nextClass.getSuperClass();
1✔
413
            }
414
        }
415

416
        /**
417
         * {@inheritDoc}
418
         */
419
        public void remove() {
420
            throw new UnsupportedOperationException("remove");
1✔
421
        }
422
    }
423
}
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