changeset 2245:f5f0872a1b84

8028389: NullPointerException compiling annotation values that have bodies Summary: Made sure anonymous class declarations inside class- and package-level annotations are properly entered. Reviewed-by: jfranck
author alundblad
date Fri, 10 Jan 2014 12:47:15 +0100
parents c1c20e618930
children 7163a6b6d8ea
files src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/comp/Resolve.java src/share/classes/com/sun/tools/javac/tree/TreeInfo.java test/tools/javac/annotations/neg/AnonSubclass.java test/tools/javac/annotations/neg/AnonSubclass.out test/tools/javac/annotations/neg/pkg/AnonSubclassOnPkg.java test/tools/javac/annotations/neg/pkg/package-info.java test/tools/javac/annotations/neg/pkg/package-info.out
diffstat 8 files changed, 85 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Jan 10 11:31:09 2014 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Jan 10 12:47:15 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -850,9 +850,18 @@
     }
 
     public void visitClassDef(JCClassDecl tree) {
-        // Local classes have not been entered yet, so we need to do it now:
-        if ((env.info.scope.owner.kind & (VAR | MTH)) != 0)
+        // Local and anonymous classes have not been entered yet, so we need to
+        // do it now.
+        if ((env.info.scope.owner.kind & (VAR | MTH)) != 0) {
             enter.classEnter(tree, env);
+        } else {
+            // If this class declaration is part of a class level annotation,
+            // as in @MyAnno(new Object() {}) class MyClass {}, enter it in
+            // order to simplify later steps and allow for sensible error
+            // messages.
+            if (env.tree.hasTag(NEWCLASS) && TreeInfo.isInAnnotation(env, tree))
+                enter.classEnter(tree, env);
+        }
 
         ClassSymbol c = tree.sym;
         if (c == null) {
--- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri Jan 10 11:31:09 2014 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri Jan 10 12:47:15 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -269,7 +269,7 @@
      *  the one of its outer environment
      */
     protected static boolean isStatic(Env<AttrContext> env) {
-        return env.info.staticLevel > env.outer.info.staticLevel;
+        return env.outer != null && env.info.staticLevel > env.outer.info.staticLevel;
     }
 
     /** An environment is an "initializer" if it is a constructor or
--- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Fri Jan 10 11:31:09 2014 +0100
+++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java	Fri Jan 10 12:47:15 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
 
 
 import com.sun.source.tree.Tree;
+import com.sun.source.util.TreePath;
 import com.sun.tools.javac.code.*;
 import com.sun.tools.javac.comp.AttrContext;
 import com.sun.tools.javac.comp.Env;
@@ -342,6 +343,18 @@
         return (lit.typetag == BOT);
     }
 
+    /** Return true iff this tree is a child of some annotation. */
+    public static boolean isInAnnotation(Env<?> env, JCTree tree) {
+        TreePath tp = TreePath.getPath(env.toplevel, tree);
+        if (tp != null) {
+            for (Tree t : tp) {
+                if (t.getKind() == Tree.Kind.ANNOTATION)
+                    return true;
+            }
+        }
+        return false;
+    }
+
     public static String getCommentText(Env<?> env, JCTree tree) {
         DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
                 ? ((JCCompilationUnit) tree).docComments
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/neg/AnonSubclass.java	Fri Jan 10 12:47:15 2014 +0100
@@ -0,0 +1,13 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8028389
+ * @summary javac should output a proper error message when given something
+ * like new Object(){} as annotation argument.
+ *
+ * @compile/fail/ref=AnonSubclass.out -XDrawDiagnostics AnonSubclass.java
+ */
+
+@AnonSubclass(new Object(){})
+@interface AnonSubclass {
+    String value();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/neg/AnonSubclass.out	Fri Jan 10 12:47:15 2014 +0100
@@ -0,0 +1,2 @@
+AnonSubclass.java:10:15: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: java.lang.Object, java.lang.String)
+1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/neg/pkg/AnonSubclassOnPkg.java	Fri Jan 10 12:47:15 2014 +0100
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package pkg;
+
+@interface AnonSubclassOnPkg {
+    String value();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/neg/pkg/package-info.java	Fri Jan 10 12:47:15 2014 +0100
@@ -0,0 +1,12 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8028389
+ * @summary javac should output a proper error message when given something
+ * like new Object(){} as annotation argument.
+ *
+ * @compile AnonSubclassOnPkg.java
+ * @compile/fail/ref=package-info.out -XDrawDiagnostics package-info.java
+ */
+
+@AnonSubclassOnPkg(new Object(){})
+package pkg;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/neg/pkg/package-info.out	Fri Jan 10 12:47:15 2014 +0100
@@ -0,0 +1,2 @@
+package-info.java:11:20: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: java.lang.Object, java.lang.String)
+1 error