- * Property {@code excludeScope} - Specify the visibility scope where Javadoc
- * comments are not checked.
- * Type is {@code com.puppycrawl.tools.checkstyle.api.Scope}.
- * Default value is {@code null}.
+ * Property {@code accessModifiers} - Access modifiers of methods where parameters are
+ * checked.
+ * Type is {@code com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption[]}.
+ * Default value is {@code public, protected, package, private}.
*
*
* Property {@code ignoreNamePattern} - Specify the regexp to define variable names to ignore.
@@ -47,11 +49,6 @@
* Default value is {@code null}.
*
*
- * Property {@code scope} - Specify the visibility scope where Javadoc comments are checked.
- * Type is {@code com.puppycrawl.tools.checkstyle.api.Scope}.
- * Default value is {@code private}.
- *
- *
* Property {@code tokens} - tokens to check
* Type is {@code java.lang.String[]}.
* Validation type is {@code tokenSet}.
@@ -84,35 +81,28 @@ public class JavadocVariableCheck
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
- public static final String MSG_JAVADOC_MISSING = "javadoc.missing";
-
- /** Specify the visibility scope where Javadoc comments are checked. */
- private Scope scope = Scope.PRIVATE;
- /** Specify the visibility scope where Javadoc comments are not checked. */
- private Scope excludeScope;
+ public static final String MSG_JAVADOC_MISSING = "javadoc.missing";
+ /** Access modifiers of methods where parameters are checked. */
+ private AccessModifierOption[] accessModifiers = {
+ AccessModifierOption.PUBLIC,
+ AccessModifierOption.PROTECTED,
+ AccessModifierOption.PACKAGE,
+ AccessModifierOption.PRIVATE,
+ };
/** Specify the regexp to define variable names to ignore. */
private Pattern ignoreNamePattern;
/**
- * Setter to specify the visibility scope where Javadoc comments are checked.
+ * Setter to access modifiers of methods where parameters are checked.
*
- * @param scope a scope.
- * @since 3.0
+ * @param accessModifiers access modifiers of methods which should be checked.
+ * @since 11.0.0
*/
- public void setScope(Scope scope) {
- this.scope = scope;
- }
-
- /**
- * Setter to specify the visibility scope where Javadoc comments are not checked.
- *
- * @param excludeScope a scope.
- * @since 3.4
- */
- public void setExcludeScope(Scope excludeScope) {
- this.excludeScope = excludeScope;
+ public void setAccessModifiers(AccessModifierOption... accessModifiers) {
+ this.accessModifiers =
+ Arrays.copyOf(accessModifiers, accessModifiers.length);
}
/**
@@ -176,6 +166,17 @@ private boolean isIgnored(DetailAST ast) {
|| "serialVersionUID".equals(name);
}
+ /**
+ * Checks whether a method has the correct access modifier to be checked.
+ *
+ * @param accessModifier the access modifier of the method.
+ * @return whether the method matches the expected access modifier.
+ */
+ private boolean matchAccessModifiers(final AccessModifierOption accessModifier) {
+ return Arrays.stream(accessModifiers)
+ .anyMatch(modifier -> modifier == accessModifier);
+ }
+
/**
* Whether we should check this node.
*
@@ -185,14 +186,15 @@ private boolean isIgnored(DetailAST ast) {
private boolean shouldCheck(final DetailAST ast) {
boolean result = false;
if (!ScopeUtil.isInCodeBlock(ast) && !isIgnored(ast)) {
- final Scope customScope = ScopeUtil.getScope(ast);
- final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast);
- result = customScope.isIn(scope) && surroundingScope.isIn(scope)
- && (excludeScope == null
- || !customScope.isIn(excludeScope)
- || !surroundingScope.isIn(excludeScope));
+ try {
+ final AccessModifierOption accessModifier =
+ CheckUtil.getAccessModifierFromModifiersToken(ast);
+ result = matchAccessModifiers(accessModifier);
+ }
+ catch (IllegalArgumentException ex) {
+ result = false;
+ }
}
return result;
}
-
}
diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/javadoc/JavadocVariableCheck.xml b/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/javadoc/JavadocVariableCheck.xml
index 32a9ff77988..0322aac524e 100644
--- a/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/javadoc/JavadocVariableCheck.xml
+++ b/src/main/resources/com/puppycrawl/tools/checkstyle/meta/checks/javadoc/JavadocVariableCheck.xml
@@ -8,18 +8,15 @@
Checks that a variable has a Javadoc comment. Ignores {@code serialVersionUID} fields.
</div>
-
- Specify the visibility scope where Javadoc
- comments are not checked.
+
+ Access modifiers of methods where parameters are
+ checked.Specify the regexp to define variable names to ignore.
-
- Specify the visibility scope where Javadoc comments are checked.
- FUNCTION1 = (String it) -> { // violation
+ // violation below, 'Missing a Javadoc comment'
+ private static final Function FUNCTION1 = (String it) -> {
String stuff = it;
return stuff + it;
};
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnInnerClassFields.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnInnerClassFields.java
index 4e4f65fc8b5..d57cdb1792f 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnInnerClassFields.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnInnerClassFields.java
@@ -1,21 +1,20 @@
/*
JavadocVariable
-scope = (default)private
+accessModifiers = (default)private
excludeScope = (default)null
ignoreNamePattern = (default)null
tokens = (default)ENUM_CONSTANT_DEF
-
*/
package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocvariable;
public class InputJavadocVariableOnInnerClassFields //comment test
{
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -23,10 +22,10 @@ void foo3() {}
private void foo4() {}
protected class ProtectedInner {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -35,10 +34,10 @@ private void foo4() {}
}
class PackageInner {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -47,10 +46,10 @@ private void foo4() {}
}
private class PrivateInner {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnPublicInnerClassFields.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnPublicInnerClassFields.java
index 2b06ae2137d..fd5d5d59bc2 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnPublicInnerClassFields.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableOnPublicInnerClassFields.java
@@ -1,20 +1,18 @@
/*
JavadocVariable
-scope = (default)private
-excludeScope = (default)null
+accessModifiers = (default)private
ignoreNamePattern = (default)null
tokens = (default)ENUM_CONSTANT_DEF
-
*/
package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocvariable;
public class InputJavadocVariableOnPublicInnerClassFields {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -22,10 +20,10 @@ void foo3() {}
private void foo4() {}
public class PublicInner {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -34,10 +32,10 @@ private void foo4() {}
}
protected class ProtectedInner {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -46,10 +44,10 @@ private void foo4() {}
}
class PackageInner {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -58,10 +56,10 @@ private void foo4() {}
}
private class PrivateInner {
- public int i1; // violation
- protected int i2; // violation
- int i3; // violation
- private int i4; // violation
+ public int i1; // violation, 'Missing a Javadoc comment'
+ protected int i2; // violation, 'Missing a Javadoc comment'
+ int i3; // violation, 'Missing a Javadoc comment'
+ private int i4; // violation, 'Missing a Javadoc comment'
public void foo1() {}
protected void foo2() {}
@@ -71,7 +69,7 @@ private void foo4() {}
class IgnoredName {
- private int logger; // violation
+ private int logger; // violation, 'Missing a Javadoc comment'
// no warning, 'serialVersionUID' fields do not require Javadoc
private static final long serialVersionUID = 0;
}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly.java
index 43f15016517..020bdb0871f 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly.java
@@ -1,11 +1,9 @@
/*
JavadocVariable
-scope = (default)private
-excludeScope = (default)null
+accessModifiers = (default)private
ignoreNamePattern = (default)null
tokens = (default)ENUM_CONSTANT_DEF
-
*/
package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocvariable;
@@ -14,12 +12,12 @@ public class InputJavadocVariablePublicOnly // ignore - need javadoc
{
private interface InnerInterface // ignore - when not relaxed about Javadoc
{
- String CONST = "InnerInterface"; // violation
+ String CONST = "InnerInterface"; // violation, 'Missing a Javadoc comment'
void method(); // ignore - when not relaxed about Javadoc
class InnerInnerClass // ignore - when not relaxed about Javadoc
{
- private int mData; // violation
+ private int mData; // violation, 'Missing a Javadoc comment'
private InnerInnerClass()
{
@@ -39,17 +37,17 @@ void method2() // ignore - when not relaxed about Javadoc
private class InnerClass // ignore
{
- private int mDiff; // violation
+ private int mDiff; // violation, 'Missing a Javadoc comment'
void method() // ignore - when not relaxed about Javadoc
{
}
}
- private int mSize; // violation
- int mLen; // violation
- protected int mDeer; // violation
- public int aFreddo; // violation
+ private int mSize; // violation, 'Missing a Javadoc comment'
+ int mLen; // violation, 'Missing a Javadoc comment'
+ protected int mDeer; // violation, 'Missing a Javadoc comment'
+ public int aFreddo; // violation, 'Missing a Javadoc comment'
// ignore - need Javadoc
private InputJavadocVariablePublicOnly(int aA)
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly2.java
index 1e2876e035b..1085924a803 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly2.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariablePublicOnly2.java
@@ -1,11 +1,9 @@
/*
JavadocVariable
-scope = public
-excludeScope = (default)null
+accessModifiers = public
ignoreNamePattern = (default)null
tokens = (default)ENUM_CONSTANT_DEF
-
*/
package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocvariable;
@@ -14,7 +12,7 @@ public class InputJavadocVariablePublicOnly2 // ignore - need javadoc
{
private interface InnerInterface // ignore - when not relaxed about Javadoc
{
- String CONST = "InnerInterface"; // ignore - w.n.r.a.j
+ String CONST = "InnerInterface"; // violation, 'Missing a Javadoc comment'
void method(); // ignore - when not relaxed about Javadoc
class InnerInnerClass // ignore - when not relaxed about Javadoc
@@ -49,7 +47,7 @@ void method() // ignore - when not relaxed about Javadoc
private int mSize; // ignore - when not relaxed about Javadoc
int mLen; // ignore - when not relaxed about Javadoc
protected int mDeer; // ignore
- public int aFreddo; // violation
+ public int aFreddo; // violation, 'Missing a Javadoc comment'
// ignore - need Javadoc
private InputJavadocVariablePublicOnly2(int aA)
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableTags.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableTags.java
index beccae83a79..a808eccfc5f 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableTags.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/InputJavadocVariableTags.java
@@ -1,11 +1,9 @@
/*
JavadocVariable
-scope = (default)private
-excludeScope = (default)null
+accessModifiers = (default)private
ignoreNamePattern = (default)null
tokens = (default)ENUM_CONSTANT_DEF
-
*/
package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocvariable;
@@ -15,7 +13,7 @@
class InputJavadocVariableTags1
{
// Invalid - should be Javadoc
- private int mMissingJavadoc; // violation
+ private int mMissingJavadoc; // violation, 'Missing a Javadoc comment'
// Invalid - should be Javadoc
void method1()
@@ -308,14 +306,14 @@ public int foo(Object _arg) {
enum InputJavadocVariableTagsEnum
{
- CONSTANT_A, // violation
+ CONSTANT_A,
/**
*
*/
CONSTANT_B,
- CONSTANT_C // violation
+ CONSTANT_C
{
/**
*
@@ -334,7 +332,7 @@ public void someOtherMethod()
@interface InputJavadocVariableTagsAnnotation
{
String someField();
- int A_CONSTANT = 0; // violation
+ int A_CONSTANT = 0; // violation, 'Missing a Javadoc comment'
/** Some javadoc. */
int B_CONSTANT = 1;
/** @return This tag is valid here and expected with Java 8 */
diff --git a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheckExamplesTest.java b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheckExamplesTest.java
index e4e6e9f7415..ded43322ba0 100644
--- a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheckExamplesTest.java
+++ b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheckExamplesTest.java
@@ -54,8 +54,8 @@ public void testExample2() throws Exception {
@Test
public void testExample3() throws Exception {
final String[] expected = {
- "15:3: " + getCheckMessage(MSG_JAVADOC_MISSING),
- "23:15: " + getCheckMessage(MSG_JAVADOC_MISSING),
+ "14:3: " + getCheckMessage(MSG_JAVADOC_MISSING),
+ "22:15: " + getCheckMessage(MSG_JAVADOC_MISSING),
};
verifyWithInlineConfigParser(getPath("Example3.java"), expected);
diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example2.java b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example2.java
index e19d834316f..f9b0cbb4597 100644
--- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example2.java
+++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example2.java
@@ -2,7 +2,7 @@
-
+
diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example3.java b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example3.java
index dad03f68d39..d1eacc469a4 100644
--- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example3.java
+++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable/Example3.java
@@ -2,8 +2,7 @@
-
-
+
diff --git a/src/xdocs/checks/javadoc/javadocvariable.xml b/src/xdocs/checks/javadoc/javadocvariable.xml
index 4a4c9e1bbe8..8f8b82d452b 100644
--- a/src/xdocs/checks/javadoc/javadocvariable.xml
+++ b/src/xdocs/checks/javadoc/javadocvariable.xml
@@ -26,11 +26,11 @@
since
-
excludeScope
-
Specify the visibility scope where Javadoc comments are not checked.
By default, this setting will report a violation if
- there is no javadoc for any scope member.
+ there is no javadoc for a member with any access modifier.
- To configure the check for public
- scope:
+ To configure the check public
+ access modifier:
- To configure the check for members which are in private, but not in
- protected scope:
+ To configure the check for members which are in private or
+ package access modifier:
This setting will report a violation if there is no
- javadoc for private member and
- ignores protected member.
+ javadoc for private or package member.