8028389: NullPointerException compiling annotation values that have bodies

Made sure anonymous class declarations inside class- and package-level annotations are properly entered.

Reviewed-by: jfranck
This commit is contained in:
Andreas Lundblad 2014-01-10 12:47:15 +01:00
parent e32b40185e
commit a76d1ab5a2
8 changed files with 85 additions and 6 deletions

View File

@ -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 class Attr extends JCTree.Visitor {
}
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) {

View File

@ -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 @@ public class Resolve {
* 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

View File

@ -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 @@ package com.sun.tools.javac.tree;
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 @@ public class TreeInfo {
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

View File

@ -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();
}

View File

@ -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

View File

@ -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();
}

View File

@ -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;

View File

@ -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