Skip to content

Commit 3e4e2b3

Browse files
committed
Refactor tracing to take Class instances instead of type Strings
1 parent 8b19a65 commit 3e4e2b3

File tree

8 files changed

+84
-57
lines changed

8 files changed

+84
-57
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public static Throwable getSavedException(String className) {
501501
public static boolean canUnsafeInstantiateAsInstance(DynamicHub hub) {
502502
Class<?> clazz = DynamicHub.toClass(hub);
503503
if (MetadataTracer.enabled()) {
504-
MetadataTracer.singleton().traceUnsafeAllocatedType(clazz.getTypeName());
504+
MetadataTracer.singleton().traceUnsafeAllocatedType(clazz);
505505
}
506506
RuntimeConditionSet conditionSet = null;
507507
for (var singleton : layeredSingletons()) {

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import org.graalvm.nativeimage.Platform;
9292
import org.graalvm.nativeimage.Platforms;
9393

94+
import com.oracle.svm.configure.ConfigurationTypeDescriptor;
9495
import com.oracle.svm.configure.config.ConfigurationType;
9596
import com.oracle.svm.configure.config.SignatureUtil;
9697
import com.oracle.svm.core.BuildPhaseProvider.AfterHostedUniverse;
@@ -786,7 +787,7 @@ private static boolean isClassFlagSet(int mask, ReflectionMetadata reflectionMet
786787
}
787788

788789
private void traceClassFlagQuery(int mask) {
789-
ConfigurationType type = MetadataTracer.singleton().traceReflectionTypeImpl(getTypeName());
790+
ConfigurationType type = MetadataTracer.singleton().traceReflectionTypeImpl(ConfigurationTypeDescriptor.fromClass(toClass(this)));
790791
if (type == null) {
791792
return;
792793
}
@@ -1397,12 +1398,12 @@ private void traceFieldLookup(String fieldName, Field field, boolean publicOnly)
13971398
ConfigurationMemberDeclaration declaration = publicOnly ? ConfigurationMemberDeclaration.PRESENT : ConfigurationMemberDeclaration.DECLARED;
13981399
if (field != null) {
13991400
// register declaring type and field
1400-
MetadataTracer.singleton().traceField(field.getDeclaringClass().getTypeName(), fieldName, declaration);
1401+
MetadataTracer.singleton().traceField(field.getDeclaringClass(), fieldName, declaration);
14011402
// register receiver type
1402-
MetadataTracer.singleton().traceReflectionType(getTypeName());
1403+
MetadataTracer.singleton().traceReflectionType(toClass(this));
14031404
} else {
14041405
// register receiver type and negative field query
1405-
MetadataTracer.singleton().traceField(getTypeName(), fieldName, declaration);
1406+
MetadataTracer.singleton().traceField(toClass(this), fieldName, declaration);
14061407
}
14071408
}
14081409

@@ -1477,12 +1478,12 @@ private void traceMethodLookup(String methodName, Class<?>[] parameterTypes, Exe
14771478
if (method != null) {
14781479
// register declaring type and method
14791480
// TODO (GR-64765) loosen to queried accessibility once method invocations are traced
1480-
MetadataTracer.singleton().traceMethod(method.getDeclaringClass().getTypeName(), methodName, toInternalSignature(parameterTypes), declaration, ConfigurationMemberAccessibility.ACCESSED);
1481+
MetadataTracer.singleton().traceMethod(method.getDeclaringClass(), methodName, toInternalSignature(parameterTypes), declaration, ConfigurationMemberAccessibility.ACCESSED);
14811482
// register receiver type
1482-
MetadataTracer.singleton().traceReflectionType(getTypeName());
1483+
MetadataTracer.singleton().traceReflectionType(toClass(this));
14831484
} else {
14841485
// register receiver type and negative method query
1485-
MetadataTracer.singleton().traceMethod(getTypeName(), methodName, toInternalSignature(parameterTypes), declaration, ConfigurationMemberAccessibility.QUERIED);
1486+
MetadataTracer.singleton().traceMethod(toClass(this), methodName, toInternalSignature(parameterTypes), declaration, ConfigurationMemberAccessibility.QUERIED);
14861487
}
14871488
}
14881489

@@ -2014,18 +2015,14 @@ public DynamicHub arrayType() {
20142015
throw new UnsupportedOperationException(new IllegalArgumentException());
20152016
}
20162017
if (MetadataTracer.enabled()) {
2017-
MetadataTracer.singleton().traceReflectionType(arrayTypeName());
2018+
MetadataTracer.singleton().traceReflectionArrayType(toClass(this));
20182019
}
20192020
if (companion.arrayHub == null) {
2020-
MissingReflectionRegistrationUtils.reportClassAccess(arrayTypeName());
2021+
MissingReflectionRegistrationUtils.reportClassAccess(getTypeName() + "[]");
20212022
}
20222023
return companion.arrayHub;
20232024
}
20242025

2025-
private String arrayTypeName() {
2026-
return getTypeName() + "[]";
2027-
}
2028-
20292026
@KeepOriginal
20302027
private native Class<?> elementType();
20312028

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaIOSubstitutions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static ObjectStreamClass lookup(Class<?> cl, boolean all) {
6666

6767
if (Serializable.class.isAssignableFrom(cl) && !cl.isArray()) {
6868
if (MetadataTracer.enabled()) {
69-
MetadataTracer.singleton().traceSerializationType(cl.getTypeName());
69+
MetadataTracer.singleton().traceSerializationType(cl);
7070
}
7171
if (!DynamicHub.fromClass(cl).isRegisteredForSerialization()) {
7272
MissingSerializationRegistrationUtils.reportSerialization(cl);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangReflectSubstitutions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private static void set(Object a, int index, Object value) {
388388
private static Object newArray(Class<?> componentType, int length)
389389
throws NegativeArraySizeException {
390390
if (MetadataTracer.enabled()) {
391-
MetadataTracer.singleton().traceReflectionType(componentType.arrayType().getTypeName());
391+
MetadataTracer.singleton().traceReflectionArrayType(componentType);
392392
}
393393
return KnownIntrinsics.unvalidatedNewArray(componentType, length);
394394
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jni/access/JNIReflectionDictionary.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ public static JNIMethodId getDeclaredMethodID(Class<?> classObject, JNIAccessibl
273273

274274
private static JNIAccessibleMethod getDeclaredMethod(Class<?> classObject, JNIAccessibleMethodDescriptor descriptor, String dumpLabel) {
275275
if (MetadataTracer.enabled()) {
276-
MetadataTracer.singleton().traceJNIType(classObject.getTypeName());
276+
MetadataTracer.singleton().traceJNIType(classObject);
277277
// TODO (GR-64765) loosen to queried accessibility once method invocations are traced
278-
MetadataTracer.singleton().traceMethod(classObject.getTypeName(), descriptor.getNameConvertToString(), descriptor.getSignatureConvertToString(),
278+
MetadataTracer.singleton().traceMethod(classObject, descriptor.getNameConvertToString(), descriptor.getSignatureConvertToString(),
279279
ConfigurationMemberInfo.ConfigurationMemberDeclaration.DECLARED, ConfigurationMemberInfo.ConfigurationMemberAccessibility.ACCESSED);
280280
}
281281
boolean foundClass = false;
@@ -334,8 +334,8 @@ private static JNIAccessibleMethod checkMethod(JNIAccessibleMethod method, Class
334334

335335
private static JNIAccessibleField getDeclaredField(Class<?> classObject, CharSequence name, boolean isStatic, String dumpLabel) {
336336
if (MetadataTracer.enabled()) {
337-
MetadataTracer.singleton().traceJNIType(classObject.getTypeName());
338-
MetadataTracer.singleton().traceField(classObject.getTypeName(), name.toString(), ConfigurationMemberInfo.ConfigurationMemberDeclaration.DECLARED);
337+
MetadataTracer.singleton().traceJNIType(classObject);
338+
MetadataTracer.singleton().traceField(classObject, name.toString(), ConfigurationMemberInfo.ConfigurationMemberDeclaration.DECLARED);
339339
}
340340
boolean foundClass = false;
341341
for (var dictionary : layeredSingletons()) {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/metadata/MetadataTracer.java

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
31+
import java.util.Arrays;
3132
import java.util.HashMap;
3233
import java.util.LinkedHashSet;
3334
import java.util.List;
@@ -173,50 +174,70 @@ private ConfigurationSet getConfigurationSetForTracing() {
173174
}
174175

175176
/**
176-
* Marks the given type as reachable from reflection.
177+
* Marks the type with the given name as reachable from reflection.
177178
*/
178179
public void traceReflectionType(String typeName) {
179-
traceReflectionTypeImpl(typeName);
180+
traceReflectionTypeImpl(new NamedConfigurationTypeDescriptor(typeName));
181+
}
182+
183+
/**
184+
* Marks the given type as reachable from reflection.
185+
*/
186+
public void traceReflectionType(Class<?> clazz) {
187+
traceReflectionTypeImpl(ConfigurationTypeDescriptor.fromClass(clazz));
188+
}
189+
190+
public void traceReflectionArrayType(Class<?> componentClazz) {
191+
ConfigurationTypeDescriptor typeDescriptor = ConfigurationTypeDescriptor.fromClass(componentClazz);
192+
if (typeDescriptor instanceof NamedConfigurationTypeDescriptor(String name)) {
193+
traceReflectionType(name + "[]");
194+
} else {
195+
debug("array type not registered for reflection (component type is not a named type)", typeDescriptor);
196+
}
180197
}
181198

182199
/**
183200
* Marks the given field as reachable from reflection.
184201
*/
185-
public void traceField(String typeName, String fieldName, ConfigurationMemberInfo.ConfigurationMemberDeclaration declaration) {
186-
ConfigurationType type = traceReflectionTypeImpl(typeName);
202+
public void traceField(Class<?> declaringClass, String fieldName, ConfigurationMemberInfo.ConfigurationMemberDeclaration declaration) {
203+
ConfigurationTypeDescriptor typeDescriptor = ConfigurationTypeDescriptor.fromClass(declaringClass);
204+
ConfigurationType type = traceReflectionTypeImpl(typeDescriptor);
187205
if (type != null) {
188-
debugField(typeName, fieldName);
206+
debugField(typeDescriptor, fieldName);
189207
type.addField(fieldName, declaration, false);
190208
}
191209
}
192210

193211
/**
194212
* Marks the given method as reachable from reflection.
195213
*/
196-
public void traceMethod(String typeName, String methodName, String internalSignature, ConfigurationMemberInfo.ConfigurationMemberDeclaration declaration,
214+
public void traceMethod(Class<?> declaringClass, String methodName, String internalSignature, ConfigurationMemberInfo.ConfigurationMemberDeclaration declaration,
197215
ConfigurationMemberInfo.ConfigurationMemberAccessibility accessibility) {
198-
ConfigurationType type = traceReflectionTypeImpl(typeName);
216+
ConfigurationTypeDescriptor typeDescriptor = ConfigurationTypeDescriptor.fromClass(declaringClass);
217+
ConfigurationType type = traceReflectionTypeImpl(typeDescriptor);
199218
if (type != null) {
200-
debugMethod(typeName, methodName, internalSignature, accessibility);
219+
debugMethod(typeDescriptor, methodName, internalSignature, accessibility);
201220
type.addMethod(methodName, internalSignature, declaration, accessibility);
202221
}
203222
}
204223

205224
/**
206225
* Marks the given type as unsafely allocated.
207226
*/
208-
public void traceUnsafeAllocatedType(String typeName) {
209-
ConfigurationType type = traceReflectionTypeImpl(typeName);
227+
public void traceUnsafeAllocatedType(Class<?> clazz) {
228+
ConfigurationTypeDescriptor typeDescriptor = ConfigurationTypeDescriptor.fromClass(clazz);
229+
ConfigurationType type = traceReflectionTypeImpl(typeDescriptor);
210230
if (type != null) {
211-
debug("type marked as unsafely allocated", typeName);
231+
debug("type marked as unsafely allocated", clazz.getTypeName());
212232
type.setUnsafeAllocated();
213233
}
214234
}
215235

216236
/**
217237
* Marks the given proxy type as reachable from reflection.
218238
*/
219-
public void traceProxyType(List<String> interfaceNames) {
239+
public void traceProxyType(Class<?>[] interfaces) {
240+
List<String> interfaceNames = Arrays.stream(interfaces).map(Class::getTypeName).toList();
220241
ProxyConfigurationTypeDescriptor descriptor = new ProxyConfigurationTypeDescriptor(interfaceNames);
221242
for (String interfaceName : interfaceNames) {
222243
if (isInternal(interfaceName)) {
@@ -233,15 +254,11 @@ public void traceProxyType(List<String> interfaceNames) {
233254
* not want to expose ConfigurationTypes to the caller, because they could perform further
234255
* registration (e.g., of methods) which would not be traced by debug logging.
235256
*/
236-
public ConfigurationType traceReflectionTypeImpl(String typeName) {
237-
if (isInternal(typeName)) {
257+
public ConfigurationType traceReflectionTypeImpl(ConfigurationTypeDescriptor typeDescriptor) {
258+
assert enabledAtRunTime();
259+
if (isInternal(typeDescriptor)) {
238260
return null;
239261
}
240-
return traceReflectionTypeImpl(new NamedConfigurationTypeDescriptor(typeName));
241-
}
242-
243-
private ConfigurationType traceReflectionTypeImpl(ConfigurationTypeDescriptor typeDescriptor) {
244-
assert enabledAtRunTime();
245262
ConfigurationSet configurationSet = getConfigurationSetForTracing();
246263
if (configurationSet != null) {
247264
debugReflectionType(typeDescriptor, configurationSet);
@@ -250,18 +267,36 @@ private ConfigurationType traceReflectionTypeImpl(ConfigurationTypeDescriptor ty
250267
return null;
251268
}
252269

270+
private static boolean isInternal(ConfigurationTypeDescriptor typeDescriptor) {
271+
if (typeDescriptor instanceof NamedConfigurationTypeDescriptor(String name)) {
272+
return isInternal(name);
273+
}
274+
return false;
275+
}
276+
253277
private static boolean isInternal(String typeName) {
254278
return typeName.startsWith("com.oracle.svm.core");
255279
}
256280

257281
/**
258-
* Marks the given type as reachable from JNI.
282+
* Marks the type with the given name as reachable from JNI.
259283
*/
260284
public void traceJNIType(String typeName) {
285+
traceJNITypeImpl(new NamedConfigurationTypeDescriptor(typeName));
286+
}
287+
288+
/**
289+
* Marks the given type as reachable from JNI.
290+
*/
291+
public void traceJNIType(Class<?> clazz) {
292+
traceJNITypeImpl(ConfigurationTypeDescriptor.fromClass(clazz));
293+
}
294+
295+
private void traceJNITypeImpl(ConfigurationTypeDescriptor typeDescriptor) {
261296
assert enabledAtRunTime();
262-
ConfigurationType type = traceReflectionTypeImpl(typeName);
297+
ConfigurationType type = traceReflectionTypeImpl(typeDescriptor);
263298
if (type != null && !type.isJniAccessible()) {
264-
debug("type registered for jni", typeName);
299+
debug("type registered for jni", typeDescriptor);
265300
type.setJniAccessible();
266301
}
267302
}
@@ -294,11 +329,12 @@ public void traceResourceBundle(String baseName) {
294329
/**
295330
* Marks the given type as serializable.
296331
*/
297-
public void traceSerializationType(String typeName) {
332+
public void traceSerializationType(Class<?> clazz) {
298333
assert enabledAtRunTime();
299-
ConfigurationType result = traceReflectionTypeImpl(typeName);
334+
ConfigurationTypeDescriptor typeDescriptor = ConfigurationTypeDescriptor.fromClass(clazz);
335+
ConfigurationType result = traceReflectionTypeImpl(typeDescriptor);
300336
if (result != null && !result.isSerializable()) {
301-
debug("type registered for serialization", typeName);
337+
debug("type registered for serialization", typeDescriptor);
302338
result.setSerializable();
303339
}
304340
}
@@ -359,21 +395,21 @@ private void debugReflectionType(ConfigurationTypeDescriptor typeDescriptor, Con
359395
/**
360396
* Debug helper for fields. Avoids field name computations if debug logging is disabled.
361397
*/
362-
private void debugField(String typeName, String fieldName) {
398+
private void debugField(ConfigurationTypeDescriptor typeDescriptor, String fieldName) {
363399
if (debugWriter == null) {
364400
return;
365401
}
366-
debug("field registered for reflection", typeName + "." + fieldName);
402+
debug("field registered for reflection", typeDescriptor + "." + fieldName);
367403
}
368404

369405
/**
370406
* Debug helper for methods. Avoids method name computations if debug logging is disabled.
371407
*/
372-
private void debugMethod(String typeName, String methodName, String internalSignature, ConfigurationMemberInfo.ConfigurationMemberAccessibility accessibility) {
408+
private void debugMethod(ConfigurationTypeDescriptor typeDescriptor, String methodName, String internalSignature, ConfigurationMemberInfo.ConfigurationMemberAccessibility accessibility) {
373409
if (debugWriter == null) {
374410
return;
375411
}
376-
debug("method registered for reflection (" + accessibility.name().toLowerCase() + ")", typeName + "." + methodName + internalSignature);
412+
debug("method registered for reflection (" + accessibility.name().toLowerCase() + ")", typeDescriptor + "." + methodName + internalSignature);
377413
}
378414

379415
/**

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/proxy/DynamicProxySupport.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626

2727
import java.lang.reflect.InvocationHandler;
2828
import java.lang.reflect.Proxy;
29-
import java.util.ArrayList;
3029
import java.util.Arrays;
3130
import java.util.EnumSet;
32-
import java.util.List;
3331
import java.util.regex.Pattern;
3432

3533
import org.graalvm.collections.EconomicMap;
@@ -192,11 +190,7 @@ private static ClassLoader getCommonClassLoaderOrFail(ClassLoader loader, Class<
192190
@Override
193191
public Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces) {
194192
if (MetadataTracer.enabled()) {
195-
List<String> interfaceNames = new ArrayList<>(interfaces.length);
196-
for (Class<?> iface : interfaces) {
197-
interfaceNames.add(iface.getName());
198-
}
199-
MetadataTracer.singleton().traceProxyType(interfaceNames);
193+
MetadataTracer.singleton().traceProxyType(interfaces);
200194
}
201195

202196
ProxyCacheKey key = new ProxyCacheKey(interfaces);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/serialize/SerializationSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public static Object getSerializationConstructorAccessor(Class<?> serializationT
268268
}
269269
} else {
270270
if (MetadataTracer.enabled()) {
271-
MetadataTracer.singleton().traceSerializationType(declaringClass.getTypeName());
271+
MetadataTracer.singleton().traceSerializationType(declaringClass);
272272
}
273273
for (var singleton : layeredSingletons()) {
274274
Object constructorAccessor = singleton.getSerializationConstructorAccessor0(declaringClass, targetConstructorClass);

0 commit comments

Comments
 (0)