Skip to content

Commit 5944fbc

Browse files
committed
move reflection metadata-related inner classes from DynamicHub to top-level
1 parent b1ad801 commit 5944fbc

File tree

9 files changed

+587
-436
lines changed

9 files changed

+587
-436
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 7 additions & 425 deletions
Large diffs are not rendered by default.

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHubCompanion.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ public final class DynamicHubCompanion {
129129
* Metadata for querying the reflection data. When using layered images this field is always
130130
* null and should not be queried. Instead, use {@link LayeredReflectionMetadataSingleton}.
131131
*/
132-
@UnknownObjectField(canBeNull = true, types = DynamicHub.ImageReflectionMetadata.class, availability = BuildPhaseProvider.AfterCompilation.class) //
132+
@UnknownObjectField(canBeNull = true, types = ImageReflectionMetadata.class, availability = BuildPhaseProvider.AfterCompilation.class) //
133133
@Stable ReflectionMetadata reflectionMetadata;
134134

135-
@UnknownObjectField(canBeNull = true, types = DynamicHub.ImageDynamicHubMetadata.class, availability = BuildPhaseProvider.AfterCompilation.class) //
135+
@UnknownObjectField(canBeNull = true, types = ImageDynamicHubMetadata.class, availability = BuildPhaseProvider.AfterCompilation.class) //
136136
@Stable DynamicHubMetadata hubMetadata;
137137

138138
/**
@@ -182,11 +182,11 @@ private DynamicHubCompanion(Module module, DynamicHub superHub, String sourceFil
182182
this.classLoader = classLoader;
183183
}
184184

185-
public void setHubMetadata(DynamicHub.RuntimeDynamicHubMetadata hubMetadata) {
185+
public void setHubMetadata(RuntimeDynamicHubMetadata hubMetadata) {
186186
this.hubMetadata = hubMetadata;
187187
}
188188

189-
public void setReflectionMetadata(DynamicHub.RuntimeReflectionMetadata reflectionMetadata) {
189+
public void setReflectionMetadata(RuntimeReflectionMetadata reflectionMetadata) {
190190
this.reflectionMetadata = reflectionMetadata;
191191
}
192192
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.hub;
26+
27+
import com.oracle.svm.core.BuildPhaseProvider;
28+
import com.oracle.svm.core.heap.UnknownPrimitiveField;
29+
import com.oracle.svm.core.reflect.RuntimeMetadataDecoder;
30+
import org.graalvm.nativeimage.ImageSingletons;
31+
32+
import static com.oracle.svm.core.reflect.RuntimeMetadataDecoder.NO_DATA;
33+
34+
final class ImageDynamicHubMetadata implements DynamicHubMetadata {
35+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class) //
36+
final int enclosingMethodInfoIndex;
37+
38+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
39+
final int annotationsIndex;
40+
41+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
42+
final int typeAnnotationsIndex;
43+
44+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
45+
final int classesEncodingIndex;
46+
47+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
48+
final int permittedSubclassesEncodingIndex;
49+
50+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
51+
final int nestMembersEncodingIndex;
52+
53+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
54+
final int signersEncodingIndex;
55+
56+
ImageDynamicHubMetadata(int enclosingMethodInfoIndex, int annotationsIndex, int typeAnnotationsIndex, int classesEncodingIndex, int permittedSubclassesEncodingIndex,
57+
int nestMembersEncodingIndex, int signersEncodingIndex) {
58+
this.enclosingMethodInfoIndex = enclosingMethodInfoIndex;
59+
this.annotationsIndex = annotationsIndex;
60+
this.typeAnnotationsIndex = typeAnnotationsIndex;
61+
this.classesEncodingIndex = classesEncodingIndex;
62+
this.permittedSubclassesEncodingIndex = permittedSubclassesEncodingIndex;
63+
this.nestMembersEncodingIndex = nestMembersEncodingIndex;
64+
this.signersEncodingIndex = signersEncodingIndex;
65+
}
66+
67+
@Override
68+
public Object[] getEnclosingMethod(DynamicHub declaringClass) {
69+
if (enclosingMethodInfoIndex == NO_DATA) {
70+
return null;
71+
}
72+
Object[] enclosingMethod = ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseEnclosingMethod(enclosingMethodInfoIndex, declaringClass);
73+
if (enclosingMethod != null) {
74+
PredefinedClassesSupport.throwIfUnresolvable((Class<?>) enclosingMethod[0], declaringClass.getClassLoader0());
75+
}
76+
return enclosingMethod;
77+
}
78+
79+
@Override
80+
public Object[] getSigners(DynamicHub declaringClass) {
81+
if (signersEncodingIndex == NO_DATA) {
82+
return null;
83+
}
84+
return ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseObjects(signersEncodingIndex, declaringClass);
85+
}
86+
87+
@Override
88+
public byte[] getRawAnnotations(DynamicHub declaringClass) {
89+
if (annotationsIndex == NO_DATA) {
90+
return null;
91+
}
92+
return ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseByteArray(annotationsIndex, declaringClass);
93+
}
94+
95+
@Override
96+
public byte[] getRawTypeAnnotations(DynamicHub declaringClass) {
97+
if (typeAnnotationsIndex == NO_DATA) {
98+
return null;
99+
}
100+
return ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseByteArray(typeAnnotationsIndex, declaringClass);
101+
}
102+
103+
@Override
104+
public Class<?>[] getDeclaredClasses(DynamicHub declaringClass) {
105+
if (classesEncodingIndex == NO_DATA) {
106+
return new Class<?>[0];
107+
}
108+
Class<?>[] declaredClasses = ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseClasses(classesEncodingIndex, declaringClass);
109+
for (Class<?> clazz : declaredClasses) {
110+
PredefinedClassesSupport.throwIfUnresolvable(clazz, declaringClass.getClassLoader0());
111+
}
112+
return declaredClasses;
113+
}
114+
115+
@Override
116+
public Class<?>[] getNestMembers(DynamicHub declaringClass) {
117+
if (nestMembersEncodingIndex == NO_DATA) {
118+
return new Class<?>[]{DynamicHub.toClass(declaringClass)};
119+
}
120+
Class<?>[] nestMembers = ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseClasses(nestMembersEncodingIndex, declaringClass);
121+
for (Class<?> clazz : nestMembers) {
122+
PredefinedClassesSupport.throwIfUnresolvable(clazz, declaringClass.getClassLoader0());
123+
}
124+
return nestMembers;
125+
}
126+
127+
@Override
128+
public Class<?>[] getPermittedSubClasses(DynamicHub declaringClass) {
129+
if (permittedSubclassesEncodingIndex == NO_DATA) {
130+
return new Class<?>[0];
131+
}
132+
Class<?>[] permittedSubclasses = ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseClasses(permittedSubclassesEncodingIndex, declaringClass);
133+
for (Class<?> clazz : permittedSubclasses) {
134+
PredefinedClassesSupport.throwIfUnresolvable(clazz, declaringClass.getClassLoader0());
135+
}
136+
return permittedSubclasses;
137+
}
138+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.hub;
26+
27+
import com.oracle.svm.core.BuildPhaseProvider;
28+
import com.oracle.svm.core.heap.UnknownPrimitiveField;
29+
import com.oracle.svm.core.reflect.RuntimeMetadataDecoder;
30+
import org.graalvm.nativeimage.ImageSingletons;
31+
32+
import java.lang.reflect.Constructor;
33+
import java.lang.reflect.Field;
34+
import java.lang.reflect.Method;
35+
import java.lang.reflect.RecordComponent;
36+
37+
import static com.oracle.svm.core.reflect.RuntimeMetadataDecoder.NO_DATA;
38+
39+
/**
40+
* Instances of this class are used to represent the reflection metadata for Dynamic hubs prepared
41+
* at build time and included in the image. For dynamic hubs loaded dynamically at runtime
42+
* {@link RuntimeReflectionMetadata} is used.
43+
*/
44+
public final class ImageReflectionMetadata implements ReflectionMetadata {
45+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
46+
final int fieldsEncodingIndex;
47+
48+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
49+
final int methodsEncodingIndex;
50+
51+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
52+
final int constructorsEncodingIndex;
53+
54+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
55+
final int recordComponentsEncodingIndex;
56+
57+
@UnknownPrimitiveField(availability = BuildPhaseProvider.CompileQueueFinished.class)//
58+
final int classFlags;
59+
60+
ImageReflectionMetadata(int fieldsEncodingIndex, int methodsEncodingIndex, int constructorsEncodingIndex, int recordComponentsEncodingIndex, int classFlags) {
61+
this.fieldsEncodingIndex = fieldsEncodingIndex;
62+
this.methodsEncodingIndex = methodsEncodingIndex;
63+
this.constructorsEncodingIndex = constructorsEncodingIndex;
64+
this.recordComponentsEncodingIndex = recordComponentsEncodingIndex;
65+
this.classFlags = classFlags;
66+
}
67+
68+
public int getClassFlags() {
69+
return classFlags;
70+
}
71+
72+
@Override
73+
public Field[] getDeclaredFields(DynamicHub declaringClass, boolean publicOnly, int layerNum) {
74+
if (fieldsEncodingIndex == NO_DATA) {
75+
return new Field[0];
76+
}
77+
return ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseFields(declaringClass, fieldsEncodingIndex, publicOnly, layerNum);
78+
}
79+
80+
@Override
81+
public Method[] getDeclaredMethods(DynamicHub declaringClass, boolean publicOnly, int layerNum) {
82+
if (methodsEncodingIndex == NO_DATA) {
83+
return new Method[0];
84+
}
85+
return ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseMethods(declaringClass, methodsEncodingIndex, publicOnly, layerNum);
86+
}
87+
88+
@Override
89+
public Constructor<?>[] getDeclaredConstructors(DynamicHub declaringClass, boolean publicOnly, int layerNum) {
90+
if (constructorsEncodingIndex == NO_DATA) {
91+
return new Constructor<?>[0];
92+
}
93+
return ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseConstructors(declaringClass, constructorsEncodingIndex, publicOnly, layerNum);
94+
}
95+
96+
@Override
97+
public RecordComponent[] getRecordComponents(DynamicHub declaringClass, int layerNum) {
98+
if (recordComponentsEncodingIndex == NO_DATA) {
99+
/* See ReflectionDataBuilder.buildRecordComponents() for details. */
100+
throw DynamicHub.recordsNotAvailable(declaringClass);
101+
}
102+
return ImageSingletons.lookup(RuntimeMetadataDecoder.class).parseRecordComponents(declaringClass, recordComponentsEncodingIndex, layerNum);
103+
}
104+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/LayeredReflectionMetadataSingleton.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class LayeredReflectionMetadataSingleton implements MultiLayeredImageSing
5252
private static final String LAYERED_REFLECTION_METADATA_HUBS = "layered reflection metadata hubs";
5353
private static final String LAYERED_REFLECTION_METADATA_CLASS_FLAGS = "layered reflection metadata classFlags";
5454

55-
private final EconomicMap<Integer, DynamicHub.ImageReflectionMetadata> reflectionMetadataMap = EconomicMap.create();
55+
private final EconomicMap<Integer, ImageReflectionMetadata> reflectionMetadataMap = EconomicMap.create();
5656

5757
/**
5858
* The class flags registered in previous layers. This map is used to check if the class flags
@@ -82,7 +82,7 @@ public static LayeredReflectionMetadataSingleton[] singletons() {
8282
}
8383

8484
@Platforms(Platform.HOSTED_ONLY.class)
85-
public void setReflectionMetadata(DynamicHub hub, DynamicHub.ImageReflectionMetadata reflectionMetadata) {
85+
public void setReflectionMetadata(DynamicHub hub, ImageReflectionMetadata reflectionMetadata) {
8686
/* GR-63472: Two different classes could have the same name in different class loaders */
8787
assert !reflectionMetadataMap.containsKey(hub.getTypeID()) : "The hub %s was added twice in the same layered reflection metadata".formatted(hub);
8888
if (isClassFlagsSubsetOfPreviousLayer(hub.getTypeID(), reflectionMetadata) && isReflectionMetadataEmpty(reflectionMetadata)) {
@@ -100,12 +100,12 @@ private static int getCombinedClassFlags(ReflectionMetadata reflectionMetadata,
100100
return previousLayerFlags | reflectionMetadata.getClassFlags();
101101
}
102102

103-
private static boolean isReflectionMetadataEmpty(DynamicHub.ImageReflectionMetadata reflectionMetadata) {
103+
private static boolean isReflectionMetadataEmpty(ImageReflectionMetadata reflectionMetadata) {
104104
return reflectionMetadata.fieldsEncodingIndex == -1 && reflectionMetadata.methodsEncodingIndex == -1 &&
105105
reflectionMetadata.constructorsEncodingIndex == -1 && reflectionMetadata.recordComponentsEncodingIndex == -1;
106106
}
107107

108-
public DynamicHub.ImageReflectionMetadata getReflectionMetadata(DynamicHub hub) {
108+
public ImageReflectionMetadata getReflectionMetadata(DynamicHub hub) {
109109
return reflectionMetadataMap.get(hub.getTypeID());
110110
}
111111

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/ReflectionMetadata.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25-
2625
package com.oracle.svm.core.hub;
2726

2827
import java.lang.reflect.Constructor;

0 commit comments

Comments
 (0)