Class IllegalTypeCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck
- All Implemented Interfaces:
- Configurable,- Contextualizable
 Checks that particular classes or interfaces are never used.
 
 Rationale: Helps reduce coupling on concrete classes.
For additional restriction of type usage see also: IllegalInstantiation, IllegalImport
 Notes:
 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;
 ...
 List list; //No violation here
 will be ok.
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 rarely used, so in most cases user will benefit from checking for them.
- Since:
- 3.2
- 
Nested Class SummaryNested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanAbstractAutomaticBean.OutputStreamOptions
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprivate static final String[]Default ignored method names.private static final String[]Types illegal by default.Specify methods that should not be checked.private PatternSpecify RegExp for illegal abstract class names.Specify classes that should not be used as types in variable declarations, return values or parameters.Illegal short classes.Define abstract classes that may be used as types.private BitSetControl whether to check only methods and fields with any of the specified modifiers.static final StringA key is pointing to the warning message text in "messages.properties" file.private booleanControl whether to validate abstract class names.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.private voidcheckBaseTypes(DetailAST clause) Checks theextendsorimplementsstatement.private voidcheckClassName(DetailAST ast) Checks type and type arguments/parameters of given method, parameter, variable or method call/reference.private voidcheckIdent(DetailAST type) Checks the identifier of the given type.private voidChecks the given type, its arguments and parameters.private voidcheckTypeArguments(DetailAST node) Checks the type arguments of the node.private voidcheckTypeBounds(DetailAST type) Checks the upper and lower bounds for the given type.private voidcheckTypeParameters(DetailAST node) Checks the type parameters of the node.private voidextendIllegalClassNamesWithShortName(String canonicalName) Extends illegal class names set via imported short type name.int[]The configurable token set.int[]Returns the default token a check is interested in.private static StringgetImportedTypeCanonicalName(DetailAST importAst) Gets imported type's canonical name.private static DetailASTgetNextSubTreeNode(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[]The tokens that this check must be registered for.private booleanisCheckedMethod(DetailAST ast) Returns true if method has to be checked or false.private booleanisContainVerifiableType(DetailAST modifiers) Checks is modifiers contain verifiable type.private booleanisMatchingClassName(String className) Returns true if given class name is one of illegal classes or else false.private static booleanisStarImport(DetailAST importAst) Checks if current import is star import.private booleanisVerifiable(DetailAST methodOrVariableDef) Checks if current method's return type or variable's type is verifiable according to memberModifiers option.voidsetIgnoredMethodNames(String... methodNames) Setter to specify methods that should not be checked.voidsetIllegalAbstractClassNameFormat(Pattern pattern) Setter to specify RegExp for illegal abstract class names.voidsetIllegalClassNames(String... classNames) Setter to specify classes that should not be used as types in variable declarations, return values or parameters.voidsetLegalAbstractClassNames(String... classNames) Setter to define abstract classes that may be used as types.voidsetMemberModifiers(String modifiers) Setter to control whether to check only methods and fields with any of the specified modifiers.voidsetValidateAbstractClassNames(boolean validateAbstractClassNames) Setter to control whether to validate abstract class names.private voidvisitImport(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 voidvisitMethodCallOrRef(DetailAST methodCallOrRef) Checks the type arguments of given method call/reference.private voidvisitMethodDef(DetailAST methodDef) Checks return type of a given method.private voidvisitParameterDef(DetailAST parameterDef) Checks type of parameters.voidvisitToken(DetailAST ast) Called to process a token.private voidvisitTypeDef(DetailAST typeDef) Checks the super type and implemented interfaces of a given type.private voidvisitVariableDef(DetailAST variableDef) Checks type of given variable.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheckclearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporterfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanconfigure, contextualize, getConfiguration, setupChild
- 
Field Details- 
MSG_KEYA key is pointing to the warning message text in "messages.properties" file.- See Also:
 
- 
DEFAULT_ILLEGAL_TYPESTypes illegal by default.
- 
DEFAULT_IGNORED_METHOD_NAMESDefault ignored method names.
- 
illegalClassNamesSpecify classes that should not be used as types in variable declarations, return values or parameters.
- 
illegalShortClassNamesIllegal short classes.
- 
legalAbstractClassNamesDefine abstract classes that may be used as types.
- 
ignoredMethodNamesSpecify methods that should not be checked.
- 
memberModifiersControl whether to check only methods and fields with any of the specified modifiers. This property does not affect method calls nor method references nor record components.
- 
illegalAbstractClassNameFormatSpecify RegExp for illegal abstract class names.
- 
validateAbstractClassNamesControl whether to validate abstract class names.
 
