Tue, 24 Sep 2013 11:46:25 -0700
8025272: doclint needs to check for valid usage of @value tag
Reviewed-by: bpatel
1.1 --- a/src/share/classes/com/sun/tools/doclint/Checker.java Tue Sep 24 10:51:28 2013 -0700 1.2 +++ b/src/share/classes/com/sun/tools/doclint/Checker.java Tue Sep 24 11:46:25 2013 -0700 1.3 @@ -44,6 +44,7 @@ 1.4 import javax.lang.model.element.ElementKind; 1.5 import javax.lang.model.element.ExecutableElement; 1.6 import javax.lang.model.element.Name; 1.7 +import javax.lang.model.element.VariableElement; 1.8 import javax.lang.model.type.TypeKind; 1.9 import javax.lang.model.type.TypeMirror; 1.10 import javax.tools.Diagnostic.Kind; 1.11 @@ -822,10 +823,33 @@ 1.12 1.13 @Override 1.14 public Void visitValue(ValueTree tree, Void ignore) { 1.15 + ReferenceTree ref = tree.getReference(); 1.16 + if (ref == null || ref.getSignature().isEmpty()) { 1.17 + if (!isConstant(env.currElement)) 1.18 + env.messages.error(REFERENCE, tree, "dc.value.not.allowed.here"); 1.19 + } else { 1.20 + Element e = env.trees.getElement(new DocTreePath(getCurrentPath(), ref)); 1.21 + if (!isConstant(e)) 1.22 + env.messages.error(REFERENCE, tree, "dc.value.not.a.constant"); 1.23 + } 1.24 + 1.25 markEnclosingTag(Flag.HAS_INLINE_TAG); 1.26 return super.visitValue(tree, ignore); 1.27 } 1.28 1.29 + private boolean isConstant(Element e) { 1.30 + if (e == null) 1.31 + return false; 1.32 + 1.33 + switch (e.getKind()) { 1.34 + case FIELD: 1.35 + Object value = ((VariableElement) e).getConstantValue(); 1.36 + return (value != null); // can't distinguish "not a constant" from "constant is null" 1.37 + default: 1.38 + return false; 1.39 + } 1.40 + } 1.41 + 1.42 @Override 1.43 public Void visitVersion(VersionTree tree, Void ignore) { 1.44 warnIfEmpty(tree, tree.getBody());
2.1 --- a/src/share/classes/com/sun/tools/doclint/resources/doclint.properties Tue Sep 24 10:51:28 2013 -0700 2.2 +++ b/src/share/classes/com/sun/tools/doclint/resources/doclint.properties Tue Sep 24 11:46:25 2013 -0700 2.3 @@ -68,6 +68,8 @@ 2.4 dc.tag.unknown = unknown tag: {0} 2.5 dc.text.not.allowed = text not allowed in <{0}> element 2.6 dc.unexpected.comment=documentation comment not expected here 2.7 +dc.value.not.allowed.here='{@value}' not allowed here 2.8 +dc.value.not.a.constant=value does not refer to a constant 2.9 2.10 dc.main.ioerror=IO error: {0} 2.11 dc.main.no.files.given=No files given
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/doclint/ValueTest.java Tue Sep 24 11:46:25 2013 -0700 3.3 @@ -0,0 +1,67 @@ 3.4 +/* 3.5 + * @test /nodynamiccopyright/ 3.6 + * @bug 8025272 3.7 + * @summary doclint needs to check for valid usage of at-value tag 3.8 + * @build DocLintTester 3.9 + * @run main DocLintTester -ref ValueTest.out ValueTest.java 3.10 + */ 3.11 + 3.12 +/** */ 3.13 +public class ValueTest { 3.14 + /* 3.15 + * Tests for {@value} without a reference 3.16 + */ 3.17 + 3.18 + /** valid: {@value} */ 3.19 + public static final boolean cBoolean = false; 3.20 + 3.21 + /** valid: {@value} */ 3.22 + public static final byte cByte = 0; 3.23 + 3.24 + /** valid: {@value} */ 3.25 + public static final short cShort = 0; 3.26 + 3.27 + /** valid: {@value} */ 3.28 + public static final int cInt = 0; 3.29 + 3.30 + /** valid: {@value} */ 3.31 + public static final long cLong = 0L; 3.32 + 3.33 + /** valid: {@value} */ 3.34 + public static final float cFloat = 0.0f; 3.35 + 3.36 + /** valid: {@value} */ 3.37 + public static final double cDouble = 0.0; 3.38 + 3.39 + /** valid: {@value} */ 3.40 + public static final String cString = ""; 3.41 + 3.42 + /** invalid class C: {@value} */ 3.43 + public class C { } 3.44 + 3.45 + /** invalid enum E: {@value} */ 3.46 + public enum E { 3.47 + /** invalid enum constant E1: {@value} */ 3.48 + E1 3.49 + } 3.50 + 3.51 + /** invalid field 1: {@value} */ 3.52 + public int f1; 3.53 + 3.54 + /** invalid field 2: {@value} */ 3.55 + public int f2 = 3; 3.56 + 3.57 + 3.58 + /* 3.59 + * Tests for {@value} with a reference 3.60 + */ 3.61 + 3.62 + /** valid: {@value Integer#SIZE} */ 3.63 + public int intRef; 3.64 + 3.65 + /** invalid method: {@value Object#toString} */ 3.66 + public int badMethod; 3.67 + 3.68 + /** invalid enum constant: {@value Thread.State#NEW} */ 3.69 + public int badEnum; 3.70 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/doclint/ValueTest.out Tue Sep 24 11:46:25 2013 -0700 4.3 @@ -0,0 +1,22 @@ 4.4 +ValueTest.java:39: error: {@value} not allowed here 4.5 + /** invalid class C: {@value} */ 4.6 + ^ 4.7 +ValueTest.java:42: error: {@value} not allowed here 4.8 + /** invalid enum E: {@value} */ 4.9 + ^ 4.10 +ValueTest.java:44: error: {@value} not allowed here 4.11 + /** invalid enum constant E1: {@value} */ 4.12 + ^ 4.13 +ValueTest.java:48: error: {@value} not allowed here 4.14 + /** invalid field 1: {@value} */ 4.15 + ^ 4.16 +ValueTest.java:51: error: {@value} not allowed here 4.17 + /** invalid field 2: {@value} */ 4.18 + ^ 4.19 +ValueTest.java:62: error: value does not refer to a constant 4.20 + /** invalid method: {@value Object#toString} */ 4.21 + ^ 4.22 +ValueTest.java:65: error: value does not refer to a constant 4.23 + /** invalid enum constant: {@value Thread.State#NEW} */ 4.24 + ^ 4.25 +7 errors