diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldName.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldName.java
index 3de3b839b52..3c3a8284935 100644
--- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldName.java
+++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldName.java
@@ -39,15 +39,20 @@
*
*
* - private static final int number = 1;
- *
- static final int otherNumber = 2;
*
*
* To the following:
*
*
* - private static final int NUMBER = 1;
- *
- static final int OTHER_NUMBER = 2;
*
+ *
+ * @apiNote This {@link BugChecker checker} has two optional flags:
+ *
+ * - ExcludedConstantFliedNames: A list of field names to exclude from this check.
+ *
- IncludePublicConstantFields: Whether to include public constants when running this
+ * check.
+ *
*/
@AutoService(BugChecker.class)
@BugPattern(
@@ -61,13 +66,16 @@ public final class CanonicalConstantFieldName extends BugChecker implements Vari
private static final long serialVersionUID = 1L;
private static final Matcher IS_CONSTANT =
allOf(hasModifier(Modifier.STATIC), hasModifier(Modifier.FINAL));
+ private static final Matcher IS_PRIVATE = hasModifier(Modifier.PRIVATE);
private static final Pattern TO_SNAKE_CASE = Pattern.compile("([a-z])([A-Z])");
private static final ImmutableSet DEFAULT_EXCLUDED_CONSTANT_FIELD_NAMES =
ImmutableSet.of("serialVersionUID");
private static final String EXCLUDED_CONSTANT_FIELD_NAMES =
"CanonicalConstantFieldName:ExcludedConstantFliedNames";
-
+ private static final String IS_INCLUDE_PUBLIC_CONSTANT_FIELDS =
+ "CanonicalConstantFieldName:IncludePublicConstantFields";
private final ImmutableList optionalExcludedConstantFliedNames;
+ private final boolean includePublicConstantFieldNames;
/** Instantiates a default {@link CanonicalConstantFieldName} instance. */
public CanonicalConstantFieldName() {
@@ -82,11 +90,12 @@ public CanonicalConstantFieldName() {
@Inject
CanonicalConstantFieldName(ErrorProneFlags flags) {
optionalExcludedConstantFliedNames = getCanonicalizedLoggerName(flags);
+ includePublicConstantFieldNames = getIncludePrivateConstantFieldNames(flags);
}
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
- if (!IS_CONSTANT.matches(tree, state)) {
+ if (!(IS_CONSTANT.matches(tree, state) && isFieldAccessModifierApplicable(tree, state))) {
return Description.NO_MATCH;
}
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
@@ -101,6 +110,10 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
return fixBuilder.isEmpty() ? Description.NO_MATCH : describeMatch(tree, fixBuilder.build());
}
+ private boolean isFieldAccessModifierApplicable(VariableTree tree, VisitorState state) {
+ return includePublicConstantFieldNames || IS_PRIVATE.matches(tree, state);
+ }
+
private static boolean isVariableUpperSnakeCase(String variableName) {
return variableName.equals(toUpperSnakeCase(variableName));
}
@@ -117,4 +130,8 @@ private static String toUpperSnakeCase(String variableName) {
private static ImmutableList getCanonicalizedLoggerName(ErrorProneFlags flags) {
return Flags.getList(flags, EXCLUDED_CONSTANT_FIELD_NAMES);
}
+
+ private static boolean getIncludePrivateConstantFieldNames(ErrorProneFlags flags) {
+ return flags.getBoolean(IS_INCLUDE_PUBLIC_CONSTANT_FIELDS).orElse(false);
+ }
}
diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldNameTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldNameTest.java
index 56dadbcc51a..36c29667ba5 100644
--- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldNameTest.java
+++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/CanonicalConstantFieldNameTest.java
@@ -13,10 +13,6 @@ void replacement() {
"class A {",
" private static final int number = 1;",
"",
- " static final int otherNumber = 2;",
- "",
- " static final int ANOTHER_NUMBER = 3;",
- "",
" static int getNumber() {",
" return number;",
" }",
@@ -26,10 +22,6 @@ void replacement() {
"class A {",
" private static final int NUMBER = 1;",
"",
- " static final int OTHER_NUMBER = 2;",
- "",
- " static final int ANOTHER_NUMBER = 3;",
- "",
" static int getNumber() {",
" return NUMBER;",
" }",