24
24
*/
25
25
package com .oracle .svm .hosted .dynamicaccessinference ;
26
26
27
- import java .lang .invoke .MethodType ;
28
27
import java .lang .reflect .Field ;
29
- import java .lang .reflect .InvocationHandler ;
30
- import java .util .function .BooleanSupplier ;
31
- import java .util .function .Predicate ;
32
28
33
- import com .oracle .svm .core .ParsingReason ;
34
29
import org .graalvm .nativeimage .ImageSingletons ;
35
30
import org .graalvm .nativeimage .hosted .RuntimeReflection ;
36
31
43
38
44
39
import jdk .graal .compiler .options .Option ;
45
40
import jdk .graal .compiler .options .OptionStability ;
46
- import jdk .vm .ci .meta .ResolvedJavaMethod ;
47
-
48
- /**
49
- * Feature which enables a graph IR optimization independent analysis of compile-time inferrable
50
- * method invocations which require dynamic access and would otherwise require a manual reachability
51
- * registration.
52
- * <p>
53
- * The targeted methods are:
54
- * <ul>
55
- * <li>{@link java.lang.Class#forName(String)}</li>
56
- * <li>{@link java.lang.Class#forName(String, boolean, ClassLoader)}</li>
57
- * <li>{@link java.lang.Class#getField(String)}</li>
58
- * <li>{@link java.lang.Class#getDeclaredField(String)}</li>
59
- * <li>{@link java.lang.Class#getConstructor(Class[])}</li>
60
- * <li>{@link java.lang.Class#getDeclaredConstructor(Class[])}</li>
61
- * <li>{@link java.lang.Class#getMethod(String, Class[])}</li>
62
- * <li>{@link java.lang.Class#getDeclaredMethod(String, Class[])}</li>
63
- * <li>{@link java.lang.Class#getFields()}</li>
64
- * <li>{@link java.lang.Class#getDeclaredFields()}</li>
65
- * <li>{@link java.lang.Class#getConstructors()}</li>
66
- * <li>{@link java.lang.Class#getDeclaredConstructors()}</li>
67
- * <li>{@link java.lang.Class#getMethods()}</li>
68
- * <li>{@link java.lang.Class#getDeclaredMethods()}</li>
69
- * <li>{@link java.lang.Class#getClasses()}</li>
70
- * <li>{@link java.lang.Class#getDeclaredClasses()}</li>
71
- * <li>{@link java.lang.Class#getNestMembers()}</li>
72
- * <li>{@link java.lang.Class#getPermittedSubclasses()}</li>
73
- * <li>{@link java.lang.Class#getRecordComponents()}</li>
74
- * <li>{@link java.lang.Class#getSigners()}</li>
75
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findClass(String)}}</li>
76
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findVirtual(Class, String, MethodType)}}</li>
77
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findStatic(Class, String, MethodType)}}</li>
78
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findConstructor(Class, MethodType)}}</li>
79
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findGetter(Class, String, Class)}}</li>
80
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findStaticGetter(Class, String, Class)}}</li>
81
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findSetter(Class, String, Class)}}</li>
82
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findStaticSetter(Class, String, Class)}}</li>
83
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findVarHandle(Class, String, Class)}}</li>
84
- * <li>{@link java.lang.invoke.MethodHandles.Lookup#findStaticVarHandle(Class, String, Class)}}</li>
85
- * <li>{@link java.lang.Class#getResource(String)}</li>
86
- * <li>{@link java.lang.Class#getResourceAsStream(String)}</li>
87
- * <li>{@link java.io.ObjectInputFilter.Config#createFilter(String)}</li>
88
- * <li>{@link java.lang.reflect.Proxy#newProxyInstance(ClassLoader, Class[], InvocationHandler)}</li>
89
- * </ul>
90
- */
41
+
91
42
@ AutomaticallyRegisteredFeature
92
43
public class StrictDynamicAccessInferenceFeature implements InternalFeature {
93
44
@@ -112,6 +63,10 @@ public static boolean isActive() {
112
63
return Options .StrictDynamicAccessInference .getValue () != Options .Mode .Disable ;
113
64
}
114
65
66
+ public static boolean isEnforced () {
67
+ return Options .StrictDynamicAccessInference .getValue () == Options .Mode .Enforce ;
68
+ }
69
+
115
70
@ Override
116
71
public boolean isInConfiguration (IsInConfigurationAccess access ) {
117
72
return isActive ();
@@ -127,9 +82,6 @@ public void afterRegistration(AfterRegistrationAccess access) {
127
82
128
83
ImageSingletons .add (ConstantExpressionRegistry .class , registry );
129
84
ImageSingletons .add (StrictDynamicAccessInferenceSupport .class , support );
130
-
131
- cacheMode (Options .StrictDynamicAccessInference .getValue ());
132
- cacheRegistry (registry );
133
85
}
134
86
135
87
@ Override
@@ -167,47 +119,4 @@ public void afterAnalysis(AfterAnalysisAccess access) {
167
119
*/
168
120
ConstantExpressionRegistry .singleton ().seal ();
169
121
}
170
-
171
- private static void cacheMode (Options .Mode mode ) {
172
- cachedMode = mode ;
173
- }
174
-
175
- private static void cacheRegistry (ConstantExpressionRegistry registry ) {
176
- cachedRegistry = registry ;
177
- }
178
-
179
- private static Options .Mode cachedMode = Options .Mode .Disable ;
180
- private static ConstantExpressionRegistry cachedRegistry = null ;
181
-
182
- /**
183
- * Utility method which attempts to infer {@code targetMethod} according to the {@code Disable},
184
- * {@code Warn} and {@code Enforce} options of {@code StrictConstantAnalysis}.
185
- */
186
- public static boolean tryToInfer (ParsingReason reason , Predicate <ConstantExpressionRegistry > strictModeRoutine , BooleanSupplier graphModeRoutine , ResolvedJavaMethod targetMethod ,
187
- Predicate <ResolvedJavaMethod > strictModeTarget ) {
188
- /*
189
- * Do not restrict the folding of reflective calls if not building graphs for the analysis.
190
- */
191
- if (!reason .duringAnalysis () || reason == ParsingReason .JITCompilation ) {
192
- return graphModeRoutine .getAsBoolean ();
193
- }
194
- boolean isTarget = strictModeTarget .test (targetMethod );
195
- if (cachedMode != Options .Mode .Disable && isTarget ) {
196
- if (strictModeRoutine .test (cachedRegistry )) {
197
- return true ;
198
- }
199
- }
200
- if (cachedMode != Options .Mode .Enforce || !isTarget ) {
201
- return graphModeRoutine .getAsBoolean ();
202
- }
203
- return false ;
204
- }
205
-
206
- /**
207
- * Utility method which attempts to infer {@code targetMethod} according to the {@code Disable},
208
- * {@code Warn} and {@code Enforce} options of {@code StrictConstantAnalysis}.
209
- */
210
- public static boolean tryToInfer (ParsingReason reason , Predicate <ConstantExpressionRegistry > strictModeRoutine , BooleanSupplier graphModeRoutine ) {
211
- return tryToInfer (reason , strictModeRoutine , graphModeRoutine , null , (method ) -> true );
212
- }
213
122
}
0 commit comments