Tue, 03 Dec 2013 18:50:26 +0100
8028699: Compiler crash during speculative attribution of annotated type
Summary: Moving the checkForDeclarationAnnotations check into Attr.TypeAnnotationsValidator
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Nov 26 15:33:12 2013 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Dec 03 18:50:26 2013 +0100 1.3 @@ -28,7 +28,6 @@ 1.4 import java.util.*; 1.5 1.6 import javax.lang.model.element.ElementKind; 1.7 -import javax.lang.model.type.TypeKind; 1.8 import javax.tools.JavaFileObject; 1.9 1.10 import com.sun.source.tree.IdentifierTree; 1.11 @@ -2164,11 +2163,6 @@ 1.12 tree.constructor, 1.13 localEnv, 1.14 new ResultInfo(pkind, newMethodTemplate(syms.voidType, argtypes, typeargtypes))); 1.15 - } else { 1.16 - if (tree.clazz.hasTag(ANNOTATED_TYPE)) { 1.17 - checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations, 1.18 - tree.clazz.type.tsym); 1.19 - } 1.20 } 1.21 1.22 if (tree.constructor != null && tree.constructor.kind == MTH) 1.23 @@ -2230,21 +2224,6 @@ 1.24 } 1.25 } 1.26 1.27 - private void checkForDeclarationAnnotations(List<? extends JCAnnotation> annotations, 1.28 - Symbol sym) { 1.29 - // Ensure that no declaration annotations are present. 1.30 - // Note that a tree type might be an AnnotatedType with 1.31 - // empty annotations, if only declaration annotations were given. 1.32 - // This method will raise an error for such a type. 1.33 - for (JCAnnotation ai : annotations) { 1.34 - if (!ai.type.isErroneous() && 1.35 - typeAnnotations.annotationType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) { 1.36 - log.error(ai.pos(), "annotation.type.not.applicable"); 1.37 - } 1.38 - } 1.39 - } 1.40 - 1.41 - 1.42 /** Make an attributed null check tree. 1.43 */ 1.44 public JCExpression makeNullCheck(JCExpression arg) { 1.45 @@ -2271,10 +2250,6 @@ 1.46 attribExpr(l.head, localEnv, syms.intType); 1.47 owntype = new ArrayType(owntype, syms.arrayClass); 1.48 } 1.49 - if (tree.elemtype.hasTag(ANNOTATED_TYPE)) { 1.50 - checkForDeclarationAnnotations(((JCAnnotatedType) tree.elemtype).annotations, 1.51 - tree.elemtype.type.tsym); 1.52 - } 1.53 } else { 1.54 // we are seeing an untyped aggregate { ... } 1.55 // this is allowed only if the prototype is an array 1.56 @@ -4419,7 +4394,7 @@ 1.57 } 1.58 public void visitMethodDef(JCMethodDecl tree) { 1.59 if (tree.recvparam != null && 1.60 - tree.recvparam.vartype.type.getKind() != TypeKind.ERROR) { 1.61 + !tree.recvparam.vartype.type.isErroneous()) { 1.62 checkForDeclarationAnnotations(tree.recvparam.mods.annotations, 1.63 tree.recvparam.vartype.type.tsym); 1.64 } 1.65 @@ -4458,17 +4433,28 @@ 1.66 super.visitTypeTest(tree); 1.67 } 1.68 public void visitNewClass(JCNewClass tree) { 1.69 - if (tree.clazz.type != null) 1.70 + if (tree.clazz.hasTag(ANNOTATED_TYPE)) { 1.71 + checkForDeclarationAnnotations(((JCAnnotatedType) tree.clazz).annotations, 1.72 + tree.clazz.type.tsym); 1.73 + } 1.74 + if (tree.def != null) { 1.75 + checkForDeclarationAnnotations(tree.def.mods.annotations, tree.clazz.type.tsym); 1.76 + } 1.77 + if (tree.clazz.type != null) { 1.78 validateAnnotatedType(tree.clazz, tree.clazz.type); 1.79 + } 1.80 super.visitNewClass(tree); 1.81 } 1.82 public void visitNewArray(JCNewArray tree) { 1.83 - if (tree.elemtype != null && tree.elemtype.type != null) 1.84 + if (tree.elemtype != null && tree.elemtype.type != null) { 1.85 + if (tree.elemtype.hasTag(ANNOTATED_TYPE)) { 1.86 + checkForDeclarationAnnotations(((JCAnnotatedType) tree.elemtype).annotations, 1.87 + tree.elemtype.type.tsym); 1.88 + } 1.89 validateAnnotatedType(tree.elemtype, tree.elemtype.type); 1.90 + } 1.91 super.visitNewArray(tree); 1.92 } 1.93 - 1.94 - @Override 1.95 public void visitClassDef(JCClassDecl tree) { 1.96 if (sigOnly) { 1.97 scan(tree.mods); 1.98 @@ -4483,8 +4469,6 @@ 1.99 scan(member); 1.100 } 1.101 } 1.102 - 1.103 - @Override 1.104 public void visitBlock(JCBlock tree) { 1.105 if (!sigOnly) { 1.106 scan(tree.stats); 1.107 @@ -4590,6 +4574,20 @@ 1.108 } 1.109 } 1.110 } 1.111 + 1.112 + private void checkForDeclarationAnnotations(List<? extends JCAnnotation> annotations, 1.113 + Symbol sym) { 1.114 + // Ensure that no declaration annotations are present. 1.115 + // Note that a tree type might be an AnnotatedType with 1.116 + // empty annotations, if only declaration annotations were given. 1.117 + // This method will raise an error for such a type. 1.118 + for (JCAnnotation ai : annotations) { 1.119 + if (!ai.type.isErroneous() && 1.120 + typeAnnotations.annotationType(ai.attribute, sym) == TypeAnnotations.AnnotationType.DECLARATION) { 1.121 + log.error(ai.pos(), "annotation.type.not.applicable"); 1.122 + } 1.123 + } 1.124 + } 1.125 }; 1.126 1.127 // <editor-fold desc="post-attribution visitor">
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Nov 26 15:33:12 2013 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Dec 03 18:50:26 2013 +0100 2.3 @@ -2985,6 +2985,7 @@ 2.4 boolean annotationApplicable(JCAnnotation a, Symbol s) { 2.5 Attribute.Array arr = getAttributeTargetAttribute(a.annotationType.type.tsym); 2.6 Name[] targets; 2.7 + 2.8 if (arr == null) { 2.9 targets = defaultTargetMetaInfo(a, s); 2.10 } else { 2.11 @@ -3001,7 +3002,7 @@ 2.12 } 2.13 for (Name target : targets) { 2.14 if (target == names.TYPE) 2.15 - { if (s.kind == TYP && !s.isAnonymous()) return true; } 2.16 + { if (s.kind == TYP) return true; } 2.17 else if (target == names.FIELD) 2.18 { if (s.kind == VAR && s.owner.kind != MTH) return true; } 2.19 else if (target == names.METHOD)
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/annotations/typeAnnotations/failures/CheckForDeclAnnoNPE.java Tue Dec 03 18:50:26 2013 +0100 3.3 @@ -0,0 +1,43 @@ 3.4 +/* 3.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 8028699 3.30 + * @summary Ensure there is no NPE in checking for decl. annotations in this example 3.31 + * @author Werner Dietl 3.32 + * @compile -doe CheckForDeclAnnoNPE.java 3.33 + */ 3.34 + 3.35 +import java.lang.annotation.ElementType; 3.36 +import java.lang.annotation.Target; 3.37 + 3.38 + 3.39 +class CheckForDeclAnnoNPE { 3.40 + void test(String s) { 3.41 + test(new @TA String().toString()); 3.42 + } 3.43 +} 3.44 + 3.45 +@Target(ElementType.TYPE_USE) 3.46 +@interface TA {}
4.1 --- a/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.java Tue Nov 26 15:33:12 2013 +0100 4.2 +++ b/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.java Tue Dec 03 18:50:26 2013 +0100 4.3 @@ -10,9 +10,6 @@ 4.4 Object e1 = new @DA int[5]; 4.5 Object e2 = new @DA String[42]; 4.6 Object e3 = new @DA Object(); 4.7 - 4.8 - // The declaration annotation is only allowed for 4.9 - // an anonymous class creation. 4.10 Object ok = new @DA Object() { }; 4.11 } 4.12
5.1 --- a/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out Tue Nov 26 15:33:12 2013 +0100 5.2 +++ b/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out Tue Dec 03 18:50:26 2013 +0100 5.3 @@ -1,5 +1,5 @@ 5.4 DeclarationAnnotation.java:10:21: compiler.err.annotation.type.not.applicable 5.5 DeclarationAnnotation.java:11:21: compiler.err.annotation.type.not.applicable 5.6 DeclarationAnnotation.java:12:21: compiler.err.annotation.type.not.applicable 5.7 -DeclarationAnnotation.java:16:21: compiler.err.annotation.type.not.applicable 5.8 +DeclarationAnnotation.java:13:21: compiler.err.annotation.type.not.applicable 5.9 4 errors