38
38
import java .nio .file .Path ;
39
39
import java .security .ProtectionDomain ;
40
40
import java .util .ArrayList ;
41
+ import java .util .Collections ;
41
42
import java .util .Enumeration ;
42
43
import java .util .HashMap ;
43
44
import java .util .List ;
44
45
import java .util .Map ;
45
- import java .util .NoSuchElementException ;
46
46
import java .util .Objects ;
47
47
import java .util .Set ;
48
- import java .util .function .Consumer ;
49
48
50
49
import org .graalvm .nativeimage .Platform ;
51
50
import org .graalvm .nativeimage .Platforms ;
59
58
/**
60
59
* A classloader, that reads class files and resources from a jimage file at image build time.
61
60
*/
61
+ @ SuppressWarnings ("unused" )
62
62
@ Platforms (Platform .HOSTED_ONLY .class )
63
- final class HostedLibGraalClassLoader extends ClassLoader {
63
+ final class HostedLibGraalClassLoader extends ClassLoader implements LibGraalClassLoaderBase {
64
64
65
65
private static final String JAVA_HOME_PROPERTY_KEY = "jdk.graal.internal.libgraal.javahome" ;
66
66
private static final String JAVA_HOME_PROPERTY_VALUE = System .getProperty (JAVA_HOME_PROPERTY_KEY , System .getProperty ("java.home" ));
@@ -79,7 +79,7 @@ final class HostedLibGraalClassLoader extends ClassLoader {
79
79
* Map from the {@linkplain Class#forName(String) name} of a class to the image path of its
80
80
* class file.
81
81
*/
82
- private final Map <String , String > classes = new HashMap <>() ;
82
+ private final Map <String , String > classes ;
83
83
84
84
/**
85
85
* Map from a service name to a list of providers.
@@ -113,7 +113,6 @@ public HostedLibGraalClassLoader() {
113
113
super (LibGraalClassLoader .LOADER_NAME , Feature .class .getClassLoader ());
114
114
libGraalJavaHome = Path .of (JAVA_HOME_PROPERTY_VALUE );
115
115
116
- Map <String , String > modulesMap = new HashMap <>();
117
116
try {
118
117
/*
119
118
* Access to jdk.internal.jimage classes is needed by this Classloader implementation.
@@ -129,6 +128,9 @@ public HostedLibGraalClassLoader() {
129
128
Modules .addExports (javaBaseModule , "jdk.internal.vm" , unnamedModuleOfThisLoader );
130
129
Modules .addExports (javaBaseModule , "jdk.internal.misc" , unnamedModuleOfThisLoader );
131
130
131
+ Map <String , String > modulesMap = new HashMap <>();
132
+ Map <String , String > classesMap = new HashMap <>();
133
+
132
134
Path imagePath = libGraalJavaHome .resolve (Path .of ("lib" , "modules" ));
133
135
this .imageReader = BasicImageReader .open (imagePath );
134
136
for (var entry : imageReader .getEntryNames ()) {
@@ -140,50 +142,38 @@ public HostedLibGraalClassLoader() {
140
142
resources .put (resource , entry );
141
143
if (resource .endsWith (".class" )) {
142
144
String className = resource .substring (0 , resource .length () - ".class" .length ()).replace ('/' , '.' );
143
- classes .put (className , entry );
144
145
if (resource .equals ("module-info.class" )) {
145
146
ModuleDescriptor md = ModuleDescriptor .read (imageReader .getResourceBuffer (imageReader .findLocation (entry )));
146
147
for (var p : md .provides ()) {
147
148
services .computeIfAbsent (p .service (), k -> new ArrayList <>()).addAll (p .providers ());
148
149
}
149
150
} else {
151
+ classesMap .put (className , entry );
150
152
modulesMap .put (className , module );
151
153
}
152
154
}
153
155
}
154
156
}
155
157
}
156
158
159
+ modules = Map .copyOf (modulesMap );
160
+ classes = Map .copyOf (classesMap );
161
+
157
162
} catch (IOException e ) {
158
- throw new RuntimeException (e );
163
+ throw GraalError . shouldNotReachHere (e );
159
164
}
160
- this .modules = Map .copyOf (modulesMap );
161
165
}
162
166
163
- /**
164
- * Gets an unmodifiable map from the {@linkplain Class#forName(String) name} of a class to the
165
- * name of its enclosing module. Reflectively accessed by
166
- * {@code LibGraalFeature.OptionCollector#afterAnalysis(AfterAnalysisAccess)}.
167
- */
168
- @ SuppressWarnings ("unused" )
167
+ @ Override
169
168
public Map <String , String > getModules () {
170
169
return modules ;
171
170
}
172
171
173
172
/* Allow image builder to perform registration action on each class this loader provides. */
174
- @ SuppressWarnings ("unused" )
175
- public void forEachClass (Consumer <Class <?>> action ) {
176
- for (String className : classes .keySet ()) {
177
- if (className .equals ("module-info" )) {
178
- continue ;
179
- }
180
- try {
181
- var clazz = loadClass (className );
182
- action .accept (clazz );
183
- } catch (ClassNotFoundException e ) {
184
- throw GraalError .shouldNotReachHere (e , LibGraalClassLoader .LOADER_NAME + " could not load class " + className );
185
- }
186
- }
173
+
174
+ @ Override
175
+ public Set <String > getAllClassNames () {
176
+ return classes .keySet ();
187
177
}
188
178
189
179
@ Override
@@ -262,24 +252,11 @@ protected URL findResource(String name) {
262
252
263
253
@ Override
264
254
protected Enumeration <URL > findResources (String name ) throws IOException {
265
- return new Enumeration <>() {
266
- private URL next = findResource (name );
267
-
268
- @ Override
269
- public boolean hasMoreElements () {
270
- return (next != null );
271
- }
272
-
273
- @ Override
274
- public URL nextElement () {
275
- if (next == null ) {
276
- throw new NoSuchElementException ();
277
- }
278
- URL u = next ;
279
- next = null ;
280
- return u ;
281
- }
282
- };
255
+ URL resource = findResource (name );
256
+ if (resource == null ) {
257
+ return Collections .emptyEnumeration ();
258
+ }
259
+ return Collections .enumeration (List .of (resource ));
283
260
}
284
261
285
262
/**
@@ -340,12 +317,13 @@ public String getContentType() {
340
317
}
341
318
}
342
319
343
- /**
344
- * @return instance of ClassLoader that should be seen at image-runtime if a class was loaded at
345
- * image-buildtime by this classloader.
346
- */
347
- @ SuppressWarnings ("unused" )
348
- public static ClassLoader getRuntimeClassLoader () {
320
+ @ Override
321
+ public HostedLibGraalClassLoader getClassLoader () {
322
+ return this ;
323
+ }
324
+
325
+ @ Override
326
+ public LibGraalClassLoader getRuntimeClassLoader () {
349
327
return LibGraalClassLoader .singleton ;
350
328
}
351
329
}
0 commit comments