RFR: JDK-8062359: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
Joel Borggrén-Franck
joel.franck at oracle.com
Wed Nov 19 16:13:40 UTC 2014
Hi,
Thanks for the reproducer Michael. Jan minimized it and we now have both a test and a fix.
Problem is when an anonymous inner class extends an unresolvable class an anonymous inner class inside it won’t get attributed. Long term we might fix this by trying harder to attribute the innermost class but a short term fix is to properly check that tree.clazz and tree.clazz.type aren’t null.
Patch with test below and attached.
cheers
/Joel
diff -r f62d01419621 src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java Wed Nov 19 13:46:04 2014 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java Wed Nov 19 17:01:57 2014 +0100
@@ -4486,14 +4486,15 @@
super.visitTypeTest(tree);
}
public void visitNewClass(JCNewClass tree) {
- if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
- checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
- tree.clazz.type.tsym);
- }
- if (tree.def != null) {
- checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym);
- }
- if (tree.clazz.type != null) {
+ if (tree.clazz != null && tree.clazz.type != null) {
+ if (tree.clazz.hasTag(ANNOTATED_TYPE)) {
+ checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations,
+ tree.clazz.type.tsym);
+ }
+ if (tree.def != null) {
+ checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym);
+ }
+
validateAnnotatedType(tree.clazz, tree.clazz.type);
}
super.visitNewClass(tree);
diff -r f62d01419621 test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.java Wed Nov 19 17:01:57 2014 +0100
@@ -0,0 +1,17 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8062359
+ * @summary NullPointerException in Attr when type-annotating an anonymous
+ * inner class in an unresolvable class
+ * @compile/fail/ref=UnresolvableClassNPEInAttrTest.out -XDrawDiagnostics UnresolvableClassNPEInAttrTest.java
+ */
+
+public class UnresolvableClassNPEInAttrTest {
+ public static void main(String[] args) {
+ new Undefined() {
+ void test() {
+ new Object() {};
+ }
+ };
+ }
+}
diff -r f62d01419621 test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.out
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/8062359/UnresolvableClassNPEInAttrTest.out Wed Nov 19 17:01:57 2014 +0100
@@ -0,0 +1,2 @@
+UnresolvableClassNPEInAttrTest.java:11:13: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, UnresolvableClassNPEInAttrTest, null)
+1 error
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 8062359.patch
Type: application/octet-stream
Size: 2598 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20141119/84365bb7/8062359.patch>
-------------- next part --------------
> On 18 Nov 2014, at 11:41, Joel Borggrén-Franck <joel.franck at oracle.com> wrote:
>
> Hi,
>
> Here is a proposed fix for [1]: javac Attr crashes with NPE in TypeAnnotationsValidator visitNewClass
>
> This is for 9 but I will propose an identical backport to 8u as well. Patch is very small:
>
> [1] https://bugs.openjdk.java.net/browse/JDK-8062359
More information about the compiler-dev
mailing list