public final class IllegalTypeCheck extends AbstractCheck
Rationale: Helps reduce coupling on concrete classes.
Check has following properties:
format - Pattern for illegal class names.
legalAbstractClassNames - Abstract classes that may be used as types.
illegalClassNames - Classes that should not be used as types in variable declarations, return values or parameters. It is possible to set illegal class names via short or canonical name. Specifying illegal type invokes analyzing imports and Check puts violations at corresponding declarations (of variables, methods or parameters). This helps to avoid ambiguous cases, e.g.:
java.awt.List
was set as illegal class name, then, code like:
import java.util.List;<br>
...<br>
List list; //No violation here
will be ok.
validateAbstractClassNames - controls whether to validate abstract class names. Default value is false
ignoredMethodNames - Methods that should not be checked.
memberModifiers - To check only methods and fields with only specified modifiers.
In most cases it's justified to put following classes to illegalClassNames:
as methods that are differ from interface methods are rear used, so in most cases user will benefit from checking for them.
Modifier and Type | Field and Description |
---|---|
private static java.lang.String[] |
DEFAULT_IGNORED_METHOD_NAMES
Default ignored method names.
|
private static java.lang.String[] |
DEFAULT_ILLEGAL_TYPES
Types illegal by default.
|
private static java.lang.String[] |
DEFAULT_LEGAL_ABSTRACT_NAMES
Abstract classes legal by default.
|
private java.util.regex.Pattern |
format
The regexp to match against.
|
private java.util.Set<java.lang.String> |
ignoredMethodNames
Methods which should be ignored.
|
private java.util.Set<java.lang.String> |
illegalClassNames
Illegal classes.
|
private java.util.Set<java.lang.String> |
legalAbstractClassNames
Legal abstract classes.
|
private java.util.List<java.lang.Integer> |
memberModifiers
Check methods and fields with only corresponding modifiers.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
private boolean |
validateAbstractClassNames
Controls whether to validate abstract class names.
|
Constructor and Description |
---|
IllegalTypeCheck()
Creates new instance of the check.
|
Modifier and Type | Method and Description |
---|---|
private void |
checkClassName(DetailAST ast)
Checks type of given method, parameter or variable.
|
private void |
extendIllegalClassNamesWithShortName(java.lang.String canonicalName)
Extends illegal class names set via imported short type name.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static java.lang.String |
getImportedTypeCanonicalName(DetailAST importAst)
Gets imported type's
canonical name.
|
private static DetailAST |
getNextSubTreeNode(DetailAST currentNodeAst,
DetailAST subTreeRootAst)
Gets the next node of a syntactical tree (child of a current node or
sibling of a current node, or sibling of a parent of a current node).
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private boolean |
isCheckedMethod(DetailAST ast)
Returns true if method has to be checked or false.
|
private boolean |
isContainVerifiableType(DetailAST modifiers)
Checks is modifiers contain verifiable type.
|
private boolean |
isMatchingClassName(java.lang.String className)
Returns true if given class name is one of illegal classes or else false.
|
private static boolean |
isStarImport(DetailAST importAst)
Checks if current import is star import.
|
private boolean |
isVerifiable(DetailAST methodOrVariableDef)
Checks if current method's return type or variable's type is verifiable
according to memberModifiers option.
|
void |
setFormat(java.util.regex.Pattern pattern)
Set the format for the specified regular expression.
|
void |
setIgnoredMethodNames(java.lang.String... methodNames)
Set the list of ignore method names.
|
void |
setIllegalClassNames(java.lang.String... classNames)
Set the list of illegal variable types.
|
void |
setLegalAbstractClassNames(java.lang.String... classNames)
Set the list of legal abstract class names.
|
void |
setMemberModifiers(java.lang.String modifiers)
Set the list of member modifiers (of methods and fields) which should be checked.
|
void |
setValidateAbstractClassNames(boolean validateAbstractClassNames)
Sets whether to validate abstract class names.
|
private void |
visitImport(DetailAST importAst)
Checks imported type (as static and star imports are not supported by Check,
only type is in the consideration).
If this type is illegal due to Check's options - puts violation on it. |
private void |
visitMethodDef(DetailAST methodDef)
Checks return type of a given method.
|
private void |
visitParameterDef(DetailAST parameterDef)
Checks type of parameters.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
private void |
visitVariableDef(DetailAST variableDef)
Checks type of given variable.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, 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 java.lang.String MSG_KEY
private static final java.lang.String[] DEFAULT_LEGAL_ABSTRACT_NAMES
private static final java.lang.String[] DEFAULT_ILLEGAL_TYPES
private static final java.lang.String[] DEFAULT_IGNORED_METHOD_NAMES
private final java.util.Set<java.lang.String> illegalClassNames
private final java.util.Set<java.lang.String> legalAbstractClassNames
private final java.util.Set<java.lang.String> ignoredMethodNames
private java.util.List<java.lang.Integer> memberModifiers
private java.util.regex.Pattern format
private boolean validateAbstractClassNames
public void setFormat(java.util.regex.Pattern pattern)
pattern
- a pattern.public void setValidateAbstractClassNames(boolean validateAbstractClassNames)
validateAbstractClassNames
- whether abstract class names must be ignored.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 visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate boolean isVerifiable(DetailAST methodOrVariableDef)
methodOrVariableDef
- METHOD_DEF or VARIABLE_DEF ast node.private boolean isContainVerifiableType(DetailAST modifiers)
modifiers
- parent node for all modifiersprivate void visitMethodDef(DetailAST methodDef)
methodDef
- method for check.private void visitParameterDef(DetailAST parameterDef)
parameterDef
- parameter list for check.private void visitVariableDef(DetailAST variableDef)
variableDef
- variable to check.private void visitImport(DetailAST importAst)
importAst
- Import
private static boolean isStarImport(DetailAST importAst)
import java.util.*;
importAst
- Import
private void checkClassName(DetailAST ast)
ast
- node to check.private boolean isMatchingClassName(java.lang.String className)
className
- class name to check.private void extendIllegalClassNamesWithShortName(java.lang.String canonicalName)
canonicalName
-
Canonical name of imported type.private static java.lang.String getImportedTypeCanonicalName(DetailAST importAst)
importAst
- Import
private static DetailAST getNextSubTreeNode(DetailAST currentNodeAst, DetailAST subTreeRootAst)
currentNodeAst
- Current node in consideringsubTreeRootAst
- SubTree rootprivate boolean isCheckedMethod(DetailAST ast)
ast
- method def to check.public void setIllegalClassNames(java.lang.String... classNames)
classNames
- array of illegal variable typespublic void setIgnoredMethodNames(java.lang.String... methodNames)
methodNames
- array of ignored method namespublic void setLegalAbstractClassNames(java.lang.String... classNames)
classNames
- array of legal abstract class namespublic void setMemberModifiers(java.lang.String modifiers)
modifiers
- String contains modifiers.