-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
ClassLoaderHandlerRegistry.java
243 lines (221 loc) · 11.1 KB
/
ClassLoaderHandlerRegistry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* This file is part of ClassGraph.
*
* Author: Luke Hutchison
*
* Hosted at: https://github.com/classgraph/classgraph
*
* --
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Luke Hutchison
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package nonapi.io.github.classgraph.classloaderhandler;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import nonapi.io.github.classgraph.classpath.ClassLoaderOrder;
import nonapi.io.github.classgraph.classpath.ClasspathOrder;
import nonapi.io.github.classgraph.scanspec.ScanSpec;
import nonapi.io.github.classgraph.utils.LogNode;
/** The registry for ClassLoaderHandler classes. */
public class ClassLoaderHandlerRegistry {
/**
* Default ClassLoaderHandlers. If a ClassLoaderHandler is added to ClassGraph, it should be added to this list.
*/
@SuppressWarnings("null")
public static final List<ClassLoaderHandlerRegistryEntry> CLASS_LOADER_HANDLERS = //
Collections.unmodifiableList(Arrays.asList(
// ClassLoaderHandlers for other ClassLoaders that are handled by ClassGraph
new ClassLoaderHandlerRegistryEntry(AntClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(EquinoxClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(EquinoxContextFinderClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(FelixClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(JBossClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(WeblogicClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(WebsphereLibertyClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(WebsphereTraditionalClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(OSGiDefaultClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(SpringBootRestartClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(TomcatWebappClassLoaderBaseHandler.class),
new ClassLoaderHandlerRegistryEntry(CxfContainerClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(PlexusClassWorldsClassRealmClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(QuarkusClassLoaderHandler.class),
new ClassLoaderHandlerRegistryEntry(UnoOneJarClassLoaderHandler.class),
// For unit testing of PARENT_LAST delegation order
new ClassLoaderHandlerRegistryEntry(ParentLastDelegationOrderTestClassLoaderHandler.class),
// JPMS support (this handler does nothing, since modules are handled separately)
new ClassLoaderHandlerRegistryEntry(JPMSClassLoaderHandler.class),
// Java 7/8 URLClassLoader support (should be second-to-last, so that subclasses of
// URLClassLoader are handled by more specific handlers above)
new ClassLoaderHandlerRegistryEntry(URLClassLoaderHandler.class),
// Placeholder for delegation to a ClassGraphClassLoader instance from an outer nested scan
new ClassLoaderHandlerRegistryEntry(ClassGraphClassLoaderHandler.class)
// FallbackClassLoaderHandler.class is registered separately below
));
/** Fallback ClassLoaderHandler. */
public static final ClassLoaderHandlerRegistryEntry FALLBACK_HANDLER = new ClassLoaderHandlerRegistryEntry(
FallbackClassLoaderHandler.class);
// -------------------------------------------------------------------------------------------------------------
/**
* Lib dirs whose jars should be added to the classpath automatically (to compensate for some classloaders not
* explicitly listing these jars as classpath elements).
*/
public static final String[] AUTOMATIC_LIB_DIR_PREFIXES = {
// Spring-Boot
// https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/html/appendix-executable-jar-format.html
"BOOT-INF/lib/",
// Tomcat
"WEB-INF/lib/", "WEB-INF/lib-provided/",
// OSGi
"META-INF/lib/",
// Tomcat and others
"lib/",
// Extension dir
"lib/ext/",
// UnoJar and One-Jar
"main/" //
};
/**
* Automatic classfile prefixes (to compensate for some classloaders not explicitly listing these prefixes as
* part of the classpath element URL or path).
*/
public static final String[] AUTOMATIC_PACKAGE_ROOT_PREFIXES = {
// Ant, Tomcat and others
"classes/",
// Ant
"test-classes/",
// Spring-Boot
"BOOT-INF/classes/",
// Tomcat
"WEB-INF/classes/", };
// -------------------------------------------------------------------------------------------------------------
/**
* Constructor.
*/
private ClassLoaderHandlerRegistry() {
// Cannot be constructed
}
/**
* A list of fully-qualified ClassLoader class names paired with the ClassLoaderHandler that can handle them.
*/
public static class ClassLoaderHandlerRegistryEntry {
/** canHandle method. */
private final Method canHandleMethod;
/** findClassLoaderOrder method. */
private final Method findClassLoaderOrderMethod;
/** findClasspathOrder method. */
private final Method findClasspathOrderMethod;
/** The ClassLoaderHandler class. */
public final Class<? extends ClassLoaderHandler> classLoaderHandlerClass;
/**
* Constructor.
*
* @param classLoaderHandlerClass
* The ClassLoaderHandler class.
*/
private ClassLoaderHandlerRegistryEntry(final Class<? extends ClassLoaderHandler> classLoaderHandlerClass) {
// TODO: replace these with MethodHandles for speed
// TODO: (although MethodHandles are disabled for now, due to Animal Sniffer bug):
// https://github.com/mojohaus/animal-sniffer/issues/67
this.classLoaderHandlerClass = classLoaderHandlerClass;
try {
canHandleMethod = classLoaderHandlerClass.getDeclaredMethod("canHandle", Class.class,
LogNode.class);
} catch (final Exception e) {
throw new RuntimeException(
"Could not find canHandle method for " + classLoaderHandlerClass.getName(), e);
}
try {
findClassLoaderOrderMethod = classLoaderHandlerClass.getDeclaredMethod("findClassLoaderOrder",
ClassLoader.class, ClassLoaderOrder.class, LogNode.class);
} catch (final Exception e) {
throw new RuntimeException(
"Could not find findClassLoaderOrder method for " + classLoaderHandlerClass.getName(), e);
}
try {
findClasspathOrderMethod = classLoaderHandlerClass.getDeclaredMethod("findClasspathOrder",
ClassLoader.class, ClasspathOrder.class, ScanSpec.class, LogNode.class);
} catch (final Exception e) {
throw new RuntimeException(
"Could not find findClasspathOrder method for " + classLoaderHandlerClass.getName(), e);
}
}
/**
* Call the static method canHandle(ClassLoader) for the associated {@link ClassLoaderHandler}.
*
* @param classLoader
* the {@link ClassLoader}.
* @param log
* the log.
* @return true, if this {@link ClassLoaderHandler} can handle the {@link ClassLoader}.
*/
public boolean canHandle(final Class<?> classLoader, final LogNode log) {
try {
return (boolean) canHandleMethod.invoke(null, classLoader, log);
} catch (final Throwable e) {
throw new RuntimeException(
"Exception while calling canHandle for " + classLoaderHandlerClass.getName(), e);
}
}
/**
* Call the static method findClassLoaderOrder(ClassLoader, ClassLoaderOrder) for the associated
* {@link ClassLoaderHandler}.
*
* @param classLoader
* the {@link ClassLoader}.
* @param classLoaderOrder
* a {@link ClassLoaderOrder} object.
* @param log
* the log
*/
public void findClassLoaderOrder(final ClassLoader classLoader, final ClassLoaderOrder classLoaderOrder,
final LogNode log) {
try {
findClassLoaderOrderMethod.invoke(null, classLoader, classLoaderOrder, log);
} catch (final Throwable e) {
throw new RuntimeException(
"Exception while calling findClassLoaderOrder for " + classLoaderHandlerClass.getName(), e);
}
}
/**
* Call the static method findClasspathOrder(ClassLoader, ClasspathOrder) for the associated
* {@link ClassLoaderHandler}.
*
* @param classLoader
* the {@link ClassLoader}.
* @param classpathOrder
* a {@link ClasspathOrder} object.
* @param scanSpec
* the {@link ScanSpec}.
* @param log
* the log.
*/
public void findClasspathOrder(final ClassLoader classLoader, final ClasspathOrder classpathOrder,
final ScanSpec scanSpec, final LogNode log) {
try {
findClasspathOrderMethod.invoke(null, classLoader, classpathOrder, scanSpec, log);
} catch (final Throwable e) {
throw new RuntimeException(
"Exception while calling findClassLoaderOrder for " + classLoaderHandlerClass.getName(), e);
}
}
}
}