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
--- 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.<JCTree>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) {
--- 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;
}
}
--- 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