8027888: javac wrongly allows annotations in array-typed class literals

Compiler incorrectly accepts type annotations on array-typed class literals.

Reviewed-by: jlahoda, jfranck
This commit is contained in:
Srikanth Adayapalam 2015-01-13 10:36:41 +01:00 committed by Jan Lahoda
parent 5cb253a2e9
commit 2b3c75aa36
3 changed files with 21 additions and 11 deletions

View File

@ -1209,15 +1209,7 @@ public class JavacParser implements Parser {
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 @@ public class JavacParser implements Parser {
}
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) {

View File

@ -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 @@ import java.util.List;
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;
}
}

View File

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