Class ImportControlCheck
- 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.imports.ImportControlCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,ExternalResourceHolder
public class ImportControlCheck extends AbstractCheck implements ExternalResourceHolder
Check that controls what packages can be imported in each package. Useful for ensuring that application layering is not violated. Ideas on how the check can be improved include support for:- Change the default policy that if a package being checked does not match any guards, then it is allowed. Currently defaults to disallowed.
-
-
Field Summary
Fields Modifier and Type Field Description private ImportControl
currentImportControl
The package controller for the current file.private java.lang.String
fileLocation
Location of import control file.static java.lang.String
MSG_DISALLOWED
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_MISSING_FILE
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_UNKNOWN_PKG
A key is pointing to the warning message text in "messages.properties" file.private java.lang.String
packageName
The package doing the import.private java.util.regex.Pattern
path
The filepath pattern this check applies to.private boolean
processCurrentFile
Whether to process the current file.private ImportControl
root
The root package controller.private static java.lang.String
UNABLE_TO_LOAD
A part of message for exception.
-
Constructor Summary
Constructors Constructor Description ImportControlCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method 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.java.util.Set<java.lang.String>
getExternalResourceLocations()
Returns a set of external configuration resource locations which are used by the module.private static java.lang.String
getImportText(DetailAST ast)
Returns import text.private static java.lang.String
getPackageText(DetailAST ast)
Returns package text.int[]
getRequiredTokens()
The tokens that this check must be registered for.void
setFile(java.net.URI uri)
Set the name for the file containing the import control configuration.void
setPath(java.util.regex.Pattern pattern)
Set the file path pattern that this check applies to.void
setUrl(java.net.URI uri)
Deprecated.usesetFile(URI uri)
to load URLs insteadvoid
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
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_MISSING_FILE
public static final java.lang.String MSG_MISSING_FILE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_UNKNOWN_PKG
public static final java.lang.String MSG_UNKNOWN_PKG
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_DISALLOWED
public static final java.lang.String MSG_DISALLOWED
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
UNABLE_TO_LOAD
private static final java.lang.String UNABLE_TO_LOAD
A part of message for exception.- See Also:
- Constant Field Values
-
fileLocation
private java.lang.String fileLocation
Location of import control file.
-
path
private java.util.regex.Pattern path
The filepath pattern this check applies to.
-
processCurrentFile
private boolean processCurrentFile
Whether to process the current file.
-
root
private ImportControl root
The root package controller.
-
packageName
private java.lang.String packageName
The package doing the import.
-
currentImportControl
private ImportControl currentImportControl
The package controller for the current file. Used for performance optimisation.
-
-
Method Detail
-
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
-
beginTree
public void beginTree(DetailAST rootAST)
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
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
-
getExternalResourceLocations
public java.util.Set<java.lang.String> getExternalResourceLocations()
Description copied from interface:ExternalResourceHolder
Returns a set of external configuration resource locations which are used by the module. ATTENTION! If 'getExternalResourceLocations()' return null, there will beNullPointerException
inChecker
. Such behaviour will signal that your module (check or filter) is designed incorrectly. It make sense to return an empty set from 'getExternalResourceLocations()' only for composite modules likeTreeWalker
.- Specified by:
getExternalResourceLocations
in interfaceExternalResourceHolder
- Returns:
- a set of external configuration resource locations which are used by the module.
-
getPackageText
private static java.lang.String getPackageText(DetailAST ast)
Returns package text.- Parameters:
ast
- PACKAGE_DEF ast node- Returns:
- String that represents full package name
-
getImportText
private static java.lang.String getImportText(DetailAST ast)
Returns import text.- Parameters:
ast
- ast node that represents import- Returns:
- String that represents importing class
-
setFile
public void setFile(java.net.URI uri)
Set the name for the file containing the import control configuration. It can also be a URL or resource in the classpath. It will cause the file to be loaded.- Parameters:
uri
- the uri of the file to load.- Throws:
java.lang.IllegalArgumentException
- on error loading the file.
-
setPath
public void setPath(java.util.regex.Pattern pattern)
Set the file path pattern that this check applies to.- Parameters:
pattern
- the file path regex this check should apply to.
-
setUrl
@Deprecated public void setUrl(java.net.URI uri)
Deprecated.usesetFile(URI uri)
to load URLs insteadSet the parameter for the url containing the import control configuration. It will cause the url to be loaded.- Parameters:
uri
- the uri of the file to load.- Throws:
org.apache.commons.beanutils.ConversionException
- on error loading the file.
-
-