2424 */
2525package com .oracle .svm .core .hub ;
2626
27+ import static com .oracle .svm .configure .config .ConfigurationMemberInfo .ConfigurationMemberAccessibility ;
28+ import static com .oracle .svm .configure .config .ConfigurationMemberInfo .ConfigurationMemberDeclaration ;
2729import static com .oracle .svm .core .MissingRegistrationUtils .throwMissingRegistrationErrors ;
2830import static com .oracle .svm .core .Uninterruptible .CALLED_FROM_UNINTERRUPTIBLE_CODE ;
2931import static com .oracle .svm .core .annotate .TargetElement .CONSTRUCTOR_NAME ;
8688import org .graalvm .nativeimage .Platforms ;
8789import org .graalvm .nativeimage .c .function .CFunctionPointer ;
8890
91+ import com .oracle .svm .configure .config .ConfigurationType ;
8992import com .oracle .svm .core .BuildPhaseProvider .AfterHostedUniverse ;
9093import com .oracle .svm .core .BuildPhaseProvider .CompileQueueFinished ;
9194import com .oracle .svm .core .NeverInline ;
116119import com .oracle .svm .core .jdk .ProtectionDomainSupport ;
117120import com .oracle .svm .core .jdk .Resources ;
118121import com .oracle .svm .core .meta .SharedType ;
122+ import com .oracle .svm .core .metadata .MetadataTracer ;
119123import com .oracle .svm .core .reflect .MissingReflectionRegistrationUtils ;
120124import com .oracle .svm .core .reflect .RuntimeMetadataDecoder ;
121125import com .oracle .svm .core .reflect .RuntimeMetadataDecoder .ConstructorDescriptor ;
143147import jdk .internal .reflect .FieldAccessor ;
144148import jdk .internal .reflect .Reflection ;
145149import jdk .internal .reflect .ReflectionFactory ;
150+ import jdk .vm .ci .meta .MetaUtil ;
146151import jdk .vm .ci .meta .ResolvedJavaType ;
147152import sun .reflect .annotation .AnnotationType ;
148153import sun .reflect .generics .factory .GenericsFactory ;
@@ -694,6 +699,9 @@ private ReflectionMetadata reflectionMetadata() {
694699 }
695700
696701 private void checkClassFlag (int mask , String methodName ) {
702+ if (MetadataTracer .Options .MetadataTracingSupport .getValue () && MetadataTracer .singleton ().enabled ()) {
703+ traceClassFlagQuery (mask );
704+ }
697705 if (throwMissingRegistrationErrors () && !(isClassFlagSet (mask ) && getConditions ().satisfied ())) {
698706 MissingReflectionRegistrationUtils .forBulkQuery (DynamicHub .toClass (this ), methodName );
699707 }
@@ -703,6 +711,25 @@ private boolean isClassFlagSet(int mask) {
703711 return (reflectionMetadata () != null && (reflectionMetadata ().classFlags & mask ) != 0 );
704712 }
705713
714+ private void traceClassFlagQuery (int mask ) {
715+ ConfigurationType type = MetadataTracer .singleton ().traceReflectionType (getName ());
716+ switch (mask ) {
717+ case ALL_FIELDS_FLAG -> type .setAllPublicFields (ConfigurationMemberAccessibility .ACCESSED );
718+ case ALL_DECLARED_FIELDS_FLAG -> type .setAllDeclaredFields (ConfigurationMemberAccessibility .ACCESSED );
719+ case ALL_METHODS_FLAG -> type .setAllPublicMethods (ConfigurationMemberAccessibility .QUERIED );
720+ case ALL_DECLARED_METHODS_FLAG -> type .setAllDeclaredMethods (ConfigurationMemberAccessibility .QUERIED );
721+ case ALL_CONSTRUCTORS_FLAG -> type .setAllPublicConstructors (ConfigurationMemberAccessibility .QUERIED );
722+ case ALL_DECLARED_CONSTRUCTORS_FLAG -> type .setAllDeclaredConstructors (ConfigurationMemberAccessibility .QUERIED );
723+ case ALL_CLASSES_FLAG -> type .setAllPublicClasses ();
724+ case ALL_DECLARED_CLASSES_FLAG -> type .setAllDeclaredClasses ();
725+ case ALL_RECORD_COMPONENTS_FLAG -> type .setAllRecordComponents ();
726+ case ALL_PERMITTED_SUBCLASSES_FLAG -> type .setAllPermittedSubclasses ();
727+ case ALL_NEST_MEMBERS_FLAG -> type .setAllNestMembers ();
728+ case ALL_SIGNERS_FLAG -> type .setAllSigners ();
729+ default -> throw VMError .shouldNotReachHere ("unknown class flag " + mask );
730+ }
731+ }
732+
706733 /** Executed at runtime. */
707734 private static Object initEnumConstantsAtRuntime (Method values ) {
708735 try {
@@ -1261,6 +1288,14 @@ private void checkField(String fieldName, Field field, boolean publicOnly) throw
12611288 */
12621289 throw new NoSuchFieldException (fieldName );
12631290 } else {
1291+ if (MetadataTracer .Options .MetadataTracingSupport .getValue () && MetadataTracer .singleton ().enabled ()) {
1292+ ConfigurationMemberDeclaration declaration = publicOnly ? ConfigurationMemberDeclaration .PRESENT : ConfigurationMemberDeclaration .DECLARED ;
1293+ // register declaring type and field
1294+ ConfigurationType declaringType = MetadataTracer .singleton ().traceReflectionType (field .getDeclaringClass ().getName ());
1295+ declaringType .addField (fieldName , declaration , false );
1296+ // register receiver type
1297+ MetadataTracer .singleton ().traceReflectionType (getName ());
1298+ }
12641299 RuntimeMetadataDecoder decoder = ImageSingletons .lookup (RuntimeMetadataDecoder .class );
12651300 int fieldModifiers = field .getModifiers ();
12661301 boolean negative = decoder .isNegative (fieldModifiers );
@@ -1328,13 +1363,31 @@ private boolean checkExecutableExists(String methodName, Class<?>[] parameterTyp
13281363 int methodModifiers = method .getModifiers ();
13291364 boolean negative = decoder .isNegative (methodModifiers );
13301365 boolean hiding = decoder .isHiding (methodModifiers );
1366+ if (MetadataTracer .Options .MetadataTracingSupport .getValue () && MetadataTracer .singleton ().enabled ()) {
1367+ ConfigurationMemberDeclaration declaration = publicOnly ? ConfigurationMemberDeclaration .PRESENT : ConfigurationMemberDeclaration .DECLARED ;
1368+ // register declaring type and method
1369+ ConfigurationType declaringType = MetadataTracer .singleton ().traceReflectionType (method .getDeclaringClass ().getName ());
1370+ declaringType .addMethod (methodName , toInternalSignature (parameterTypes ), declaration );
1371+ // register receiver type
1372+ MetadataTracer .singleton ().traceReflectionType (getName ());
1373+ }
13311374 if (throwMissingErrors && hiding ) {
13321375 MissingReflectionRegistrationUtils .forMethod (clazz , methodName , parameterTypes );
13331376 }
13341377 return !(negative || hiding );
13351378 }
13361379 }
13371380
1381+ private static String toInternalSignature (Class <?>[] classes ) {
1382+ StringBuilder sb = new StringBuilder ("(" );
1383+ if (classes != null ) {
1384+ for (Class <?> clazz : classes ) {
1385+ sb .append (MetaUtil .toInternalName (clazz .getName ()));
1386+ }
1387+ }
1388+ return sb .append (')' ).toString ();
1389+ }
1390+
13381391 private boolean allElementsRegistered (boolean publicOnly , int allDeclaredElementsFlag , int allPublicElementsFlag ) {
13391392 return isClassFlagSet (allDeclaredElementsFlag ) || (publicOnly && isClassFlagSet (allPublicElementsFlag ));
13401393 }
@@ -1824,6 +1877,8 @@ public DynamicHub arrayType() {
18241877 }
18251878 if (companion .arrayHub == null ) {
18261879 MissingReflectionRegistrationUtils .forClass (getTypeName () + "[]" );
1880+ } else if (MetadataTracer .Options .MetadataTracingSupport .getValue () && MetadataTracer .singleton ().enabled () && !isPrimitive ()) {
1881+ MetadataTracer .singleton ().traceReflectionType (companion .arrayHub .getTypeName ());
18271882 }
18281883 return companion .arrayHub ;
18291884 }
0 commit comments