提交 fb50b2ae 编写于 作者: M mcimadamore

6976649: javac does not enforce required annotation elements in arrays

Summary: type annotation should take advantage of recursive annotation checking
Reviewed-by: jjg
上级 8b72508f
......@@ -696,9 +696,11 @@ public class Attr extends JCTree.Visitor {
// ensure that annotation method does not clash with members of Object/Annotation
chk.validateAnnotationMethod(tree.pos(), m);
// 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);
if (tree.defaultValue != null) {
// if default value is an annotation, check it is a well-formed
// annotation value (e.g. no duplicate values, no missing values, etc.)
chk.validateAnnotationTree(tree.defaultValue);
}
}
for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail)
......
......@@ -25,6 +25,7 @@
package com.sun.tools.javac.comp;
import com.sun.source.tree.AssignmentTree;
import java.util.*;
import java.util.Set;
......@@ -1930,20 +1931,17 @@ public class Check {
**************************************************************************/
/**
* Validate annotations in default values
* Recursively validate annotations values
*/
void validateAnnotationDefaultValue(JCTree defaultValue) {
class DefaultValueValidator extends TreeScanner {
void validateAnnotationTree(JCTree tree) {
class AnnotationValidator 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());
}
tree.accept(new AnnotationValidator());
}
/** Annotation types are restricted to primitives, String, an
......@@ -2009,7 +2007,7 @@ public class Check {
/** Check an annotation of a symbol.
*/
public void validateAnnotation(JCAnnotation a, Symbol s) {
validateAnnotation(a);
validateAnnotationTree(a);
if (!annotationApplicable(a, s))
log.error(a.pos(), "annotation.type.not.applicable");
......@@ -2023,7 +2021,7 @@ public class Check {
public void validateTypeAnnotation(JCTypeAnnotation a, boolean isTypeParameter) {
if (a.type == null)
throw new AssertionError("annotation tree hasn't been attributed yet: " + a);
validateAnnotation(a);
validateAnnotationTree(a);
if (!isTypeAnnotation(a, isTypeParameter))
log.error(a.pos(), "annotation.type.not.applicable");
......@@ -2141,8 +2139,6 @@ public class Check {
if (!members.remove(m))
log.error(assign.lhs.pos(), "duplicate.annotation.member.value",
m.name, a.type);
if (assign.rhs.getTag() == ANNOTATION)
validateAnnotation((JCAnnotation)assign.rhs);
}
// all the remaining ones better have default values
......
/*
* @test /nodynamiccopyright/
* @bug 6881115
* @bug 6881115 6976649
* @summary javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
* @author mcimadamore
* @compile/fail/ref=T6881115.out -XDrawDiagnostics T6881115.java
......@@ -14,5 +14,7 @@
String b1();
int b2();
}
@A
class T6881115 {}
@A(b = @B(b2 = 1, b2 = 2),
b_arr = {@B(), @B(b2 = 1, b2 = 2)})
class T6881115<@A(b = @B(b2 = 1, b2 = 2),
b_arr = {@B(), @B(b2 = 1, b2 = 2)}) X> {}
......@@ -3,4 +3,14 @@ T6881115.java:10:19: compiler.err.annotation.missing.default.value: B, b1
T6881115.java:11:26: compiler.err.annotation.missing.default.value.1: B, b1,b2
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
T6881115.java:17:19: compiler.err.duplicate.annotation.member.value: b2, B
T6881115.java:17:8: compiler.err.annotation.missing.default.value: B, b1
T6881115.java:18:13: compiler.err.annotation.missing.default.value.1: B, b1,b2
T6881115.java:18:30: compiler.err.duplicate.annotation.member.value: b2, B
T6881115.java:18:19: compiler.err.annotation.missing.default.value: B, b1
T6881115.java:19:34: compiler.err.duplicate.annotation.member.value: b2, B
T6881115.java:19:23: compiler.err.annotation.missing.default.value: B, b1
T6881115.java:20:28: compiler.err.annotation.missing.default.value.1: B, b1,b2
T6881115.java:20:45: compiler.err.duplicate.annotation.member.value: b2, B
T6881115.java:20:34: compiler.err.annotation.missing.default.value: B, b1
15 errors
......@@ -36,7 +36,7 @@ import java.lang.annotation.*;
}
@TestAnnotation({@SuppressWarnings(),
@TestAnnotation({@SuppressWarnings({}),
@SuppressWarnings({"Beware the ides of March.",}),
@SuppressWarnings({"Look both ways", "Before Crossing",}), })
public class TrailingComma {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册