diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheck.java index 75982d48542..a9c616fc37d 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocVariableCheck.java @@ -19,6 +19,7 @@ package com.puppycrawl.tools.checkstyle.checks.javadoc; +import java.util.Arrays; import java.util.regex.Pattern; import com.puppycrawl.tools.checkstyle.StatelessCheck; @@ -28,6 +29,7 @@ import com.puppycrawl.tools.checkstyle.api.Scope; import com.puppycrawl.tools.checkstyle.api.TextBlock; import com.puppycrawl.tools.checkstyle.api.TokenTypes; +import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption; import com.puppycrawl.tools.checkstyle.utils.ScopeUtil; /** @@ -36,15 +38,10 @@ *
*- * To configure the check for {@code public} scope: + * To configure the check for {@code public} and {@code protected} variables: *
** <module name="JavadocVariable"> - * <property name="scope" value="public"/> - * </module> - *- *
- * To configure the check for members which are in {@code private}, - * but not in {@code protected} scope: - *
- *- * <module name="JavadocVariable"> - * <property name="scope" value="private"/> - * <property name="excludeScope" value="protected"/> + * <property name="accessModifiers" value="public,protected"/> * </module> **
@@ -116,31 +103,25 @@ public class JavadocVariableCheck
*/
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;
+ /** Specify the access modifiers where Javadoc comments 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.
- *
- * @param scope a scope.
- */
- public void setScope(Scope scope) {
- this.scope = scope;
- }
-
- /**
- * Setter to specify the visibility scope where Javadoc comments are not checked.
+ * Setter to specify the access modifiers where Javadoc comments are checked.
*
- * @param excludeScope a scope.
+ * @param accessModifiers access modifiers of variables which should be checked.
*/
- public void setExcludeScope(Scope excludeScope) {
- this.excludeScope = excludeScope;
+ public void setAccessModifiers(AccessModifierOption... accessModifiers) {
+ this.accessModifiers =
+ Arrays.copyOf(accessModifiers, accessModifiers.length);
}
/**
@@ -218,12 +199,54 @@ private boolean shouldCheck(final DetailAST ast) {
}
final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast);
- result = customScope.isIn(scope) && surroundingScope.isIn(scope)
- && (excludeScope == null
- || !customScope.isIn(excludeScope)
- || !surroundingScope.isIn(excludeScope));
+
+ final Scope effectiveScope;
+ if (surroundingScope.isIn(customScope)) {
+ effectiveScope = customScope;
+ }
+ else {
+ effectiveScope = surroundingScope;
+ }
+ result = matchAccessModifiers(toAccessModifier(effectiveScope));
}
return result;
}
+ /**
+ * Checks whether a variable has the correct access modifier to be checked.
+ *
+ * @param accessModifier the access modifier of the variable.
+ * @return whether the variable matches the expected access modifier.
+ */
+ private boolean matchAccessModifiers(final AccessModifierOption accessModifier) {
+ return Arrays.stream(accessModifiers)
+ .anyMatch(modifier -> modifier == accessModifier);
+ }
+
+ /**
+ * Converts a {@link Scope} to {@link AccessModifierOption}. {@code Scope.NOTHING} and {@code
+ * Scope.ANONINNER} are converted to {@code AccessModifierOption.PUBLIC}.
+ *
+ * @param scope Scope to be converted.
+ * @return the corresponding AccessModifierOption.
+ */
+ private static AccessModifierOption toAccessModifier(Scope scope) {
+ final AccessModifierOption accessModifier;
+ switch (scope) {
+ case PROTECTED:
+ accessModifier = AccessModifierOption.PROTECTED;
+ break;
+ case PACKAGE:
+ accessModifier = AccessModifierOption.PACKAGE;
+ break;
+ case PRIVATE:
+ accessModifier = AccessModifierOption.PRIVATE;
+ break;
+ // $CASES-OMITTED$
+ default:
+ accessModifier = AccessModifierOption.PUBLIC;
+ }
+ return accessModifier;
+ }
+
}
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 aea982226cd..a5e7ea0806f 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,14 +8,11 @@
Checks that a variable has a Javadoc comment. Ignores {@code serialVersionUID} fields.
</p>
- To configure the check for
- To configure the check for members which are in since
-
- scope
- Specify the visibility scope where Javadoc comments are checked.
- Scope
-
+ private
accessModifiers
+ Specify the access modifiers where Javadoc comments are checked.
+ AccessModifierOption[]
+
+
+ public, protected, package, private
3.0
-
excludeScope
- Specify the visibility scope where Javadoc comments are not checked.
- Scope
-
- null
3.4
-
ignoreNamePattern
Specify the regexp to define variable names to ignore.
@@ -2337,25 +2332,13 @@ class DatabaseConfiguration {}
public
- scope:
- private
, but not in
- protected
scope:
+ To configure the check for public
and protected
+ variables: