Skip to content

Commit bb5696b

Browse files
committed
Options to include metadata for module, package or classpath
1 parent 2d50264 commit bb5696b

File tree

17 files changed

+199
-74
lines changed

17 files changed

+199
-74
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/RuntimeReflection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static void registerAllDeclaredMethods(Class<?> declaringClass) {
201201
* @since 23.0
202202
*/
203203
public static void registerAllConstructors(Class<?> declaringClass) {
204-
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllConstructorsQuery(ConfigurationCondition.alwaysTrue(), true, declaringClass);
204+
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllConstructorsQuery(ConfigurationCondition.alwaysTrue(), false, declaringClass);
205205
}
206206

207207
/**
@@ -211,7 +211,7 @@ public static void registerAllConstructors(Class<?> declaringClass) {
211211
* @since 23.0
212212
*/
213213
public static void registerAllDeclaredConstructors(Class<?> declaringClass) {
214-
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredConstructorsQuery(ConfigurationCondition.alwaysTrue(), true, declaringClass);
214+
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredConstructorsQuery(ConfigurationCondition.alwaysTrue(), false, declaringClass);
215215
}
216216

217217
/**
@@ -221,7 +221,7 @@ public static void registerAllDeclaredConstructors(Class<?> declaringClass) {
221221
* @since 23.0
222222
*/
223223
public static void registerAllFields(Class<?> declaringClass) {
224-
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllFieldsQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
224+
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllFields(ConfigurationCondition.alwaysTrue(), declaringClass);
225225
}
226226

227227
/**
@@ -231,7 +231,7 @@ public static void registerAllFields(Class<?> declaringClass) {
231231
* @since 23.0
232232
*/
233233
public static void registerAllDeclaredFields(Class<?> declaringClass) {
234-
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredFieldsQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
234+
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredFields(ConfigurationCondition.alwaysTrue(), declaringClass);
235235
}
236236

237237
/**

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/ReflectionRegistry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.Arrays;
4646

4747
public interface ReflectionRegistry {
48+
// TODO thread this true behind MRE flag
4849
default void register(ConfigurationCondition condition, Class<?>... classes) {
4950
Arrays.stream(classes).forEach(clazz -> register(condition, false, clazz));
5051
}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/RuntimeReflectionSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public interface RuntimeReflectionSupport extends ReflectionRegistry {
4646

4747
void registerAllDeclaredMethodsQuery(ConfigurationCondition condition, boolean queriedOnly, Class<?> clazz);
4848

49-
void registerAllFieldsQuery(ConfigurationCondition condition, Class<?> clazz);
49+
void registerAllFields(ConfigurationCondition condition, Class<?> clazz);
5050

51-
void registerAllDeclaredFieldsQuery(ConfigurationCondition condition, Class<?> clazz);
51+
void registerAllDeclaredFields(ConfigurationCondition condition, Class<?> clazz);
5252

5353
void registerAllConstructorsQuery(ConfigurationCondition condition, boolean queriedOnly, Class<?> clazz);
5454

substratevm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ At runtime, premain runtime options are set along with main class' arguments in
2525
* (GR-60081) Native Image now targets `armv8.1-a` by default on AArch64. Use `-march=compatibility` for best compatibility or `-march=native` for best performance if the native executable is deployed on the same machine or on a machine with the same CPU features. To list all available machine types, use `-march=list`.
2626
* (GR-60234) Remove `"customTargetConstructorClass"` field from the serialization JSON metadata. All possible constructors are now registered by default when registering a type for serialization. `RuntimeSerialization.registerWithTargetConstructorClass` is now deprecated.
2727
* (GR-60237) Include serialization JSON reachability metadata as part of reflection metadata by introducing the `"serializable"` flag for reflection entries.
28+
* (GR-54953) Add options `-H:IncludeAllMetadataForClassPathEntry`, `-H:+IncludeAllForClassPath`, and `-H:IncludeAllMetadataForModule` for bulk inclusion of reachability metadata.
2829

2930
## GraalVM for JDK 23 (Internal Version 24.1.0)
3031
* (GR-51520) The old class initialization strategy, which was deprecated in GraalVM for JDK 22, is removed. The option `StrictImageHeap` no longer has any effect.

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/ClassInclusionPolicy.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.oracle.graal.pointsto.meta.AnalysisField;
3737
import com.oracle.graal.pointsto.meta.AnalysisMethod;
3838
import com.oracle.svm.core.annotate.TargetClass;
39+
import com.oracle.svm.util.ReflectionUtil;
3940

4041
import jdk.graal.compiler.api.replacements.Fold;
4142

@@ -56,12 +57,17 @@ public void setBigBang(BigBang bb) {
5657
this.bb = bb;
5758
}
5859

60+
public static boolean isClassIncludedBase(Class<?> cls) {
61+
Class<?> enclosingClass = ReflectionUtil.linkageSafeQuery(cls, null, Class::getEnclosingClass);
62+
return !Feature.class.isAssignableFrom(cls) && !AnnotationAccess.isAnnotationPresent(cls, TargetClass.class) && (enclosingClass == null || isClassIncludedBase(enclosingClass));
63+
}
64+
5965
/**
6066
* Determine if the given class needs to be included in the image according to the policy.
6167
*/
6268
public boolean isClassIncluded(Class<?> cls) {
6369
Class<?> enclosingClass = cls.getEnclosingClass();
64-
return !Feature.class.isAssignableFrom(cls) && !AnnotationAccess.isAnnotationPresent(cls, TargetClass.class) && (enclosingClass == null || isClassIncluded(enclosingClass));
70+
return isClassIncludedBase(cls) && (enclosingClass == null || isClassIncluded(enclosingClass));
6571
}
6672

6773
/**

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/PointsToAnalysis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ public void onTypeInstantiated(AnalysisType type) {
662662
/* Register the type as instantiated with all its super types. */
663663

664664
assert type.isInstantiated() : type;
665-
AnalysisError.guarantee(type.isArray() || (type.isInstanceClass() && !type.isAbstract()));
665+
AnalysisError.guarantee(type.isArray() || (type.isInstanceClass() && !type.isAbstract()), "Type %s must be either an array, or a non abstract instance class", type.getName());
666666

667667
TypeState typeState = TypeState.forExactType(this, type, true);
668668
TypeState typeStateNonNull = TypeState.forExactType(this, type, false);

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/ParserConfigurationAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public TypeResult<ConfigurationType> resolveType(UnresolvedConfigurationConditio
5555
}
5656

5757
@Override
58-
public void registerType(UnresolvedConfigurationCondition condition, ConfigurationType type) {
58+
public void registerType(UnresolvedConfigurationCondition condition, ConfigurationType type, boolean registerMetadata) {
5959
VMError.guarantee(condition.equals(type.getCondition()), "condition is already a part of the type");
6060
configuration.add(type);
6161
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,16 +1301,29 @@ public enum ReportingMode {
13011301
@Option(help = "Include all classes, methods, and fields from given modules", type = OptionType.Debug) //
13021302
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> IncludeAllFromModule = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build());
13031303

1304+
@Option(help = "Include all classes, methods, fields, and resources from a given module for dynamic access", type = OptionType.Debug) //
1305+
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> EnableDynamicAccessForModule = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build());
1306+
13041307
@Option(help = "Include all classes, methods, fields, and resources from given paths", type = OptionType.Debug) //
13051308
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> IncludeAllFromPath = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build());
13061309

13071310
@Option(help = "Include all classes, methods and fields from the given packages", type = OptionType.Debug) //
13081311
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> IncludeAllFromPackage = new HostedOptionKey<>(
13091312
AccumulatingLocatableMultiOptionValue.Strings.buildWithCommaDelimiter());
13101313

1314+
@Option(help = "Include all classes, methods and fields from the given packages for dynamic access", type = OptionType.Debug) //
1315+
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> EnableDynamicAccessForPackage = new HostedOptionKey<>(
1316+
AccumulatingLocatableMultiOptionValue.Strings.buildWithCommaDelimiter());
1317+
1318+
@Option(help = "Include all classes, methods, fields, and resources from given paths for dynamic access", type = OptionType.Debug) //
1319+
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> EnableDynamicAccessForClassPathEntry = new HostedOptionKey<>(AccumulatingLocatableMultiOptionValue.Strings.build());
1320+
13111321
@Option(help = "Include all classes, methods, fields, and resources from the class path", type = OptionType.Debug) //
13121322
public static final HostedOptionKey<Boolean> IncludeAllFromClassPath = new HostedOptionKey<>(false);
13131323

1324+
@Option(help = "Include all classes, methods, fields, and resources for dynamic access for the whole classpath", type = OptionType.Debug) //
1325+
public static final HostedOptionKey<Boolean> EnableDynamicAccess = new HostedOptionKey<>(false);
1326+
13141327
public static boolean includeAll() {
13151328
return IncludeAllFromModule.hasBeenSet() || IncludeAllFromPath.hasBeenSet() || IncludeAllFromPackage.hasBeenSet() || IncludeAllFromClassPath.hasBeenSet();
13161329
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/configure/LegacyReflectionConfigurationParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected void parseClass(EconomicMap<String, Object> data) {
9191

9292
C queryCondition = isType ? conditionResolver.alwaysTrue() : condition;
9393
T clazz = result.get();
94-
delegate.registerType(conditionResult.get(), clazz);
94+
delegate.registerType(conditionResult.get(), clazz, false);
9595

9696
registerIfNotDefault(data, false, clazz, "allDeclaredConstructors", () -> delegate.registerDeclaredConstructors(condition, false, clazz));
9797
registerIfNotDefault(data, false, clazz, "allPublicConstructors", () -> delegate.registerPublicConstructors(condition, false, clazz));

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/configure/ReflectionConfigurationParserDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface ReflectionConfigurationParserDelegate<C, T> {
3232

3333
TypeResult<T> resolveType(C condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives);
3434

35-
void registerType(C condition, T type);
35+
void registerType(C condition, T type, boolean registerMetadata);
3636

3737
void registerPublicClasses(C condition, T type);
3838

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/configure/ReflectionMetadataParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected void parseClass(EconomicMap<String, Object> data) {
8484

8585
C queryCondition = conditionResolver.alwaysTrue();
8686
T clazz = result.get();
87-
delegate.registerType(conditionResult.get(), clazz);
87+
delegate.registerType(conditionResult.get(), clazz, true);
8888

8989
delegate.registerDeclaredClasses(queryCondition, clazz);
9090
delegate.registerRecordComponents(queryCondition, clazz);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public void collectResources(ResourceCollector resourceCollector) {
116116

117117
/* Collect remaining resources from classpath */
118118
classLoaderSupport.classpath().stream().parallel().forEach(classpathFile -> {
119-
boolean includeCurrent = classLoaderSupport.getJavaPathsToInclude().contains(classpathFile) || classLoaderSupport.includeAllFromClassPath();
119+
boolean includeCurrent = classLoaderSupport.getPathsToInclude().contains(classpathFile) || classLoaderSupport.includeAllFromClassPath() ||
120+
classLoaderSupport.getClassPathEntriesToEnableDynamicAccess().contains(classpathFile) || classLoaderSupport.isEnableDynamicAccessForClassPath();
120121
try {
121122
if (Files.isDirectory(classpathFile)) {
122123
scanDirectory(classpathFile, resourceCollector, includeCurrent);
@@ -132,7 +133,8 @@ public void collectResources(ResourceCollector resourceCollector) {
132133
private void collectResourceFromModule(ResourceCollector resourceCollector, ResourceLookupInfo info) {
133134
ModuleReference moduleReference = info.resolvedModule.reference();
134135
try (ModuleReader moduleReader = moduleReference.open()) {
135-
boolean includeCurrent = classLoaderSupport.getJavaModuleNamesToInclude().contains(info.resolvedModule().name());
136+
boolean includeCurrent = classLoaderSupport.getModuleNamesToInclude().contains(info.resolvedModule().name()) ||
137+
classLoaderSupport.getModuleNamesToEnableDynamicAccess().contains(info.resolvedModule().name());
136138
List<ConditionalResource> resourcesFound = new ArrayList<>();
137139
moduleReader.list().forEach(resourceName -> {
138140
var conditionsWithOrigins = shouldIncludeEntry(info.module, resourceCollector, resourceName, moduleReference.location().orElse(null), includeCurrent);

0 commit comments

Comments
 (0)