# HG changeset patch # User alundblad # Date 1389354435 -3600 # Node ID 380f6c17ea01934ad4caaca5df53917f75f1b772 # Parent d94fe2d29b1e2cebdd69240bf29c550f0c65a592 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 diff -r d94fe2d29b1e -r 380f6c17ea01 src/share/classes/com/sun/tools/javac/comp/Attr.java --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Jun 10 09:13:27 2015 +0200 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Fri Jan 10 12:47:15 2014 +0100 @@ -825,9 +825,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) { diff -r d94fe2d29b1e -r 380f6c17ea01 src/share/classes/com/sun/tools/javac/comp/Resolve.java --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Jun 10 09:13:27 2015 +0200 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Fri Jan 10 12:47:15 2014 +0100 @@ -271,7 +271,7 @@ * the one of its outer environment */ protected static boolean isStatic(Env 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 diff -r d94fe2d29b1e -r 380f6c17ea01 src/share/classes/com/sun/tools/javac/tree/TreeInfo.java --- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Wed Jun 10 09:13:27 2015 +0200 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Fri Jan 10 12:47:15 2014 +0100 @@ -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; @@ -351,6 +352,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 diff -r d94fe2d29b1e -r 380f6c17ea01 test/tools/javac/annotations/neg/AnonSubclass.java --- /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(); +} diff -r d94fe2d29b1e -r 380f6c17ea01 test/tools/javac/annotations/neg/AnonSubclass.out --- /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 diff -r d94fe2d29b1e -r 380f6c17ea01 test/tools/javac/annotations/neg/pkg/AnonSubclassOnPkg.java --- /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(); +} diff -r d94fe2d29b1e -r 380f6c17ea01 test/tools/javac/annotations/neg/pkg/package-info.java --- /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; diff -r d94fe2d29b1e -r 380f6c17ea01 test/tools/javac/annotations/neg/pkg/package-info.out --- /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