43
43
import org .graalvm .compiler .options .Option ;
44
44
import org .graalvm .nativeimage .ImageSingletons ;
45
45
46
+ import com .oracle .svm .core .annotate .Delete ;
46
47
import com .oracle .svm .core .meta .SubstrateObjectConstant ;
47
48
import com .oracle .svm .core .option .HostedOptionKey ;
48
49
import com .oracle .svm .core .reflect .ReflectionPluginExceptions ;
49
50
import com .oracle .svm .core .util .VMError ;
50
51
import com .oracle .svm .hosted .ImageClassLoader ;
52
+ import com .oracle .svm .hosted .NativeImageOptions ;
51
53
52
54
import jdk .vm .ci .meta .JavaConstant ;
53
55
import jdk .vm .ci .meta .JavaKind ;
56
+ import jdk .vm .ci .meta .MetaAccessProvider ;
54
57
import jdk .vm .ci .meta .ResolvedJavaMethod ;
55
58
56
59
public class ReflectionPlugins {
@@ -171,12 +174,12 @@ private static boolean processForName(GraphBuilderContext b, ResolvedJavaMethod
171
174
String className = snippetReflection .asObject (String .class , name .asJavaConstant ());
172
175
Class <?> clazz = imageClassLoader .findClassByName (className , false );
173
176
if (clazz == null ) {
174
- if (shouldNotIntrinsify (analysis , hosted , throwClassNotFoundExceptionMethod )) {
177
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), throwClassNotFoundExceptionMethod )) {
175
178
return false ;
176
179
}
177
180
throwClassNotFoundException (b , targetMethod , className );
178
181
} else {
179
- if (shouldNotIntrinsify (analysis , hosted , clazz )) {
182
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), clazz )) {
180
183
return false ;
181
184
}
182
185
JavaConstant hub = b .getConstantReflection ().asJavaClass (b .getMetaAccess ().lookupJavaType (clazz ));
@@ -196,12 +199,12 @@ private static boolean processGetField(GraphBuilderContext b, ResolvedJavaMethod
196
199
String target = clazz .getTypeName () + "." + fieldName ;
197
200
try {
198
201
Field field = declared ? clazz .getDeclaredField (fieldName ) : clazz .getField (fieldName );
199
- if (shouldNotIntrinsify (analysis , hosted , field )) {
202
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), field )) {
200
203
return false ;
201
204
}
202
205
pushConstant (b , targetMethod , snippetReflection .forObject (field ), target );
203
206
} catch (NoSuchFieldException e ) {
204
- if (shouldNotIntrinsify (analysis , hosted , throwNoSuchFieldExceptionMethod )) {
207
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), throwNoSuchFieldExceptionMethod )) {
205
208
return false ;
206
209
}
207
210
throwNoSuchFieldException (b , targetMethod , target );
@@ -223,12 +226,12 @@ private static boolean processGetMethod(GraphBuilderContext b, ResolvedJavaMetho
223
226
String target = clazz .getTypeName () + "." + methodName + "(" + Stream .of (paramTypes ).map (Class ::getTypeName ).collect (Collectors .joining (", " )) + ")" ;
224
227
try {
225
228
Method method = declared ? clazz .getDeclaredMethod (methodName , paramTypes ) : clazz .getMethod (methodName , paramTypes );
226
- if (shouldNotIntrinsify (analysis , hosted , method )) {
229
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), method )) {
227
230
return false ;
228
231
}
229
232
pushConstant (b , targetMethod , snippetReflection .forObject (method ), target );
230
233
} catch (NoSuchMethodException e ) {
231
- if (shouldNotIntrinsify (analysis , hosted , throwNoSuchMethodExceptionMethod )) {
234
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), throwNoSuchMethodExceptionMethod )) {
232
235
return false ;
233
236
}
234
237
throwNoSuchMethodException (b , targetMethod , target );
@@ -253,12 +256,12 @@ private static boolean processGetConstructor(GraphBuilderContext b, ResolvedJava
253
256
String target = clazz .getTypeName () + ".<init>(" + Stream .of (paramTypes ).map (Class ::getTypeName ).collect (Collectors .joining (", " )) + ")" ;
254
257
try {
255
258
Constructor <?> constructor = declared ? clazz .getDeclaredConstructor (paramTypes ) : clazz .getConstructor (paramTypes );
256
- if (shouldNotIntrinsify (analysis , hosted , constructor )) {
259
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), constructor )) {
257
260
return false ;
258
261
}
259
262
pushConstant (b , targetMethod , snippetReflection .forObject (constructor ), target );
260
263
} catch (NoSuchMethodException e ) {
261
- if (shouldNotIntrinsify (analysis , hosted , throwNoSuchMethodExceptionMethod )) {
264
+ if (shouldNotIntrinsify (analysis , hosted , b . getMetaAccess (), throwNoSuchMethodExceptionMethod )) {
262
265
return false ;
263
266
}
264
267
throwNoSuchMethodException (b , targetMethod , target );
@@ -271,12 +274,17 @@ private static boolean processGetConstructor(GraphBuilderContext b, ResolvedJava
271
274
}
272
275
273
276
/** Check if the element should be intrinsified. */
274
- private static boolean shouldNotIntrinsify (boolean analysis , boolean hosted , Object element ) {
277
+ private static boolean shouldNotIntrinsify (boolean analysis , boolean hosted , MetaAccessProvider metaAccess , Object element ) {
275
278
if (!hosted ) {
276
279
/* We are analyzing the static initializers and should always intrinsify. */
277
280
return false ;
278
281
}
279
282
if (analysis ) {
283
+ if (NativeImageOptions .ReportUnsupportedElementsAtRuntime .getValue ()) {
284
+ if (element instanceof Field && metaAccess .lookupJavaField ((Field ) element ).isAnnotationPresent (Delete .class )) {
285
+ return true ; /* Fail during the reflective field lookup at runtime. */
286
+ }
287
+ }
280
288
/* We are during analysis, we should intrinsify and mark the objects as intrinsified. */
281
289
ImageSingletons .lookup (ReflectionPluginRegistry .class ).add (element );
282
290
return false ;
0 commit comments