public final class IllegalTypeCheck extends AbstractCheck
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
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:
as methods that are differ from interface methods are rarely used, so in most cases user will benefit from checking for them.
validateAbstractClassNames - Control whether to validate abstract class names.
Default value is false.
illegalClassNames - Specify classes that should not be used
as types in variable declarations, return values or parameters.
Default value is HashMap, HashSet, LinkedHashMap, LinkedHashSet, TreeMap,
TreeSet, java.util.HashMap, java.util.HashSet, java.util.LinkedHashMap,
java.util.LinkedHashSet, java.util.TreeMap, java.util.TreeSet.
legalAbstractClassNames - Define abstract classes that may be used as types.
Default value is {}.
ignoredMethodNames - Specify methods that should not be checked.
Default value is getEnvironment, getInitialContext.
illegalAbstractClassNameFormat - Specify RegExp for illegal abstract class
names.
Default value is "^(.*[.])?Abstract.*$".
memberModifiers - Control whether to check only methods and fields with any
of the specified modifiers.
This property does not affect method calls nor method references.
Default value is no tokens.
tokens - tokens to check
Default value is:
ANNOTATION_FIELD_DEF,
CLASS_DEF,
INTERFACE_DEF,
METHOD_CALL,
METHOD_DEF,
METHOD_REF,
PARAMETER_DEF,
VARIABLE_DEF.
To configure the check so that it ignores getInstance() methods:
<module name="IllegalType"> <property name="ignoredMethodNames" value="getInstance"/> </module>
To configure the Check so that it verifies only public, protected or static methods and fields:
<module name="IllegalType">
<property name="memberModifiers" value="LITERAL_PUBLIC,
LITERAL_PROTECTED, LITERAL_STATIC"/>
</module>
To configure the check so that it verifies usage of types Boolean and Foo:
<module name="IllegalType">
<property name="illegalClassNames" value="Boolean, Foo"/>
</module>
public class Test {
public Set<Boolean> set; // violation
public java.util.List<Map<Boolean, Foo>> list; // violation
private void method(List<Foo> list, Boolean value) { // violation
SomeType.<Boolean>foo(); // violation
final Consumer<Foo> consumer = Foo<Boolean>::foo; // violation
}
public <T extends Boolean, U extends Serializable> void typeParam(T a) {} // violation
public void fullName(java.util.ArrayList<? super Boolean> a) {} // violation
public abstract Set<Boolean> shortName(Set<? super Boolean> a); // violation
public Set<? extends Foo> typeArgument() { // violation
return new TreeSet<Foo<Boolean>>();
}
}
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
IllegalTypeCheck()
Creates new instance of the check.
|
| 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.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
setIgnoredMethodNames(java.lang.String... methodNames)
Setter to specify methods that should not be checked.
|
void |
setIllegalAbstractClassNameFormat(java.util.regex.Pattern pattern)
Setter to specify RegExp for illegal abstract class names.
|
void |
setIllegalClassNames(java.lang.String... classNames)
Setter to specify classes that should not be used as types in variable declarations,
return values or parameters.
|
void |
setLegalAbstractClassNames(java.lang.String... classNames)
Setter to define abstract classes that may be used as types.
|
void |
setMemberModifiers(java.lang.String modifiers)
Setter to control whether to check only methods and fields with any of
the specified modifiers.
|
void |
setValidateAbstractClassNames(boolean validateAbstractClassNames)
Setter to control whether to validate abstract class names.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
public IllegalTypeCheck()
public void setIllegalAbstractClassNameFormat(java.util.regex.Pattern pattern)
pattern - a pattern.public void setValidateAbstractClassNames(boolean validateAbstractClassNames)
validateAbstractClassNames - whether abstract class names must be ignored.public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic 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.Copyright © 2001-2020. All Rights Reserved.