# HG changeset patch # User jlahoda # Date 1421141801 -3600 # Node ID 63c31d7de8f64c5086ad2ca624fa4afdb50f58b1 # Parent 9b94e84322e4f1b2bb553abd99bdee99662d2068 8027888: javac wrongly allows annotations in array-typed class literals Summary: Compiler incorrectly accepts type annotations on array-typed class literals. Reviewed-by: jlahoda, jfranck Contributed-by: srikanth.adayapalam@oracle.com diff -r 9b94e84322e4 -r 63c31d7de8f6 langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java --- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Jan 13 10:32:19 2015 +0100 +++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Jan 13 10:36:41 2015 +0100 @@ -1209,15 +1209,7 @@ if (annos.nonEmpty()) { t = toP(F.at(pos).AnnotatedType(annos, t)); } - // .class is only allowed if there were no annotations - JCExpression nt = bracketsSuffix(t); - if (nt != t && (annos.nonEmpty() || TreeInfo.containsTypeAnnotation(t))) { - // t and nt are different if bracketsSuffix parsed a .class. - // The check for nonEmpty covers the case when the whole array is annotated. - // Helper method isAnnotated looks for annos deeply within t. - syntaxError("no.annotations.on.dot.class"); - } - t = nt; + t = bracketsSuffix(t); } else { if ((mode & EXPR) != 0) { mode = EXPR; @@ -1956,6 +1948,12 @@ } t = F.at(pos).Erroneous(List.of(toP(F.at(pos).Select(t, name)))); } else { + Tag tag = t.getTag(); + // Type annotations are illegal on class literals. Annotated non array class literals + // are complained about directly in term3(), Here check for type annotations on dimensions + // taking care to handle some interior dimension(s) being annotated. + if ((tag == TYPEARRAY && TreeInfo.containsTypeAnnotation(t)) || tag == ANNOTATED_TYPE) + syntaxError("no.annotations.on.dot.class"); t = toP(F.at(pos).Select(t, names._class)); } } else if ((mode & TYPE) != 0) { diff -r 9b94e84322e4 -r 63c31d7de8f6 langtools/test/tools/javac/annotations/typeAnnotations/failures/AnnotatedClassExpr.java --- a/langtools/test/tools/javac/annotations/typeAnnotations/failures/AnnotatedClassExpr.java Tue Jan 13 10:32:19 2015 +0100 +++ b/langtools/test/tools/javac/annotations/typeAnnotations/failures/AnnotatedClassExpr.java Tue Jan 13 10:36:41 2015 +0100 @@ -1,6 +1,6 @@ /* * @test /nodynamiccopyright/ - * @bug 8027262 + * @bug 8027262 8027888 * @summary A class expression cannot be annotated. * @compile/fail/ref=AnnotatedClassExpr.out -XDrawDiagnostics AnnotatedClassExpr.java */ @@ -10,6 +10,12 @@ class AnnotatedClassExpr { static void main() { Object o1 = @A int.class; + o1 = @A int [] . class; + o1 = int @A [] . class; + o1 = int [] @A [] . class; + o1 = AnnotatedClassExpr @A [] .class; + o1 = @A AnnotatedClassExpr @A [] .class; + o1 = @A AnnotatedClassExpr.class; } } diff -r 9b94e84322e4 -r 63c31d7de8f6 langtools/test/tools/javac/annotations/typeAnnotations/failures/AnnotatedClassExpr.out --- a/langtools/test/tools/javac/annotations/typeAnnotations/failures/AnnotatedClassExpr.out Tue Jan 13 10:32:19 2015 +0100 +++ b/langtools/test/tools/javac/annotations/typeAnnotations/failures/AnnotatedClassExpr.out Tue Jan 13 10:36:41 2015 +0100 @@ -1,2 +1,8 @@ AnnotatedClassExpr.java:12:29: compiler.err.no.annotations.on.dot.class -1 error +AnnotatedClassExpr.java:13:27: compiler.err.no.annotations.on.dot.class +AnnotatedClassExpr.java:14:27: compiler.err.no.annotations.on.dot.class +AnnotatedClassExpr.java:15:30: compiler.err.no.annotations.on.dot.class +AnnotatedClassExpr.java:16:41: compiler.err.no.annotations.on.dot.class +AnnotatedClassExpr.java:17:44: compiler.err.no.annotations.on.dot.class +AnnotatedClassExpr.java:18:37: compiler.err.no.annotations.on.dot.class +7 errors