88
88
public final class DynamicHub implements JavaKind .FormatWithToString , AnnotatedElement , java .lang .reflect .Type , GenericDeclaration , Serializable ,
89
89
Target_java_lang_invoke_TypeDescriptor_OfField , Target_java_lang_constant_Constable {
90
90
91
- /** Marker value for {@link #classLoader}. */
92
- static final Object NO_CLASS_LOADER = new Object ();
93
-
94
91
@ Substitute //
95
92
private static final Class <?>[] EMPTY_CLASS_ARRAY = new Class <?>[0 ];
96
93
@@ -272,11 +269,6 @@ public final class DynamicHub implements JavaKind.FormatWithToString, AnnotatedE
272
269
*/
273
270
private ClassInitializationInfo classInitializationInfo ;
274
271
275
- /**
276
- * Classloader used for loading this class during image-build time.
277
- */
278
- private final Object classLoader ;
279
-
280
272
/**
281
273
* Array containing this type's type check id information. During a type check, a requested
282
274
* column of this array is read to determine if this value fits within the range of ids which
@@ -305,7 +297,7 @@ public void setModule(Module module) {
305
297
this .module = module ;
306
298
}
307
299
308
- private final LazyFinalReference < DynamicHubCompanion > companion = new LazyFinalReference <>(() -> new DynamicHubCompanion ( this )) ;
300
+ private final DynamicHubCompanion companion ;
309
301
310
302
@ Platforms (Platform .HOSTED_ONLY .class )
311
303
public DynamicHub (Class <?> hostedJavaClass , String name , HubType hubType , ReferenceType referenceType , Object isLocalClass , Object isAnonymousClass , DynamicHub superType , DynamicHub componentHub ,
@@ -321,7 +313,6 @@ public DynamicHub(Class<?> hostedJavaClass, String name, HubType hubType, Refere
321
313
this .componentType = componentHub ;
322
314
this .sourceFileName = sourceFileName ;
323
315
this .modifiers = modifiers ;
324
- this .classLoader = PredefinedClassesSupport .isPredefined (hostedJavaClass ) ? NO_CLASS_LOADER : classLoader ;
325
316
this .nestHost = nestHost ;
326
317
327
318
this .flags = NumUtil .safeToUByte (makeFlag (IS_PRIMITIVE_FLAG_BIT , hostedJavaClass .isPrimitive ()) |
@@ -332,6 +323,8 @@ public DynamicHub(Class<?> hostedJavaClass, String name, HubType hubType, Refere
332
323
makeFlag (HAS_DEFAULT_METHODS_FLAG_BIT , hasDefaultMethods ) |
333
324
makeFlag (DECLARES_DEFAULT_METHODS_FLAG_BIT , declaresDefaultMethods ) |
334
325
makeFlag (IS_SEALED_FLAG_BIT , isSealed ));
326
+
327
+ this .companion = new DynamicHubCompanion (hostedJavaClass , classLoader );
335
328
}
336
329
337
330
@ Platforms (Platform .HOSTED_ONLY .class )
@@ -560,6 +553,10 @@ public static DynamicHub fromClass(Class<?> clazz) {
560
553
return SubstrateUtil .cast (clazz , DynamicHub .class );
561
554
}
562
555
556
+ public DynamicHubCompanion getCompanion () {
557
+ return companion ;
558
+ }
559
+
563
560
/*
564
561
* Note that this method must be a static method and not an instance method, otherwise null
565
562
* values cannot be converted.
@@ -696,18 +693,15 @@ public InputStream getResourceAsStream(String resourceName) {
696
693
697
694
@ Substitute
698
695
private ClassLoader getClassLoader0 () {
699
- if (classLoader == NO_CLASS_LOADER ) {
700
- return companion .get ().getClassLoader ();
701
- }
702
- return (ClassLoader ) classLoader ;
696
+ return companion .getClassLoader ();
703
697
}
704
698
705
699
public boolean isLoaded () {
706
- return classLoader != NO_CLASS_LOADER || ( companion .isPresent () && companion . get (). hasClassLoader () );
700
+ return companion .hasClassLoader ();
707
701
}
708
702
709
703
void setClassLoaderAtRuntime (ClassLoader loader ) {
710
- companion .get (). setClassLoader (loader );
704
+ companion .setClassLoader (loader );
711
705
}
712
706
713
707
@ Substitute
@@ -1066,7 +1060,7 @@ private Method getMethod(@SuppressWarnings("hiding") String name, Class<?>... pa
1066
1060
* The original code of getMethods() does a recursive search to avoid creating objects for
1067
1061
* all public methods. We prepare them during the image build and can just iterate here.
1068
1062
*/
1069
- Method method = searchMethods (companion .get (). getCompleteReflectionData ().publicMethods , name , parameterTypes );
1063
+ Method method = searchMethods (companion .getCompleteReflectionData (this ).publicMethods , name , parameterTypes );
1070
1064
if (method == null ) {
1071
1065
throw new NoSuchMethodException (describeMethod (getName () + "." + name + "(" , parameterTypes , ")" ));
1072
1066
}
@@ -1112,7 +1106,8 @@ private Class<?>[] getClasses() {
1112
1106
1113
1107
@ Substitute
1114
1108
private Constructor <?>[] privateGetDeclaredConstructors (boolean publicOnly ) {
1115
- return publicOnly ? companion .get ().getCompleteReflectionData ().publicConstructors : companion .get ().getCompleteReflectionData ().declaredConstructors ;
1109
+ ReflectionData reflectionData = companion .getCompleteReflectionData (this );
1110
+ return publicOnly ? reflectionData .publicConstructors : reflectionData .declaredConstructors ;
1116
1111
}
1117
1112
1118
1113
@ Substitute
@@ -1122,7 +1117,8 @@ private Field[] privateGetDeclaredFields(boolean publicOnly) {
1122
1117
1123
1118
@ Substitute
1124
1119
private Method [] privateGetDeclaredMethods (boolean publicOnly ) {
1125
- return publicOnly ? companion .get ().getCompleteReflectionData ().declaredPublicMethods : companion .get ().getCompleteReflectionData ().declaredMethods ;
1120
+ ReflectionData reflectionData = companion .getCompleteReflectionData (this );
1121
+ return publicOnly ? reflectionData .declaredPublicMethods : reflectionData .declaredMethods ;
1126
1122
}
1127
1123
1128
1124
@ Substitute
@@ -1132,7 +1128,7 @@ private Field[] privateGetPublicFields() {
1132
1128
1133
1129
@ Substitute
1134
1130
Method [] privateGetPublicMethods () {
1135
- return companion .get (). getCompleteReflectionData ().publicMethods ;
1131
+ return companion .getCompleteReflectionData (this ).publicMethods ;
1136
1132
}
1137
1133
1138
1134
@ KeepOriginal
@@ -1292,7 +1288,7 @@ public String getPackageName() {
1292
1288
if (SubstrateUtil .HOSTED ) { // avoid eager initialization in image heap
1293
1289
return computePackageName ();
1294
1290
}
1295
- return companion .get (). getPackageName ();
1291
+ return companion .getPackageName (this );
1296
1292
}
1297
1293
1298
1294
String computePackageName () {
@@ -1330,7 +1326,7 @@ public Object[] getSigners() {
1330
1326
1331
1327
@ Substitute
1332
1328
public ProtectionDomain getProtectionDomain () {
1333
- return companion .get (). getProtectionDomain ();
1329
+ return companion .getProtectionDomain ();
1334
1330
}
1335
1331
1336
1332
@ TargetElement (onlyWith = JDK17OrLater .class )
@@ -1340,7 +1336,7 @@ private ProtectionDomain protectionDomain() {
1340
1336
}
1341
1337
1342
1338
void setProtectionDomainAtRuntime (ProtectionDomain protectionDomain ) {
1343
- companion .get (). setProtectionDomain (protectionDomain );
1339
+ companion .setProtectionDomain (protectionDomain );
1344
1340
}
1345
1341
1346
1342
@ Substitute
0 commit comments