Skip to content

Commit 7b1e402

Browse files
raphwJoel Borggrén-Franck
authored andcommitted
8266598: Exception values for AnnotationTypeMismatchException are not always informative
Reviewed-by: jfranck
1 parent 13d6180 commit 7b1e402

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/java.base/share/classes/sun/reflect/annotation/AnnotationParser.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,18 @@ public static Object parseMemberValue(Class<?> memberType,
356356

357357
if (result == null) {
358358
result = new AnnotationTypeMismatchExceptionProxy(
359-
memberType.getClass().getName());
359+
Proxy.isProxyClass(memberType)
360+
? memberType.getInterfaces()[0].getName()
361+
: memberType.getName());
360362
} else if (!(result instanceof ExceptionProxy) &&
361363
!memberType.isInstance(result)) {
362-
result = new AnnotationTypeMismatchExceptionProxy(
363-
result.getClass() + "[" + result + "]");
364+
if (result instanceof Annotation) {
365+
result = new AnnotationTypeMismatchExceptionProxy(
366+
result.toString());
367+
} else {
368+
result = new AnnotationTypeMismatchExceptionProxy(
369+
result.getClass().getName() + "[" + result + "]");
370+
}
364371
}
365372
return result;
366373
}
@@ -463,12 +470,9 @@ private static Object parseEnumValue(Class<? extends Enum> enumType, ByteBuffer
463470
String typeName = constPool.getUTF8At(typeNameIndex);
464471
int constNameIndex = buf.getShort() & 0xFFFF;
465472
String constName = constPool.getUTF8At(constNameIndex);
466-
if (!enumType.isEnum()) {
467-
return new AnnotationTypeMismatchExceptionProxy(
468-
typeName + "." + constName);
469-
} else if (enumType != parseSig(typeName, container)) {
473+
if (!enumType.isEnum() || enumType != parseSig(typeName, container)) {
470474
return new AnnotationTypeMismatchExceptionProxy(
471-
typeName + "." + constName);
475+
typeName.substring(1, typeName.length() - 1).replace('/', '.') + "." + constName);
472476
}
473477

474478
try {
@@ -755,7 +759,7 @@ private static Object parseArrayElements(Object[] result,
755759
*/
756760
private static ExceptionProxy exceptionProxy(int tag) {
757761
return new AnnotationTypeMismatchExceptionProxy(
758-
"Array with component tag: " + tag);
762+
"Array with component tag: " + (tag == 0 ? "0" : (char) tag));
759763
}
760764

761765
/**

test/jdk/java/lang/annotation/AnnotationTypeMismatchException/AnnotationTypeMismatchTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8228988
26+
* @bug 8228988 8266598
2727
* @summary An annotation-typed property of an annotation that is represented as an
2828
* incompatible property of another type should yield an AnnotationTypeMismatchException.
2929
* @modules java.base/jdk.internal.org.objectweb.asm
@@ -59,7 +59,11 @@ public static void main(String[] args) throws Exception {
5959
Value value = sample.value();
6060
throw new IllegalStateException("Found value: " + value);
6161
} catch (AnnotationTypeMismatchException e) {
62-
// correct
62+
if (!e.element().getName().equals("value")) {
63+
throw new IllegalStateException("Unexpected element: " + e.element());
64+
} else if (!e.foundType().equals(AnEnum.class.getName() + "." + AnEnum.VALUE.name())) {
65+
throw new IllegalStateException("Unexpected type: " + e.foundType());
66+
}
6367
}
6468
}
6569

test/jdk/java/lang/annotation/AnnotationTypeMismatchException/EnumTypeMismatchTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8228988
26+
* @bug 8228988 8266598
2727
* @summary An enumeration-typed property of an annotation that is represented as an
2828
* incompatible property of another type should yield an AnnotationTypeMismatchException.
2929
* @modules java.base/jdk.internal.org.objectweb.asm
@@ -59,7 +59,11 @@ public static void main(String[] args) throws Exception {
5959
AnEnum value = sample.value();
6060
throw new IllegalStateException("Found value: " + value);
6161
} catch (AnnotationTypeMismatchException e) {
62-
// correct
62+
if (!e.element().getName().equals("value")) {
63+
throw new IllegalStateException("Unexpected element: " + e.element());
64+
} else if (!e.foundType().equals("@" + AnAnnotation.class.getName() + "(" + AnEnum.VALUE.name() + ")")) {
65+
throw new IllegalStateException("Unexpected type: " + e.foundType());
66+
}
6367
}
6468
}
6569

0 commit comments

Comments
 (0)