Skip to content

Commit 3a131e2

Browse files
committed
[GR-52021] Include targetElement in AnnotationExtractionError.
PullRequest: graal/16990
2 parents ca31e94 + 9f3d026 commit 3a131e2

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationMetadata.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
import java.lang.reflect.Method;
3131
import java.nio.ByteBuffer;
3232

33-
import jdk.graal.compiler.debug.GraalError;
34-
3533
import com.oracle.svm.util.ReflectionUtil;
3634

35+
import jdk.graal.compiler.debug.GraalError;
3736
import jdk.internal.reflect.ConstantPool;
3837
import sun.reflect.annotation.AnnotationParser;
3938
import sun.reflect.annotation.TypeNotPresentExceptionProxy;
@@ -42,12 +41,12 @@ public class AnnotationMetadata {
4241

4342
@SuppressWarnings("serial")
4443
static final class AnnotationExtractionError extends Error {
45-
AnnotationExtractionError(Throwable cause) {
46-
super(cause);
44+
AnnotationExtractionError(Object targetElement, Throwable cause) {
45+
super("Failed to process '%s': %s".formatted(targetElement, cause), cause);
4746
}
4847

49-
AnnotationExtractionError(String message) {
50-
super(message);
48+
AnnotationExtractionError(Object targetElement, String message) {
49+
super("Failed to process '%s': %s".formatted(targetElement, message));
5150
}
5251
}
5352

@@ -76,9 +75,9 @@ static Object extractType(ByteBuffer buf, ConstantPool cp, Class<?> container, b
7675
if (targetException instanceof LinkageError || targetException instanceof TypeNotPresentException) {
7776
return new TypeNotPresentExceptionProxy(signature, targetException);
7877
}
79-
throw new AnnotationExtractionError(e);
78+
throw new AnnotationExtractionError(signature, e);
8079
} catch (ReflectiveOperationException e) {
81-
throw new AnnotationExtractionError(e);
80+
throw new AnnotationExtractionError(signature, e);
8281
}
8382
return type;
8483
}
@@ -103,7 +102,7 @@ static Object createAnnotationTypeMismatchExceptionProxy(String message) {
103102
try {
104103
return annotationTypeMismatchExceptionProxyConstructor.newInstance(message);
105104
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
106-
throw new AnnotationExtractionError(e);
105+
throw new AnnotationExtractionError(message, e);
107106
}
108107
}
109108
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationPrimitiveValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static AnnotationPrimitiveValue extract(ByteBuffer buf, ConstantPool cp, char ta
6666
value = cp.getIntAt(constIndex) != 0;
6767
break;
6868
default:
69-
throw new AnnotationMetadata.AnnotationExtractionError("Invalid annotation encoding. Unknown tag " + tag);
69+
throw new AnnotationMetadata.AnnotationExtractionError(tag, "Invalid annotation encoding. Unknown tag");
7070
}
7171
assert Wrapper.forWrapperType(value.getClass()).basicTypeChar() == tag;
7272
return new AnnotationPrimitiveValue(tag, value);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationValue.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.function.BiConsumer;
3737

3838
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
39-
4039
import jdk.internal.reflect.ConstantPool;
4140
import jdk.vm.ci.meta.JavaConstant;
4241
import sun.reflect.annotation.AnnotationParser;
@@ -95,7 +94,7 @@ static AnnotationValue forAnnotationFormatException() {
9594
try {
9695
memberValue = AnnotationMemberValue.from(annotationType.memberTypes().get(memberName), memberAccessor.invoke(annotation));
9796
} catch (IllegalAccessException | InvocationTargetException e) {
98-
throw new AnnotationMetadata.AnnotationExtractionError(e);
97+
throw new AnnotationMetadata.AnnotationExtractionError(annotation, e);
9998
}
10099
Object memberDefault = annotationType.memberDefaults().get(memberName);
101100
if (!memberValue.equals(memberDefault)) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/SubstrateAnnotationExtractor.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public boolean hasAnnotation(AnnotatedElement element, Class<? extends Annotatio
162162
@SuppressWarnings("unchecked")
163163
@Override
164164
public Class<? extends Annotation>[] getAnnotationTypes(AnnotatedElement element) {
165-
return Arrays.stream(getAnnotationData(element, false)).map(AnnotationValue::getType).filter(t -> t != null).toArray(Class[]::new);
165+
return Arrays.stream(getAnnotationData(element, false)).map(AnnotationValue::getType).filter(Objects::nonNull).toArray(Class[]::new);
166166
}
167167

168168
public AnnotationValue[] getDeclaredAnnotationData(AnnotatedElement element) {
@@ -175,8 +175,7 @@ private AnnotationValue[] getAnnotationData(AnnotatedElement element, boolean de
175175
cur = ((WrappedElement) cur).getWrapped();
176176
}
177177
AnnotationValue[] result = NO_ANNOTATIONS;
178-
while (cur instanceof AnnotationWrapper) {
179-
AnnotationWrapper wrapper = (AnnotationWrapper) cur;
178+
while (cur instanceof AnnotationWrapper wrapper) {
180179
result = concat(result, wrapper.getInjectedAnnotations());
181180
cur = wrapper.getAnnotationRoot();
182181
}
@@ -201,11 +200,10 @@ private static AnnotationValue[] concat(AnnotationValue[] a1, AnnotationValue[]
201200
}
202201

203202
private AnnotationValue[] getAnnotationDataFromRoot(AnnotatedElement rootElement) {
204-
if (!(rootElement instanceof Class<?>)) {
203+
if (!(rootElement instanceof Class<?> clazz)) {
205204
return getDeclaredAnnotationDataFromRoot(rootElement);
206205
}
207206

208-
Class<?> clazz = (Class<?>) rootElement;
209207
AnnotationValue[] existing = annotationCache.get(clazz);
210208
if (existing != null) {
211209
return existing;
@@ -355,10 +353,10 @@ private static byte[] getRawAnnotations(AnnotatedElement rootElement) {
355353
} else if (recordComponentClass != null && recordComponentClass.isInstance(rootElement)) {
356354
return (byte[]) recordComponentAnnotations.get(rootElement);
357355
} else {
358-
throw new AnnotationExtractionError("Unexpected annotated element type: " + rootElement.getClass());
356+
throw new AnnotationExtractionError(rootElement, "Unexpected annotated element type: " + rootElement.getClass());
359357
}
360358
} catch (InvocationTargetException | IllegalAccessException e) {
361-
throw new AnnotationExtractionError(e);
359+
throw new AnnotationExtractionError(rootElement, e);
362360
}
363361
}
364362

@@ -369,10 +367,10 @@ private static byte[] getRawParameterAnnotations(Executable rootElement) {
369367
} else if (rootElement instanceof Constructor<?>) {
370368
return (byte[]) constructorParameterAnnotations.get(rootElement);
371369
} else {
372-
throw new AnnotationExtractionError("Unexpected annotated element type: " + rootElement.getClass());
370+
throw new AnnotationExtractionError(rootElement, "Unexpected annotated element type: " + rootElement.getClass());
373371
}
374372
} catch (IllegalAccessException e) {
375-
throw new AnnotationExtractionError(e);
373+
throw new AnnotationExtractionError(rootElement, e);
376374
}
377375
}
378376

@@ -387,18 +385,18 @@ private static byte[] getRawTypeAnnotations(AnnotatedElement rootElement) {
387385
} else if (recordComponentClass != null && recordComponentClass.isInstance(rootElement)) {
388386
return (byte[]) recordComponentTypeAnnotations.get(rootElement);
389387
} else {
390-
throw new AnnotationExtractionError("Unexpected annotated element type: " + rootElement.getClass());
388+
throw new AnnotationExtractionError(rootElement, "Unexpected annotated element type: " + rootElement.getClass());
391389
}
392390
} catch (InvocationTargetException | IllegalAccessException e) {
393-
throw new AnnotationExtractionError(e);
391+
throw new AnnotationExtractionError(rootElement, e);
394392
}
395393
}
396394

397395
private static byte[] getRawAnnotationDefault(Method method) {
398396
try {
399397
return (byte[]) methodAnnotationDefault.get(method);
400398
} catch (IllegalAccessException e) {
401-
throw new AnnotationExtractionError(e);
399+
throw new AnnotationExtractionError(method, e);
402400
}
403401
}
404402

@@ -407,7 +405,7 @@ private static ConstantPool getConstantPool(AnnotatedElement rootElement) {
407405
try {
408406
return (ConstantPool) classGetConstantPool.invoke(container);
409407
} catch (InvocationTargetException | IllegalAccessException e) {
410-
throw new AnnotationExtractionError(e);
408+
throw new AnnotationExtractionError(rootElement, e);
411409
}
412410
}
413411

@@ -422,10 +420,10 @@ private static Class<?> getContainer(AnnotatedElement rootElement) {
422420
try {
423421
return (Class<?>) recordComponentGetDeclaringRecord.invoke(rootElement);
424422
} catch (IllegalAccessException | InvocationTargetException e) {
425-
throw new AnnotationExtractionError(e);
423+
throw new AnnotationExtractionError(rootElement, e);
426424
}
427425
} else {
428-
throw new AnnotationExtractionError("Unexpected annotated element type: " + rootElement.getClass());
426+
throw new AnnotationExtractionError(rootElement, "Unexpected annotated element type: " + rootElement.getClass());
429427
}
430428
}
431429

@@ -443,14 +441,14 @@ private static AnnotatedElement unwrap(AnnotatedElement element) {
443441
private static AnnotatedElement findRoot(AnnotatedElement element) {
444442
assert !(element instanceof WrappedElement || element instanceof AnnotationWrapper);
445443
try {
446-
if (element instanceof ResolvedJavaType) {
447-
return OriginalClassProvider.getJavaClass((ResolvedJavaType) element);
448-
} else if (element instanceof ResolvedJavaMethod) {
449-
return OriginalMethodProvider.getJavaMethod((ResolvedJavaMethod) element);
450-
} else if (element instanceof ResolvedJavaField) {
451-
return OriginalFieldProvider.getJavaField((ResolvedJavaField) element);
452-
} else if (element instanceof Package) {
453-
return (Class<?>) packageGetPackageInfo.invoke(element);
444+
if (element instanceof ResolvedJavaType type) {
445+
return OriginalClassProvider.getJavaClass(type);
446+
} else if (element instanceof ResolvedJavaMethod method) {
447+
return OriginalMethodProvider.getJavaMethod(method);
448+
} else if (element instanceof ResolvedJavaField field) {
449+
return OriginalFieldProvider.getJavaField(field);
450+
} else if (element instanceof Package packageObject) {
451+
return (Class<?>) packageGetPackageInfo.invoke(packageObject);
454452
} else {
455453
return element;
456454
}
@@ -459,9 +457,9 @@ private static AnnotatedElement findRoot(AnnotatedElement element) {
459457
if (targetException instanceof LinkageError) {
460458
throw (LinkageError) targetException;
461459
}
462-
throw new AnnotationExtractionError(e);
460+
throw new AnnotationExtractionError(element, e);
463461
} catch (IllegalAccessException e) {
464-
throw new AnnotationExtractionError(e);
462+
throw new AnnotationExtractionError(element, e);
465463
}
466464
}
467465
}

0 commit comments

Comments
 (0)