public final class MutableExceptionCheck extends AbstractCheck
Ensures that exceptions (classes with names conforming to some regular expression and explicitly extending classes with names conforming to other regular expression) are immutable. That is, they have only final fields.
Rationale: Exception instances should represent an error condition. Having non final fields not only allows the state to be modified by accident and therefore mask the original condition but also allows developers to accidentally forget to initialise state thereby leading to code catching the exception to draw incorrect conclusions based on the state.
| Modifier and Type | Field and Description |
|---|---|
private boolean |
checking
Should we check current class or not.
|
private java.util.Deque<java.lang.Boolean> |
checkingStack
Stack of checking information for classes.
|
private static java.lang.String |
DEFAULT_FORMAT
Default value for format and extendedClassNameFormat properties.
|
private java.util.regex.Pattern |
extendedClassNameFormat
Pattern for class name that is being extended.
|
private java.util.regex.Pattern |
format
The regexp to match against.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
MutableExceptionCheck() |
| Modifier and Type | Method and Description |
|---|---|
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.
|
private boolean |
isExtendedClassNamedAsException(DetailAST ast)
Checks that if extended class name conforms to specified format.
|
private boolean |
isNamedAsException(DetailAST ast)
Checks that a class name conforms to specified format.
|
private void |
leaveClassDef()
Called when we leave class definition.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
void |
setExtendedClassNameFormat(java.util.regex.Pattern extendedClassNameFormat)
Sets the format of extended class name to the specified regular expression.
|
void |
setFormat(java.util.regex.Pattern pattern)
Set the format for the specified regular expression.
|
private void |
visitClassDef(DetailAST ast)
Called when we start processing class definition.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
private void |
visitVariableDef(DetailAST ast)
Checks variable definition.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokensgetCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverityconfigure, contextualize, finishLocalSetup, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private static final java.lang.String DEFAULT_FORMAT
private final java.util.Deque<java.lang.Boolean> checkingStack
private java.util.regex.Pattern extendedClassNameFormat
private boolean checking
private java.util.regex.Pattern format
public void setExtendedClassNameFormat(java.util.regex.Pattern extendedClassNameFormat)
extendedClassNameFormat - a String valuepublic void setFormat(java.util.regex.Pattern pattern)
pattern - the new patternpublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingprivate void visitClassDef(DetailAST ast)
ast - class definition nodeprivate void leaveClassDef()
private void visitVariableDef(DetailAST ast)
ast - variable def node for checkprivate boolean isNamedAsException(DetailAST ast)
ast - class definition nodeprivate boolean isExtendedClassNamedAsException(DetailAST ast)
ast - class definition node