Fri, 10 Jan 2014 12:47:15 +0100
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
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Jun 10 09:13:27 2015 +0200 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Fri Jan 10 12:47:15 2014 +0100 1.3 @@ -825,9 +825,18 @@ 1.4 } 1.5 1.6 public void visitClassDef(JCClassDecl tree) { 1.7 - // Local classes have not been entered yet, so we need to do it now: 1.8 - if ((env.info.scope.owner.kind & (VAR | MTH)) != 0) 1.9 + // Local and anonymous classes have not been entered yet, so we need to 1.10 + // do it now. 1.11 + if ((env.info.scope.owner.kind & (VAR | MTH)) != 0) { 1.12 enter.classEnter(tree, env); 1.13 + } else { 1.14 + // If this class declaration is part of a class level annotation, 1.15 + // as in @MyAnno(new Object() {}) class MyClass {}, enter it in 1.16 + // order to simplify later steps and allow for sensible error 1.17 + // messages. 1.18 + if (env.tree.hasTag(NEWCLASS) && TreeInfo.isInAnnotation(env, tree)) 1.19 + enter.classEnter(tree, env); 1.20 + } 1.21 1.22 ClassSymbol c = tree.sym; 1.23 if (c == null) {
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Jun 10 09:13:27 2015 +0200 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Fri Jan 10 12:47:15 2014 +0100 2.3 @@ -271,7 +271,7 @@ 2.4 * the one of its outer environment 2.5 */ 2.6 protected static boolean isStatic(Env<AttrContext> env) { 2.7 - return env.info.staticLevel > env.outer.info.staticLevel; 2.8 + return env.outer != null && env.info.staticLevel > env.outer.info.staticLevel; 2.9 } 2.10 2.11 /** An environment is an "initializer" if it is a constructor or
3.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Wed Jun 10 09:13:27 2015 +0200 3.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Fri Jan 10 12:47:15 2014 +0100 3.3 @@ -28,6 +28,7 @@ 3.4 3.5 3.6 import com.sun.source.tree.Tree; 3.7 +import com.sun.source.util.TreePath; 3.8 import com.sun.tools.javac.code.*; 3.9 import com.sun.tools.javac.comp.AttrContext; 3.10 import com.sun.tools.javac.comp.Env; 3.11 @@ -351,6 +352,18 @@ 3.12 return (lit.typetag == BOT); 3.13 } 3.14 3.15 + /** Return true iff this tree is a child of some annotation. */ 3.16 + public static boolean isInAnnotation(Env<?> env, JCTree tree) { 3.17 + TreePath tp = TreePath.getPath(env.toplevel, tree); 3.18 + if (tp != null) { 3.19 + for (Tree t : tp) { 3.20 + if (t.getKind() == Tree.Kind.ANNOTATION) 3.21 + return true; 3.22 + } 3.23 + } 3.24 + return false; 3.25 + } 3.26 + 3.27 public static String getCommentText(Env<?> env, JCTree tree) { 3.28 DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL)) 3.29 ? ((JCCompilationUnit) tree).docComments
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/annotations/neg/AnonSubclass.java Fri Jan 10 12:47:15 2014 +0100 4.3 @@ -0,0 +1,13 @@ 4.4 +/* 4.5 + * @test /nodynamiccopyright/ 4.6 + * @bug 8028389 4.7 + * @summary javac should output a proper error message when given something 4.8 + * like new Object(){} as annotation argument. 4.9 + * 4.10 + * @compile/fail/ref=AnonSubclass.out -XDrawDiagnostics AnonSubclass.java 4.11 + */ 4.12 + 4.13 +@AnonSubclass(new Object(){}) 4.14 +@interface AnonSubclass { 4.15 + String value(); 4.16 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/annotations/neg/AnonSubclass.out Fri Jan 10 12:47:15 2014 +0100 5.3 @@ -0,0 +1,2 @@ 5.4 +AnonSubclass.java:10:15: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: java.lang.Object, java.lang.String) 5.5 +1 error
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/annotations/neg/pkg/AnonSubclassOnPkg.java Fri Jan 10 12:47:15 2014 +0100 6.3 @@ -0,0 +1,28 @@ 6.4 +/* 6.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +package pkg; 6.28 + 6.29 +@interface AnonSubclassOnPkg { 6.30 + String value(); 6.31 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/annotations/neg/pkg/package-info.java Fri Jan 10 12:47:15 2014 +0100 7.3 @@ -0,0 +1,12 @@ 7.4 +/* 7.5 + * @test /nodynamiccopyright/ 7.6 + * @bug 8028389 7.7 + * @summary javac should output a proper error message when given something 7.8 + * like new Object(){} as annotation argument. 7.9 + * 7.10 + * @compile AnonSubclassOnPkg.java 7.11 + * @compile/fail/ref=package-info.out -XDrawDiagnostics package-info.java 7.12 + */ 7.13 + 7.14 +@AnonSubclassOnPkg(new Object(){}) 7.15 +package pkg;
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/annotations/neg/pkg/package-info.out Fri Jan 10 12:47:15 2014 +0100 8.3 @@ -0,0 +1,2 @@ 8.4 +package-info.java:11:20: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: java.lang.Object, java.lang.String) 8.5 +1 error