Skip to content

Commit f47faf2

Browse files
raphwjddarcy
authored andcommitted
8228988: AnnotationParser throws NullPointerException on incompatible member type
Reviewed-by: darcy
1 parent d613459 commit f47faf2

3 files changed

Lines changed: 182 additions & 4 deletions

File tree

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,14 @@ public static Object parseMemberValue(Class<?> memberType,
358358
result = parseConst(tag, buf, constPool);
359359
}
360360

361-
if (!(result instanceof ExceptionProxy) &&
362-
!memberType.isInstance(result))
361+
if (result == null) {
362+
result = new AnnotationTypeMismatchExceptionProxy(
363+
memberType.getClass().getName());
364+
} else if (!(result instanceof ExceptionProxy) &&
365+
!memberType.isInstance(result)) {
363366
result = new AnnotationTypeMismatchExceptionProxy(
364367
result.getClass() + "[" + result + "]");
368+
}
365369
return result;
366370
}
367371

@@ -469,8 +473,10 @@ private static Object parseEnumValue(Class<? extends Enum> enumType, ByteBuffer
469473
String typeName = constPool.getUTF8At(typeNameIndex);
470474
int constNameIndex = buf.getShort() & 0xFFFF;
471475
String constName = constPool.getUTF8At(constNameIndex);
472-
473-
if (!typeName.endsWith(";")) {
476+
if (!enumType.isEnum()) {
477+
return new AnnotationTypeMismatchExceptionProxy(
478+
typeName + "." + constName);
479+
} else if (!typeName.endsWith(";")) {
474480
// support now-obsolete early jsr175-format class files.
475481
if (!enumType.getName().equals(typeName))
476482
return new AnnotationTypeMismatchExceptionProxy(
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8228988
27+
* @summary An annotation-typed property of an annotation that is represented as an
28+
* incompatible property of another type should yield an AnnotationTypeMismatchException.
29+
* @modules java.base/jdk.internal.org.objectweb.asm
30+
* @run main AnnotationTypeMismatchTest
31+
*/
32+
33+
import jdk.internal.org.objectweb.asm.AnnotationVisitor;
34+
import jdk.internal.org.objectweb.asm.ClassWriter;
35+
import jdk.internal.org.objectweb.asm.Opcodes;
36+
import jdk.internal.org.objectweb.asm.Type;
37+
38+
import java.lang.annotation.AnnotationTypeMismatchException;
39+
import java.lang.annotation.Retention;
40+
import java.lang.annotation.RetentionPolicy;
41+
42+
public class AnnotationTypeMismatchTest {
43+
44+
public static void main(String[] args) throws Exception {
45+
/*
46+
* @AnAnnotation(value = AnEnum.VALUE) // would now be: value = @Value
47+
* class Carrier { }
48+
*/
49+
ClassWriter writer = new ClassWriter(0);
50+
writer.visit(Opcodes.V1_8, 0, "sample/Carrier", null, Type.getInternalName(Object.class), null);
51+
AnnotationVisitor v = writer.visitAnnotation(Type.getDescriptor(AnAnnotation.class), true);
52+
v.visitEnum("value", Type.getDescriptor(AnEnum.class), "VALUE");
53+
writer.visitEnd();
54+
byte[] b = writer.toByteArray();
55+
ByteArrayClassLoader cl = new ByteArrayClassLoader(AnnotationTypeMismatchTest.class.getClassLoader());
56+
cl.init(b);
57+
AnAnnotation sample = cl.loadClass("sample.Carrier").getAnnotation(AnAnnotation.class);
58+
try {
59+
Value value = sample.value();
60+
throw new IllegalStateException("Found value: " + value);
61+
} catch (AnnotationTypeMismatchException e) {
62+
// correct
63+
}
64+
}
65+
66+
public enum AnEnum {
67+
VALUE
68+
}
69+
70+
@Retention(RetentionPolicy.RUNTIME)
71+
public @interface AnAnnotation {
72+
Value value() default @Value;
73+
}
74+
75+
public @interface Value { }
76+
77+
public static class ByteArrayClassLoader extends ClassLoader {
78+
79+
public ByteArrayClassLoader(ClassLoader parent) {
80+
super(parent);
81+
}
82+
83+
public void init(byte[] b) {
84+
defineClass("sample.Carrier", b, 0, b.length);
85+
}
86+
}
87+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8228988
27+
* @summary An enumeration-typed property of an annotation that is represented as an
28+
* incompatible property of another type should yield an AnnotationTypeMismatchException.
29+
* @modules java.base/jdk.internal.org.objectweb.asm
30+
* @run main EnumTypeMismatchTest
31+
*/
32+
33+
import jdk.internal.org.objectweb.asm.AnnotationVisitor;
34+
import jdk.internal.org.objectweb.asm.ClassWriter;
35+
import jdk.internal.org.objectweb.asm.Opcodes;
36+
import jdk.internal.org.objectweb.asm.Type;
37+
38+
import java.lang.annotation.AnnotationTypeMismatchException;
39+
import java.lang.annotation.Retention;
40+
import java.lang.annotation.RetentionPolicy;
41+
42+
public class EnumTypeMismatchTest {
43+
44+
public static void main(String[] args) throws Exception {
45+
/*
46+
* @AnAnnotation(value = @AnAnnotation) // would now be: value = AnEnum.VALUE
47+
* class Carrier { }
48+
*/
49+
ClassWriter writer = new ClassWriter(0);
50+
writer.visit(Opcodes.V1_8, 0, "sample/Carrier", null, Type.getInternalName(Object.class), null);
51+
AnnotationVisitor v = writer.visitAnnotation(Type.getDescriptor(AnAnnotation.class), true);
52+
v.visitAnnotation("value", Type.getDescriptor(AnAnnotation.class)).visitEnd();
53+
writer.visitEnd();
54+
byte[] b = writer.toByteArray();
55+
ByteArrayClassLoader cl = new ByteArrayClassLoader(EnumTypeMismatchTest.class.getClassLoader());
56+
cl.init(b);
57+
AnAnnotation sample = cl.loadClass("sample.Carrier").getAnnotation(AnAnnotation.class);
58+
try {
59+
AnEnum value = sample.value();
60+
throw new IllegalStateException("Found value: " + value);
61+
} catch (AnnotationTypeMismatchException e) {
62+
// correct
63+
}
64+
}
65+
66+
public enum AnEnum {
67+
VALUE
68+
}
69+
70+
@Retention(RetentionPolicy.RUNTIME)
71+
public @interface AnAnnotation {
72+
AnEnum value() default AnEnum.VALUE;
73+
}
74+
75+
public static class ByteArrayClassLoader extends ClassLoader {
76+
77+
public ByteArrayClassLoader(ClassLoader parent) {
78+
super(parent);
79+
}
80+
81+
void init(byte[] b) {
82+
defineClass("sample.Carrier", b, 0, b.length);
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)