Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the constant reflection analysis used by Native Image is optimization dependent. This can lead to unexpected results during image run-time when using reflection. For example, the
Class.forName
call in the following snippet will be folded by the analysis:However, adding a simple printing statement to
isEven
or toggling different optimizations during build-time can cause the method to be non-inlinable andClass.forName
call won't be folded:In order to prevent this behavior, we can run a constant reflection analysis directly on the bytecode used as input to Native Image.
This PR implements a JVMTI agent (
com.oracle.svm.reflectionagent.NativeImageReflectionAgent
) which intercepts user provided class files, analyzes them for constant reflection usage and marks such reflective methods as constant by redirecting them to their counterparts in theorg.graalvm.nativeimage.impl.reflectiontags.ConstantTags
) class. If a reflective method invocation in a user provided class gets folded bycom.oracle.svm.hosted.snippets.ReflectionPlugins
without it being marked as constant by the agent, the user gets a warning during build-time:To enable the agent and warnings, use the
-H:+EnableStrictReflection
. In addition, three new options are provided for more accurate logging of constant reflection folding done byReflectionPlugins
:-H:ReflectionPluginTraceLocation=<log_location>
- Location for the log file. If not set, the log isn't created.-H:ReflectionPluginTraceFormat=json|plain
- Specify the format of the location log. Default value is json. IfReflectionPluginTraceLocation
isn't set, this option has no effect.-H:+ReflectionPluginTraceUserOnly
- Log only the constant folding which occurred in user classes. Default value is true. IfReflectionPluginTraceLocation
isn't set, this option has no effect.