Class IllegalTypeCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public final class IllegalTypeCheck extends AbstractCheck
Checks that particular class are never used as types in variable declarations, return values or parameters.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:
- GregorianCalendar
- Hashtable
- ArrayList
- LinkedList
- Vector
as methods that are differ from interface methods are rear used, so in most cases user will benefit from checking for them.
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description IllegalTypeCheck()
Creates new instance of the check.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
DEFAULT_LEGAL_ABSTRACT_NAMES
private static final java.lang.String[] DEFAULT_LEGAL_ABSTRACT_NAMES
Abstract classes legal by default.
-
DEFAULT_ILLEGAL_TYPES
private static final java.lang.String[] DEFAULT_ILLEGAL_TYPES
Types illegal by default.
-
DEFAULT_IGNORED_METHOD_NAMES
private static final java.lang.String[] DEFAULT_IGNORED_METHOD_NAMES
Default ignored method names.
-
illegalClassNames
private final java.util.Set<java.lang.String> illegalClassNames
Illegal classes.
-
legalAbstractClassNames
private final java.util.Set<java.lang.String> legalAbstractClassNames
Legal abstract classes.
-
ignoredMethodNames
private final java.util.Set<java.lang.String> ignoredMethodNames
Methods which should be ignored.
-
memberModifiers
private java.util.List<java.lang.Integer> memberModifiers
Check methods and fields with only corresponding modifiers.
-
format
private java.util.regex.Pattern format
The regexp to match against.
-
validateAbstractClassNames
private boolean validateAbstractClassNames
Controls whether to validate abstract class names.
-
-
Method Detail
-
setFormat
public void setFormat(java.util.regex.Pattern pattern)
Set the format for the specified regular expression.- Parameters:
pattern
- a pattern.
-
setValidateAbstractClassNames
public void setValidateAbstractClassNames(boolean validateAbstractClassNames)
Sets whether to validate abstract class names.- Parameters:
validateAbstractClassNames
- whether abstract class names must be ignored.
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
isVerifiable
private boolean isVerifiable(DetailAST methodOrVariableDef)
Checks if current method's return type or variable's type is verifiable according to memberModifiers option.- Parameters:
methodOrVariableDef
- METHOD_DEF or VARIABLE_DEF ast node.- Returns:
- true if member is verifiable according to memberModifiers option.
-
isContainVerifiableType
private boolean isContainVerifiableType(DetailAST modifiers)
Checks is modifiers contain verifiable type.- Parameters:
modifiers
- parent node for all modifiers- Returns:
- true if method or variable can be verified
-
visitMethodDef
private void visitMethodDef(DetailAST methodDef)
Checks return type of a given method.- Parameters:
methodDef
- method for check.
-
visitParameterDef
private void visitParameterDef(DetailAST parameterDef)
Checks type of parameters.- Parameters:
parameterDef
- parameter list for check.
-
visitVariableDef
private void visitVariableDef(DetailAST variableDef)
Checks type of given variable.- Parameters:
variableDef
- variable to check.
-
visitImport
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.- Parameters:
importAst
-Import
-
isStarImport
private static boolean isStarImport(DetailAST importAst)
Checks if current import is star import. E.g.:import java.util.*;
- Parameters:
importAst
-Import
- Returns:
- true if it is star import
-
checkClassName
private void checkClassName(DetailAST ast)
Checks type of given method, parameter or variable.- Parameters:
ast
- node to check.
-
isMatchingClassName
private boolean isMatchingClassName(java.lang.String className)
Returns true if given class name is one of illegal classes or else false.- Parameters:
className
- class name to check.- Returns:
- true if given class name is one of illegal classes or if it matches to abstract class names pattern.
-
extendIllegalClassNamesWithShortName
private void extendIllegalClassNamesWithShortName(java.lang.String canonicalName)
Extends illegal class names set via imported short type name.- Parameters:
canonicalName
- Canonical name of imported type.
-
getImportedTypeCanonicalName
private static java.lang.String getImportedTypeCanonicalName(DetailAST importAst)
Gets imported type's canonical name.- Parameters:
importAst
-Import
- Returns:
- Imported canonical type's name.
-
getNextSubTreeNode
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).- Parameters:
currentNodeAst
- Current node in consideringsubTreeRootAst
- SubTree root- Returns:
- Current node after bypassing, if current node reached the root of a subtree method returns null
-
isCheckedMethod
private boolean isCheckedMethod(DetailAST ast)
Returns true if method has to be checked or false.- Parameters:
ast
- method def to check.- Returns:
- true if we should check this method.
-
setIllegalClassNames
public void setIllegalClassNames(java.lang.String... classNames)
Set the list of illegal variable types.- Parameters:
classNames
- array of illegal variable types
-
setIgnoredMethodNames
public void setIgnoredMethodNames(java.lang.String... methodNames)
Set the list of ignore method names.- Parameters:
methodNames
- array of ignored method names
-
setLegalAbstractClassNames
public void setLegalAbstractClassNames(java.lang.String... classNames)
Set the list of legal abstract class names.- Parameters:
classNames
- array of legal abstract class names
-
setMemberModifiers
public void setMemberModifiers(java.lang.String modifiers)
Set the list of member modifiers (of methods and fields) which should be checked.- Parameters:
modifiers
- String contains modifiers.
-
-