• 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

90.77
/byte-buddy-dep/src/main/java/net/bytebuddy/utility/JavaType.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.utility;
17

18
import net.bytebuddy.build.CachedReturnPlugin;
19
import net.bytebuddy.description.type.TypeDefinition;
20
import net.bytebuddy.description.type.TypeDescription;
21
import net.bytebuddy.description.type.TypeList;
22
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
23
import net.bytebuddy.utility.nullability.MaybeNull;
24
import org.objectweb.asm.Opcodes;
25

26
import java.io.Serializable;
27
import java.lang.reflect.AccessibleObject;
28
import java.lang.reflect.AnnotatedElement;
29
import java.lang.reflect.GenericDeclaration;
30
import java.lang.reflect.Member;
31
import java.lang.reflect.Type;
32
import java.util.List;
33

34
/**
35
 * Representations of Java types that do not exist in Java 6 but that have a special meaning to the JVM.
36
 */
37
public enum JavaType {
1✔
38

39
    /**
40
     * The Java 12 {@code java.lang.constant.Constable} type.
41
     */
42
    CONSTABLE("java.lang.constant.Constable", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, TypeDescription.UNDEFINED),
1✔
43

44
    /**
45
     * The Java 12 {@code java.lang.invoke.TypeDescriptor} type.
46
     */
47
    TYPE_DESCRIPTOR("java.lang.invoke.TypeDescriptor", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, TypeDescription.UNDEFINED),
1✔
48

49
    /**
50
     * The Java 12 {@code java.lang.invoke.TypeDescriptor$OfMethod} type.
51
     */
52
    TYPE_DESCRIPTOR_OF_FIELD("java.lang.invoke.TypeDescriptor$OfField",
1✔
53
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
54
            TypeDescription.UNDEFINED,
55
            TYPE_DESCRIPTOR.getTypeStub()),
1✔
56

57
    /**
58
     * The Java 12 {@code java.lang.invoke.TypeDescriptor$OfMethod} type.
59
     */
60
    TYPE_DESCRIPTOR_OF_METHOD("java.lang.invoke.TypeDescriptor$OfMethod",
1✔
61
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
62
            TypeDescription.UNDEFINED,
63
            TYPE_DESCRIPTOR.getTypeStub()),
1✔
64

65
    /**
66
     * The Java 12 {@code java.lang.constant.ConstableDesc} type.
67
     */
68
    CONSTANT_DESCRIPTION("java.lang.constant.ConstantDesc", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, TypeDescription.UNDEFINED),
1✔
69

70
    /**
71
     * The Java 12 {@code java.lang.constant.DynamicConstantDesc} type.
72
     */
73
    DYNAMIC_CONSTANT_DESCRIPTION("java.lang.constant.DynamicConstantDesc",
1✔
74
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT,
75
            TypeDescription.ForLoadedType.of(Object.class),
1✔
76
            CONSTANT_DESCRIPTION.getTypeStub()),
1✔
77

78
    /**
79
     * The Java 12 {@code java.lang.constant.ClassDesc} type.
80
     */
81
    CLASS_DESCRIPTION("java.lang.constant.ClassDesc",
1✔
82
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
83
            TypeDescription.UNDEFINED,
84
            CONSTANT_DESCRIPTION.getTypeStub(),
1✔
85
            TYPE_DESCRIPTOR_OF_FIELD.getTypeStub()),
1✔
86

87
    /**
88
     * The Java 12 {@code java.lang.constant.MethodTypeDesc} type.
89
     */
90
    METHOD_TYPE_DESCRIPTION("java.lang.constant.MethodTypeDesc",
1✔
91
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
92
            TypeDescription.UNDEFINED,
93
            CONSTANT_DESCRIPTION.getTypeStub(),
1✔
94
            TYPE_DESCRIPTOR_OF_METHOD.getTypeStub()),
1✔
95

96
    /**
97
     * The Java 12 {@code java.lang.constant.MethodHandleDesc} type.
98
     */
99
    METHOD_HANDLE_DESCRIPTION("java.lang.constant.MethodHandleDesc",
1✔
100
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
101
            TypeDescription.UNDEFINED,
102
            CONSTANT_DESCRIPTION.getTypeStub()),
1✔
103

104
    /**
105
     * The Java 12 {@code java.lang.constant.DirectMethodHandleDesc} type.
106
     */
107
    DIRECT_METHOD_HANDLE_DESCRIPTION("java.lang.constant.DirectMethodHandleDesc",
1✔
108
            Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE,
109
            TypeDescription.UNDEFINED,
110
            METHOD_HANDLE_DESCRIPTION.getTypeStub()),
1✔
111

112
    /**
113
     * The Java 7 {@code java.lang.invoke.MethodHandle} type.
114
     */
115
    METHOD_HANDLE("java.lang.invoke.MethodHandle", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, TypeDescription.ForLoadedType.of(Object.class), CONSTABLE.getTypeStub()),
1✔
116

117
    /**
118
     * The Java 7 {@code java.lang.invoke.MethodHandles} type.
119
     */
120
    METHOD_HANDLES("java.lang.invoke.MethodHandles", Opcodes.ACC_PUBLIC, Object.class),
1✔
121

122
    /**
123
     * The Java 7 {@code java.lang.invoke.MethodType} type.
124
     */
125
    METHOD_TYPE("java.lang.invoke.MethodType",
1✔
126
            Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL,
127
            TypeDescription.ForLoadedType.of(Object.class),
1✔
128
            CONSTABLE.getTypeStub(),
1✔
129
            TYPE_DESCRIPTOR_OF_METHOD.getTypeStub(),
1✔
130
            TypeDescription.ForLoadedType.of(Serializable.class)),
1✔
131

132
    /**
133
     * The Java 7 {@code java.lang.invoke.MethodTypes.Lookup} type.
134
     */
135
    METHOD_HANDLES_LOOKUP("java.lang.invoke.MethodHandles$Lookup", Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, Object.class),
1✔
136

137
    /**
138
     * The Java 7 {@code java.lang.invoke.CallSite} type.
139
     */
140
    CALL_SITE("java.lang.invoke.CallSite", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, Object.class),
1✔
141

142
    /**
143
     * The Java 9 {@code java.lang.invoke.VarHandle} type.
144
     */
145
    VAR_HANDLE("java.lang.invoke.VarHandle", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, TypeDescription.Generic.OfNonGenericType.ForLoadedType.of(Object.class), CONSTABLE.getTypeStub()),
1✔
146

147
    /**
148
     * The Java 8 {@code java.lang.reflect.Parameter} type.
149
     */
150
    PARAMETER("java.lang.reflect.Parameter", Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, Object.class, AnnotatedElement.class),
1✔
151

152
    /**
153
     * The Java 7 {@code java.lang.reflect.Executable} type.
154
     */
155
    EXECUTABLE("java.lang.reflect.Executable", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, AccessibleObject.class, Member.class, GenericDeclaration.class),
1✔
156

157
    /**
158
     * The Java 9 {@code java.lang.Module} type.
159
     */
160
    MODULE("java.lang.Module", Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, Object.class, AnnotatedElement.class),
1✔
161

162
    /**
163
     * The Java 12 {@code java.lang.invoke.ConstantBootstraps} type.
164
     */
165
    CONSTANT_BOOTSTRAPS("java.lang.invoke.ConstantBootstraps", Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, Object.class),
1✔
166

167
    /**
168
     * The Java 14 {@code java.lang.Record} type.
169
     */
170
    RECORD("java.lang.Record", Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, Object.class),
1✔
171

172
    /**
173
     * The Java 14 {@code java.lang.runtime.ObjectMethods} type.
174
     */
175
    OBJECT_METHODS("java.lang.runtime.ObjectMethods", Opcodes.ACC_PUBLIC, Object.class),
1✔
176

177
    /**
178
     * The {@code java.security.AccessControlContext} type which is deprecated for removal beginning in Java 17.
179
     */
180
    ACCESS_CONTROL_CONTEXT("java.security.AccessControlContext", Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, TypeDescription.UNDEFINED);
1✔
181

182
    /**
183
     * The type description to represent this type which is either a loaded type or a stub.
184
     */
185
    private final TypeDescription typeDescription;
186

187
    /**
188
     * Creates a new java type representation.
189
     *
190
     * @param typeName    The binary name of this type.
191
     * @param modifiers   The modifiers of this type when creating a stub.
192
     * @param superClass  The super class of this type when creating a stub or {@code null} if no super class is defined.
193
     * @param anInterface The interfaces of this type when creating a stub.
194
     */
195
    JavaType(String typeName, int modifiers, @MaybeNull Type superClass, Type... anInterface) {
196
        this(typeName, modifiers, superClass == null
1✔
197
                ? TypeDescription.Generic.UNDEFINED
198
                : TypeDescription.Generic.Sort.describe(superClass), new TypeList.Generic.ForLoadedTypes(anInterface));
1✔
199
    }
1✔
200

201
    /**
202
     * Creates a new java type representation.
203
     *
204
     * @param typeName    The binary name of this type.
205
     * @param modifiers   The modifiers of this type when creating a stub.
206
     * @param superClass  The super class of this type when creating a stub or {@code null} if no super class is defined.
207
     * @param anInterface The interfaces of this type when creating a stub.
208
     */
209
    JavaType(String typeName, int modifiers, @MaybeNull TypeDefinition superClass, TypeDefinition... anInterface) {
210
        this(typeName, modifiers, superClass == null
1✔
211
                ? TypeDescription.Generic.UNDEFINED
212
                : superClass.asGenericType(), new TypeList.Generic.Explicit(anInterface));
1✔
213
    }
1✔
214

215
    /**
216
     * Creates a new java type representation.
217
     *
218
     * @param typeName   The binary name of this type.
219
     * @param modifiers  The modifiers of this type when creating a stub.
220
     * @param superClass The super class of this type when creating a stub or {@code null} if no super class is defined.
221
     * @param interfaces The interfaces of this type when creating a stub.
222
     */
223
    JavaType(String typeName, int modifiers, @MaybeNull TypeDescription.Generic superClass, TypeList.Generic interfaces) {
1✔
224
        typeDescription = new LatentTypeWithSimpleName(typeName, modifiers, superClass, interfaces);
1✔
225
    }
1✔
226

227
    /**
228
     * Returns at least a stub representing this type where the stub does not define any methods or fields. If a type exists for
229
     * the current runtime, a loaded type representation is returned.
230
     *
231
     * @return A type description for this Java type.
232
     */
233
    public TypeDescription getTypeStub() {
234
        return typeDescription;
1✔
235
    }
236

237
    /**
238
     * Loads the class that is represented by this Java type.
239
     *
240
     * @return A loaded type of this Java type.
241
     * @throws ClassNotFoundException If the represented type cannot be loaded.
242
     */
243
    @CachedReturnPlugin.Enhance("loaded")
244
    public Class<?> load() throws ClassNotFoundException {
245
        return Class.forName(typeDescription.getName(), false, ClassLoadingStrategy.BOOTSTRAP_LOADER);
1✔
246
    }
247

248
    /**
249
     * Loads the class that is represented by this Java type and represents it as a {@link TypeDescription}.
250
     *
251
     * @return A loaded type of this Java type.
252
     * @throws ClassNotFoundException If the represented type cannot be loaded.
253
     */
254
    public TypeDescription loadAsDescription() throws ClassNotFoundException {
255
        return TypeDescription.ForLoadedType.of(load());
1✔
256
    }
257

258
    /**
259
     * Returns {@code true} if this type is available on the current JVM.
260
     *
261
     * @return {@code true} if this type is available on the current JVM.
262
     */
263
    public boolean isAvailable() {
264
        return doIsAvailable();
1✔
265
    }
266

267
    /**
268
     * Returns {@code true} if this type is available on the current VM. By boxing the result,
269
     * the result can be cached efficiently what is not possible when using a primitive type.
270
     *
271
     * @return {@code true} if this type is available on the current JVM.
272
     */
273
    @CachedReturnPlugin.Enhance("available")
274
    private Boolean doIsAvailable() {
275
        try {
276
            load();
1✔
277
            return true;
1✔
278
        } catch (ClassNotFoundException ignored) {
1✔
279
            return false;
1✔
280
        }
281
    }
282

283
    /**
284
     * Checks if the supplied object is an instance of this type.
285
     *
286
     * @param instance The instance to check.
287
     * @return {@code true} if the supplied object is an instance of this type.
288
     */
289
    public boolean isInstance(Object instance) {
290
        if (!isAvailable()) {
1✔
291
            return false;
×
292
        }
293
        try {
294
            return load().isInstance(instance);
1✔
295
        } catch (ClassNotFoundException ignored) {
×
296
            return false;
×
297
        }
298
    }
299

300
    /**
301
     * A latent type that resolves the simple name without considering the declaring type which is not normally available. This
302
     * is required since the {@link JavaConstant} replication of Java's string representation uses the simple name.
303
     */
304
    protected static class LatentTypeWithSimpleName extends TypeDescription.Latent {
305

306
        /**
307
         * Creates a new latent type with a simple name.
308
         *
309
         * @param name       The name of the type.
310
         * @param modifiers  The modifiers of the type.
311
         * @param superClass The super type or {@code null} if no such type exists.
312
         * @param interfaces The interfaces that this type implements.
313
         */
314
        protected LatentTypeWithSimpleName(String name, int modifiers, @MaybeNull Generic superClass, List<? extends Generic> interfaces) {
315
            super(name, modifiers, superClass, interfaces);
1✔
316
        }
1✔
317

318
        @Override
319
        public String getSimpleName() {
320
            String name = getName();
×
321
            int index = Math.max(name.lastIndexOf('$'), name.lastIndexOf('.'));
×
322
            return index == -1 ? name : name.substring(index + 1);
×
323
        }
324
    }
325
}
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