Class AbstractClassCouplingCheck
- 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.metrics.AbstractClassCouplingCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
- Direct Known Subclasses:
ClassDataAbstractionCouplingCheck
,ClassFanOutComplexityCheck
public abstract class AbstractClassCouplingCheck extends AbstractCheck
Base class for coupling calculation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
AbstractClassCouplingCheck.ClassContext
Encapsulates information about class coupling.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private static Pattern
BRACKET_PATTERN
Pattern to match brackets in a full type name.private Deque<AbstractClassCouplingCheck.ClassContext>
classesContexts
Stack of class contexts.private static Set<String>
DEFAULT_EXCLUDED_CLASSES
Class names to ignore.private static Set<String>
DEFAULT_EXCLUDED_PACKAGES
Package names to ignore.private static char
DOT
A package separator - ".".private List<Pattern>
excludeClassesRegexps
Specify user-configured regular expressions to ignore classes.private Set<String>
excludedClasses
Specify user-configured class names to ignore.private Set<String>
excludedPackages
Specify user-configured packages to ignore.private Map<String,String>
importedClassPackages
A map of (imported class name -> class name with package) pairs.private int
max
Specify the maximum threshold allowed.private String
packageName
Current file package.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractClassCouplingCheck(int defaultMax)
Creates new instance of the check.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
beginTree(DetailAST ast)
Called before the starting to process a tree.private void
checkCurrentClassAndRestorePrevious()
Restores previous context.private void
createNewClassContext(String className, DetailAST ast)
Creates new inner class context with given name and location.int[]
getDefaultTokens()
Returns the default token a check is interested in.protected abstract String
getLogMessageId()
Returns message key we use for log violations.private void
leaveClassDef()
Restores previous context.void
leaveToken(DetailAST ast)
Called after all the child nodes have been process.private void
registerImport(DetailAST imp)
Registers given import.void
setExcludeClassesRegexps(Pattern... from)
Setter to specify user-configured regular expressions to ignore classes.void
setExcludedClasses(String... excludedClasses)
Setter to specify user-configured class names to ignore.void
setExcludedPackages(String... excludedPackages)
Setter to specify user-configured packages to ignore.void
setMax(int max)
Setter to specify the maximum threshold allowed.private void
visitAnnotationType(DetailAST annotationAST)
Visit ANNOTATION literal and get its type to referenced classes of context.private void
visitClassDef(DetailAST classDef)
Creates new context for a given class.private void
visitLiteralNew(DetailAST ast)
Visits NEW token for the current class context.private void
visitLiteralThrows(DetailAST ast)
Visits THROWS token for the current class context.private void
visitPackageDef(DetailAST pkg)
Stores package of current class we check.void
visitToken(DetailAST ast)
Called to process a token.private void
visitType(DetailAST ast)
Visits type token for the current class context.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getAcceptableTokens, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getRequiredTokens, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
DOT
private static final char DOT
A package separator - ".".- See Also:
- Constant Field Values
-
DEFAULT_EXCLUDED_CLASSES
private static final Set<String> DEFAULT_EXCLUDED_CLASSES
Class names to ignore.
-
DEFAULT_EXCLUDED_PACKAGES
private static final Set<String> DEFAULT_EXCLUDED_PACKAGES
Package names to ignore.
-
BRACKET_PATTERN
private static final Pattern BRACKET_PATTERN
Pattern to match brackets in a full type name.
-
excludeClassesRegexps
private final List<Pattern> excludeClassesRegexps
Specify user-configured regular expressions to ignore classes.
-
importedClassPackages
private final Map<String,String> importedClassPackages
A map of (imported class name -> class name with package) pairs.
-
classesContexts
private final Deque<AbstractClassCouplingCheck.ClassContext> classesContexts
Stack of class contexts.
-
excludedClasses
private Set<String> excludedClasses
Specify user-configured class names to ignore.
-
excludedPackages
private Set<String> excludedPackages
Specify user-configured packages to ignore.
-
max
private int max
Specify the maximum threshold allowed.
-
packageName
private String packageName
Current file package.
-
-
Constructor Detail
-
AbstractClassCouplingCheck
protected AbstractClassCouplingCheck(int defaultMax)
Creates new instance of the check.- Parameters:
defaultMax
- default value for allowed complexity.
-
-
Method Detail
-
getLogMessageId
protected abstract String getLogMessageId()
Returns message key we use for log violations.- Returns:
- message key we use for log violations.
-
getDefaultTokens
public final 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
-
setMax
public final void setMax(int max)
Setter to specify the maximum threshold allowed.- Parameters:
max
- allowed complexity.
-
setExcludedClasses
public final void setExcludedClasses(String... excludedClasses)
Setter to specify user-configured class names to ignore.- Parameters:
excludedClasses
- classes to ignore.
-
setExcludeClassesRegexps
public void setExcludeClassesRegexps(Pattern... from)
Setter to specify user-configured regular expressions to ignore classes.- Parameters:
from
- array representing regular expressions of classes to ignore.
-
setExcludedPackages
public final void setExcludedPackages(String... excludedPackages)
Setter to specify user-configured packages to ignore.- Parameters:
excludedPackages
- packages to ignore.- Throws:
IllegalArgumentException
- if there are invalid identifiers among the packages.
-
beginTree
public final void beginTree(DetailAST ast)
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:
ast
- 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
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
visitPackageDef
private void visitPackageDef(DetailAST pkg)
Stores package of current class we check.- Parameters:
pkg
- package definition.
-
visitClassDef
private void visitClassDef(DetailAST classDef)
Creates new context for a given class.- Parameters:
classDef
- class definition node.
-
leaveClassDef
private void leaveClassDef()
Restores previous context.
-
registerImport
private void registerImport(DetailAST imp)
Registers given import. This allows us to track imported classes.- Parameters:
imp
- import definition.
-
createNewClassContext
private void createNewClassContext(String className, DetailAST ast)
Creates new inner class context with given name and location.- Parameters:
className
- The class name.ast
- The class ast.
-
checkCurrentClassAndRestorePrevious
private void checkCurrentClassAndRestorePrevious()
Restores previous context.
-
visitType
private void visitType(DetailAST ast)
Visits type token for the current class context.- Parameters:
ast
- TYPE token.
-
visitLiteralNew
private void visitLiteralNew(DetailAST ast)
Visits NEW token for the current class context.- Parameters:
ast
- NEW token.
-
visitLiteralThrows
private void visitLiteralThrows(DetailAST ast)
Visits THROWS token for the current class context.- Parameters:
ast
- THROWS token.
-
visitAnnotationType
private void visitAnnotationType(DetailAST annotationAST)
Visit ANNOTATION literal and get its type to referenced classes of context.- Parameters:
annotationAST
- Annotation ast.
-
-