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