public class VisibilityModifierCheck extends AbstractCheck
Public members are not flagged if the name matches the public member regular expression (contains "^serialVersionUID$" by default).
Rationale: Enforce encapsulation.Check also has options making it less strict:
ignoreAnnotationCanonicalNames - the list of annotations canonical names which ignore variables in consideration, if user will provide short annotation name that type will match to any named the same type without consideration of package, list by default:
For example such public field will be skipped by default value of list above:
@org.junit.Rule
public TemporaryFolder publicJUnitRule = new TemporaryFolder();
allowPublicFinalFields - which allows public final fields. Default value is false.
allowPublicImmutableFields - which allows immutable fields to be declared as public if defined in final class. Default value is false
Field is known to be immutable if:
Classes known to be immutable are listed in immutableClassCanonicalNames by their canonical names. List by default:
User can override this list via adding canonical class names to immutableClassCanonicalNames, if user will provide short class name all that type will match to any named the same type without consideration of package.
Rationale: Forcing all fields of class to have private modified by default is good in most cases, but in some cases it drawbacks in too much boilerplate get/set code. One of such cases are immutable classes.
Restriction: Check doesn't check if class is immutable, there's no checking if accessory methods are missing and all fields are immutable, we only check if current field is immutable by matching a name to user defined list of immutable classes and defined in final class
Star imports are out of scope of this Check. So if one of type imported via star import collides with user specified one by its short name - there won't be Check's violation.
Examples:The check will rise 3 violations if it is run with default configuration against the following code example:
public class ImmutableClass
{
public int intValue; // violation
public java.lang.String notes; // violation
public BigDecimal value; // violation
public ImmutableClass(int intValue, BigDecimal value, String notes)
{
this.intValue = intValue;
this.value = value;
this.notes = notes;
}
}
To configure the Check passing fields of type com.google.common.collect.ImmutableSet and java.util.List:
<module name="VisibilityModifier"> <property name="allowPublicImmutableFields" value="true"/> <property name="immutableClassCanonicalNames" value="java.util.List, com.google.common.collect.ImmutableSet"/> </module>
public final class ImmutableClass
{
public final ImmutableSet<String> includes; // No warning
public final ImmutableSet<String> excludes; // No warning
public final BigDecimal value; // Warning here, type BigDecimal isn't specified as immutable
public ImmutableClass(Collection<String> includes, Collection<String> excludes,
BigDecimal value)
{
this.includes = ImmutableSet.copyOf(includes);
this.excludes = ImmutableSet.copyOf(excludes);
this.value = value;
this.notes = notes;
}
}
To configure the Check passing fields annotated with
@com.annotation.CustomAnnotation:
<module name="VisibilityModifier"> <property name="ignoreAnnotationCanonicalNames" value=" com.annotation.CustomAnnotation"/> </module>
@com.annotation.CustomAnnotation String customAnnotated; // No warning
@CustomAnnotation String shortCustomAnnotated; // No warning
To configure the Check passing fields annotated with short annotation name
@CustomAnnotation:
<module name="VisibilityModifier"> <property name="ignoreAnnotationCanonicalNames" value="CustomAnnotation"/> </module>
@CustomAnnotation String customAnnotated; // No warning
@com.annotation.CustomAnnotation String customAnnotated1; // No warning
@mypackage.annotation.CustomAnnotation String customAnnotatedAnotherPackage; // another package but short name matches // so no violation
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
VisibilityModifierCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAst)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setAllowPublicFinalFields(boolean allow)
Sets whether public final fields are allowed.
|
void |
setAllowPublicImmutableFields(boolean allow)
Sets whether public immutable fields are allowed.
|
void |
setIgnoreAnnotationCanonicalNames(String... annotationNames)
Set the list of ignore annotations.
|
void |
setImmutableClassCanonicalNames(String... classNames)
Set the list of immutable classes types names.
|
void |
setPackageAllowed(boolean packageAllowed)
Set whether package visible members are allowed.
|
void |
setProtectedAllowed(boolean protectedAllowed)
Set whether protected members are allowed.
|
void |
setPublicMemberPattern(Pattern pattern)
Set the pattern for public members to ignore.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setClassLoader, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY
public VisibilityModifierCheck()
public void setIgnoreAnnotationCanonicalNames(String... annotationNames)
annotationNames
- array of ignore annotations canonical names.public void setProtectedAllowed(boolean protectedAllowed)
protectedAllowed
- whether protected members are allowedpublic void setPackageAllowed(boolean packageAllowed)
packageAllowed
- whether package visible members are allowedpublic void setPublicMemberPattern(Pattern pattern)
pattern
- pattern for public members to ignore.public void setAllowPublicImmutableFields(boolean allow)
allow
- user's value.public void setAllowPublicFinalFields(boolean allow)
allow
- user's value.public void setImmutableClassCanonicalNames(String... classNames)
classNames
- array of immutable types canonical names.public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST rootAst)
AbstractCheck
beginTree
in class AbstractCheck
rootAst
- the root of the treepublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processCopyright © 2001–2018. All rights reserved.