• 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

33.33
/byte-buddy-dep/src/main/java/net/bytebuddy/utility/JavaModule.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.ClassFileVersion;
19
import net.bytebuddy.build.AccessControllerPlugin;
20
import net.bytebuddy.description.NamedElement;
21
import net.bytebuddy.description.annotation.AnnotationList;
22
import net.bytebuddy.description.annotation.AnnotationSource;
23
import net.bytebuddy.description.module.ModuleDescription;
24
import net.bytebuddy.description.type.PackageDescription;
25
import net.bytebuddy.utility.dispatcher.JavaDispatcher;
26
import net.bytebuddy.utility.nullability.AlwaysNull;
27
import net.bytebuddy.utility.nullability.MaybeNull;
28

29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.lang.reflect.AnnotatedElement;
32
import java.security.PrivilegedAction;
33
import java.util.Set;
34

35
/**
36
 * Type-safe representation of a {@code java.lang.Module}. On platforms that do not support the module API, modules are represented by {@code null}.
37
 */
38
public class JavaModule implements NamedElement.WithOptionalName, AnnotationSource {
39

40
    /**
41
     * Canonical representation of a Java module on a JVM that does not support the module API.
42
     */
43
    @AlwaysNull
44
    public static final JavaModule UNSUPPORTED = null;
1✔
45

46
    /**
47
     * A dispatcher to resolve a {@link Class}'s {@code java.lang.Module}.
48
     */
49
    protected static final Resolver RESOLVER = doPrivileged(JavaDispatcher.of(Resolver.class));
1✔
50

51
    /**
52
     * A dispatcher to interact with {@code java.lang.Module}.
53
     */
54
    protected static final Module MODULE = doPrivileged(JavaDispatcher.of(Module.class));
1✔
55

56
    /**
57
     * The {@code java.lang.Module} instance this wrapper represents.
58
     */
59
    private final AnnotatedElement module;
60

61
    /**
62
     * Creates a new Java module representation.
63
     *
64
     * @param module The {@code java.lang.Module} instance this wrapper represents.
65
     */
66
    protected JavaModule(AnnotatedElement module) {
1✔
67
        this.module = module;
1✔
68
    }
1✔
69

70
    /**
71
     * A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
72
     *
73
     * @param action The action to execute from a privileged context.
74
     * @param <T>    The type of the action's resolved value.
75
     * @return The action's resolved value.
76
     */
77
    @AccessControllerPlugin.Enhance
78
    private static <T> T doPrivileged(PrivilegedAction<T> action) {
79
        return action.run();
×
80
    }
81

82
    /**
83
     * Returns a representation of the supplied type's {@code java.lang.Module} or {@code null} if the current VM does not support modules.
84
     *
85
     * @param type The type for which to describe the module.
86
     * @return A representation of the type's module or {@code null} if the current VM does not support modules.
87
     */
88
    @MaybeNull
89
    public static JavaModule ofType(Class<?> type) {
90
        Object module = RESOLVER.getModule(type);
1✔
91
        return module == null
1✔
92
                ? UNSUPPORTED
93
                : new JavaModule((AnnotatedElement) module);
94
    }
95

96
    /**
97
     * Represents the supplied {@code java.lang.Module} as an instance of this class and validates that the
98
     * supplied instance really represents a Java {@code Module}.
99
     *
100
     * @param module The module to represent.
101
     * @return A representation of the supplied Java module.
102
     */
103
    public static JavaModule of(Object module) {
104
        if (!MODULE.isInstance(module)) {
1✔
105
            throw new IllegalArgumentException("Not a Java module: " + module);
1✔
106
        }
107
        return new JavaModule((AnnotatedElement) module);
×
108
    }
109

110
    /**
111
     * Checks if the current VM supports the {@code java.lang.Module} API.
112
     *
113
     * @return {@code true} if the current VM supports modules.
114
     */
115
    public static boolean isSupported() {
116
        return ClassFileVersion.ofThisVm(ClassFileVersion.JAVA_V5).isAtLeast(ClassFileVersion.JAVA_V9);
1✔
117
    }
118

119
    /**
120
     * {@inheritDoc}
121
     */
122
    public boolean isNamed() {
123
        return MODULE.isNamed(module);
×
124
    }
125

126
    /**
127
     * {@inheritDoc}
128
     */
129
    public String getActualName() {
130
        return MODULE.getName(module);
×
131
    }
132

133
    /**
134
     * Returns the packages of this module.
135
     *
136
     * @return A set of the names of the packages that are defined by this module.
137
     */
138
    public Set<String> getPackages() {
139
        return MODULE.getPackages(module);
×
140
    }
141

142
    /**
143
     * Returns a resource stream for this module for a resource of the given name or {@code null} if such a resource does not exist.
144
     *
145
     * @param name The name of the resource.
146
     * @return An input stream for the resource or {@code null} if it does not exist.
147
     * @throws IOException If an I/O exception occurs.
148
     */
149
    @MaybeNull
150
    public InputStream getResourceAsStream(String name) throws IOException {
151
        return MODULE.getResourceAsStream(module, name);
×
152
    }
153

154
    /**
155
     * Returns the class loader of this module.
156
     *
157
     * @return The class loader of the represented module.
158
     */
159
    @MaybeNull
160
    public ClassLoader getClassLoader() {
161
        return MODULE.getClassLoader(module);
×
162
    }
163

164
    /**
165
     * Unwraps this instance to a {@code java.lang.Module}.
166
     *
167
     * @return The represented {@code java.lang.Module}.
168
     */
169
    public Object unwrap() {
170
        return module;
1✔
171
    }
172

173
    /**
174
     * Checks if this module can read the exported packages of the supplied module.
175
     *
176
     * @param module The module to check for its readability by this module.
177
     * @return {@code true} if this module can read the supplied module.
178
     */
179
    public boolean canRead(JavaModule module) {
180
        return MODULE.canRead(this.module, module.unwrap());
×
181
    }
182

183
    /**
184
     * Returns {@code true} if this module exports the supplied package to this module.
185
     *
186
     * @param packageDescription The package to check for
187
     * @param module             The target module.
188
     * @return {@code true} if this module exports the supplied package to this module.
189
     */
190
    public boolean isExported(@MaybeNull PackageDescription packageDescription, JavaModule module) {
191
        return packageDescription == null
×
192
                || packageDescription.isDefault()
×
193
                || MODULE.isExported(this.module, packageDescription.getName(), module.unwrap());
×
194
    }
195

196
    /**
197
     * Returns {@code true} if this module opens the supplied package to this module.
198
     *
199
     * @param packageDescription The package to check for.
200
     * @param module             The target module.
201
     * @return {@code true} if this module opens the supplied package to this module.
202
     */
203
    public boolean isOpened(@MaybeNull PackageDescription packageDescription, JavaModule module) {
204
        return packageDescription == null
×
205
                || packageDescription.isDefault()
×
206
                || MODULE.isOpen(this.module, packageDescription.getName(), module.unwrap());
×
207
    }
208

209
    /**
210
     * {@inheritDoc}
211
     */
212
    public AnnotationList getDeclaredAnnotations() {
213
        return new AnnotationList.ForLoadedAnnotations(module.getDeclaredAnnotations());
×
214
    }
215

216
    /**
217
     * Returns a description of the module if the current module is a named module. Otherwise,
218
     * an exception is thrown.
219
     *
220
     * @return A description of the current module.
221
     */
222
    public ModuleDescription toDescription() {
223
        return ModuleDescription.ForLoadedModule.of(module);
×
224
    }
225

226
    @Override
227
    public int hashCode() {
228
        return module.hashCode();
×
229
    }
230

231
    @Override
232
    public boolean equals(@MaybeNull Object other) {
233
        if (this == other) {
×
234
            return true;
×
235
        } else if (!(other instanceof JavaModule)) {
×
236
            return false;
×
237
        }
238
        JavaModule javaModule = (JavaModule) other;
×
239
        return module.equals(javaModule.module);
×
240
    }
241

242
    @Override
243
    public String toString() {
244
        return module.toString();
×
245
    }
246

247
    /**
248
     * A proxy for resolving a {@link Class}'s {@code java.lang.Module}.
249
     */
250
    @JavaDispatcher.Proxied("java.lang.Class")
251
    protected interface Resolver {
252

253
        /**
254
         * Resolves the {@code java.lang.Module} of the supplied type.
255
         *
256
         * @param type The type for which to resolve the module.
257
         * @return The type's module or {@code null} if the module system is not supported.
258
         */
259
        @MaybeNull
260
        @JavaDispatcher.Defaults
261
        Object getModule(Class<?> type);
262
    }
263

264
    /**
265
     * A proxy for interacting with {@code java.lang.Module}.
266
     */
267
    @JavaDispatcher.Proxied("java.lang.Module")
268
    protected interface Module {
269

270
        /**
271
         * Returns {@code true} if the supplied instance is of type {@code java.lang.Module}.
272
         *
273
         * @param value The instance to investigate.
274
         * @return {@code true} if the supplied value is a {@code java.lang.Module}.
275
         */
276
        @JavaDispatcher.Instance
277
        boolean isInstance(Object value);
278

279
        /**
280
         * Returns {@code true} if the supplied module is named.
281
         *
282
         * @param value The {@code java.lang.Module} to check for the existence of a name.
283
         * @return {@code true} if the supplied module is named.
284
         */
285
        boolean isNamed(Object value);
286

287
        /**
288
         * Returns the module's name.
289
         *
290
         * @param value The {@code java.lang.Module} to check for its name.
291
         * @return The module's (implicit or explicit) name.
292
         */
293
        String getName(Object value);
294

295
        /**
296
         * Returns the module's exported packages.
297
         *
298
         * @param value The {@code java.lang.Module} to check for its packages.
299
         * @return The module's packages.
300
         */
301
        Set<String> getPackages(Object value);
302

303
        /**
304
         * Returns the class loader of a module.
305
         *
306
         * @param value The {@code java.lang.Module} for which to return a class loader.
307
         * @return The module's class loader.
308
         */
309
        @MaybeNull
310
        ClassLoader getClassLoader(Object value);
311

312
        /**
313
         * Returns a resource stream for this module for a resource of the given name or {@code null} if such a resource does not exist.
314
         *
315
         * @param value The {@code java.lang.Module} instance to apply this method upon.
316
         * @param name  The name of the resource.
317
         * @return An input stream for the resource or {@code null} if it does not exist.
318
         * @throws IOException If an I/O exception occurs.
319
         */
320
        @MaybeNull
321
        InputStream getResourceAsStream(Object value, String name) throws IOException;
322

323
        /**
324
         * Returns {@code true} if the source module exports the supplied package to the target module.
325
         *
326
         * @param value    The source module.
327
         * @param aPackage The name of the package to check.
328
         * @param target   The target module.
329
         * @return {@code true} if the source module exports the supplied package to the target module.
330
         */
331
        boolean isExported(Object value, String aPackage, @JavaDispatcher.Proxied("java.lang.Module") Object target);
332

333
        /**
334
         * Returns {@code true} if the source module opens the supplied package to the target module.
335
         *
336
         * @param value    The source module.
337
         * @param aPackage The name of the package to check.
338
         * @param target   The target module.
339
         * @return {@code true} if the source module opens the supplied package to the target module.
340
         */
341
        boolean isOpen(Object value, String aPackage, @JavaDispatcher.Proxied("java.lang.Module") Object target);
342

343
        /**
344
         * Checks if the source module can read the target module.
345
         *
346
         * @param value  The source module.
347
         * @param target The target module.
348
         * @return {@code true} if the source module can read the target module.
349
         */
350
        boolean canRead(Object value, @JavaDispatcher.Proxied("java.lang.Module") Object target);
351
    }
352
}
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