6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException

Default annotation value is not attributed

Reviewed-by: jjg, darcy
This commit is contained in:
Maurizio Cimadamore 2010-08-05 09:44:54 +01:00
parent 0c1962f5de
commit 156e82c164
4 changed files with 61 additions and 12 deletions

View File

@ -675,24 +675,32 @@ public class Attr extends JCTree.Visitor {
// Check that type parameters are well-formed.
chk.validate(tree.typarams, localEnv);
if ((owner.flags() & ANNOTATION) != 0 &&
tree.typarams.nonEmpty())
log.error(tree.typarams.head.pos(),
"intf.annotation.members.cant.have.type.params");
// Check that result type is well-formed.
chk.validate(tree.restype, localEnv);
if ((owner.flags() & ANNOTATION) != 0)
chk.validateAnnotationType(tree.restype);
if ((owner.flags() & ANNOTATION) != 0)
// annotation method checks
if ((owner.flags() & ANNOTATION) != 0) {
// annotation method cannot have throws clause
if (tree.thrown.nonEmpty()) {
log.error(tree.thrown.head.pos(),
"throws.not.allowed.in.intf.annotation");
}
// annotation method cannot declare type-parameters
if (tree.typarams.nonEmpty()) {
log.error(tree.typarams.head.pos(),
"intf.annotation.members.cant.have.type.params");
}
// validate annotation method's return type (could be an annotation type)
chk.validateAnnotationType(tree.restype);
// ensure that annotation method does not clash with members of Object/Annotation
chk.validateAnnotationMethod(tree.pos(), m);
// Check that all exceptions mentioned in the throws clause extend
// java.lang.Throwable.
if ((owner.flags() & ANNOTATION) != 0 && tree.thrown.nonEmpty())
log.error(tree.thrown.head.pos(),
"throws.not.allowed.in.intf.annotation");
// if default value is an annotation, check it is a well-formed
// annotation value (e.g. no duplicate values, no missing values, etc.)
chk.validateAnnotationDefaultValue(tree.defaultValue);
}
for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail)
chk.checkType(l.head.pos(), l.head.type, syms.throwableType);

View File

@ -1929,6 +1929,23 @@ public class Check {
* Check annotations
**************************************************************************/
/**
* Validate annotations in default values
*/
void validateAnnotationDefaultValue(JCTree defaultValue) {
class DefaultValueValidator extends TreeScanner {
@Override
public void visitAnnotation(JCAnnotation tree) {
super.visitAnnotation(tree);
validateAnnotation(tree);
}
}
// defaultValue may be null if an error occurred, so don't bother validating it
if (defaultValue != null) {
defaultValue.accept(new DefaultValueValidator());
}
}
/** Annotation types are restricted to primitives, String, an
* enum, an annotation, Class, Class<?>, Class<? extends
* Anything>, arrays of the preceding.

View File

@ -0,0 +1,18 @@
/*
* @test /nodynamiccopyright/
* @bug 6881115
* @summary javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
* @author mcimadamore
* @compile/fail/ref=T6881115.out -XDrawDiagnostics T6881115.java
*/
@interface A {
B b() default @B(b2 = 1, b2 = 2);
B[] b_arr() default {@B(), @B(b2 = 1, b2 = 2)};
}
@interface B {
String b1();
int b2();
}
@A
class T6881115 {}

View File

@ -0,0 +1,6 @@
T6881115.java:10:30: compiler.err.duplicate.annotation.member.value: b2, B
T6881115.java:10:19: compiler.err.annotation.missing.default.value: B, b1
T6881115.java:11:26: compiler.err.annotation.missing.default.value: B, b1
T6881115.java:11:43: compiler.err.duplicate.annotation.member.value: b2, B
T6881115.java:11:32: compiler.err.annotation.missing.default.value: B, b1
5 errors