public class VisibilityModifierCheck extends Check
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();
allowPublicImmutableFields - which allows immutable fields be declared as public if defined in final class. Default value is true
Field is known to be immutable if:
Classes known to be immutable are listed in immutableClassCanonicalNames by their canonical names. List by default:
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:Default Check's configuration will pass the code below:
public final class ImmutableClass
{
public final int intValue; // No warning
public final java.lang.String notes; // No warning
public final BigDecimal value; // No warning
public ImmutableClass(int intValue, BigDecimal value, String notes)
{
this.includes = ImmutableSet.copyOf(includes);
this.excludes = ImmutableSet.copyOf(excludes);
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="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
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.
|
boolean |
isPackageAllowed() |
boolean |
isProtectedAllowed() |
void |
setAllowPublicImmutableFields(boolean allow)
Sets whether public immutable 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(String pattern)
Set the pattern for public members to ignore.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final String MSG_KEY
public boolean isProtectedAllowed()
public void setIgnoreAnnotationCanonicalNames(String... annotationNames)
annotationNames
- array of ignore annotations canonical names.public void setProtectedAllowed(boolean protectedAllowed)
protectedAllowed
- whether protected members are allowedpublic boolean isPackageAllowed()
public void setPackageAllowed(boolean packageAllowed)
packageAllowed
- whether package visible members are allowedpublic void setPublicMemberPattern(String pattern)
pattern
- pattern for public members to ignore.org.apache.commons.beanutils.ConversionException
- if unable to create Pattern objectpublic void setAllowPublicImmutableFields(boolean allow)
allow
- user's value.public void setImmutableClassCanonicalNames(String... classNames)
classNames
- array of immutable types canonical names.public int[] getDefaultTokens()
Check
getDefaultTokens
in class Check
TokenTypes
public int[] getAcceptableTokens()
Check
getAcceptableTokens
in class Check
TokenTypes
public void beginTree(DetailAST rootAst)
Check
public void visitToken(DetailAST ast)
Check
visitToken
in class Check
ast
- the token to processCopyright © 2001-2015. All Rights Reserved.