- 
- 
Constructor Details- 
IllegalTypeCheckpublic IllegalTypeCheck()Creates new instance of the check.
 
- 
- 
Method Details- 
setIllegalAbstractClassNameFormatSetter to specify RegExp for illegal abstract class names.- Parameters:
- pattern- a pattern.
- Since:
- 3.2
 
- 
setValidateAbstractClassNamesSetter to control whether to validate abstract class names.- Parameters:
- validateAbstractClassNames- whether abstract class names must be ignored.
- Since:
- 6.10
 
- 
getDefaultTokensDescription copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
- getDefaultTokensin class- AbstractCheck
- Returns:
- the default tokens
- See Also:
 
- 
getAcceptableTokensDescription copied from class:AbstractCheckThe 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:
- getAcceptableTokensin class- AbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
 
- 
beginTreeDescription copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
- beginTreein class- AbstractCheck
- Parameters:
- rootAST- the root of the tree
 
- 
getRequiredTokensDescription copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
- getRequiredTokensin class- AbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
 
- 
visitTokenDescription copied from class:AbstractCheckCalled to process a token.- Overrides:
- visitTokenin class- AbstractCheck
- Parameters:
- ast- the token to process
 
- 
isVerifiableChecks 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.
 
- 
isContainVerifiableTypeChecks is modifiers contain verifiable type.- Parameters:
- modifiers- parent node for all modifiers
- Returns:
- true if method or variable can be verified
 
- 
visitTypeDefChecks the super type and implemented interfaces of a given type.- Parameters:
- typeDef- class or interface for check.
 
- 
visitMethodDefChecks return type of a given method.- Parameters:
- methodDef- method for check.
 
- 
visitParameterDefChecks type of parameters.- Parameters:
- parameterDef- parameter list for check.
 
- 
visitVariableDefChecks type of given variable.- Parameters:
- variableDef- variable to check.
 
- 
visitMethodCallOrRefChecks the type arguments of given method call/reference.- Parameters:
- methodCallOrRef- method call/reference to check.
 
- 
visitImportChecks 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
 
- 
isStarImportChecks if current import is star import. E.g.:import java.util.*;- Parameters:
- importAst-- Import
- Returns:
- true if it is star import
 
- 
checkClassNameChecks type and type arguments/parameters of given method, parameter, variable or method call/reference.- Parameters:
- ast- node to check.
 
- 
checkIdentChecks the identifier of the given type.- Parameters:
- type- node to check.
 
- 
checkBaseTypesChecks theextendsorimplementsstatement.- Parameters:
- clause- DetailAST for either- TokenTypes.EXTENDS_CLAUSEor- TokenTypes.IMPLEMENTS_CLAUSE
 
- 
checkTypeChecks the given type, its arguments and parameters.- Parameters:
- type- node to check.
 
- 
checkTypeBoundsChecks the upper and lower bounds for the given type.- Parameters:
- type- node to check.
 
- 
checkTypeParametersChecks the type parameters of the node.- Parameters:
- node- node to check.
 
- 
checkTypeArgumentsChecks the type arguments of the node.- Parameters:
- node- node to check.
 
- 
isMatchingClassNameReturns 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.
 
- 
extendIllegalClassNamesWithShortNameExtends illegal class names set via imported short type name.- Parameters:
- canonicalName- Canonical name of imported type.
 
- 
getImportedTypeCanonicalNameGets imported type's canonical name.- Parameters:
- importAst-- Import
- Returns:
- Imported canonical type's name.
 
- 
getNextSubTreeNodeGets 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 considering
- subTreeRootAst- SubTree root
- Returns:
- Current node after bypassing, if current node reached the root of a subtree method returns null
 
- 
isCheckedMethodReturns true if method has to be checked or false.- Parameters:
- ast- method def to check.
- Returns:
- true if we should check this method.
 
- 
setIllegalClassNamesSetter to specify classes that should not be used as types in variable declarations, return values or parameters.- Parameters:
- classNames- array of illegal variable types
- Since:
- 3.2
 
- 
setIgnoredMethodNamesSetter to specify methods that should not be checked.- Parameters:
- methodNames- array of ignored method names
- Since:
- 3.2
 
- 
setLegalAbstractClassNamesSetter to define abstract classes that may be used as types.- Parameters:
- classNames- array of legal abstract class names
- Since:
- 4.2
 
- 
setMemberModifiersSetter to control whether to check only methods and fields with any of the specified modifiers. This property does not affect method calls nor method references nor record components.- Parameters:
- modifiers- String contains modifiers.
- Since:
- 6.3
 
 
-