Class PkgImportControl
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.checks.imports.AbstractImportControl
-
- com.puppycrawl.tools.checkstyle.checks.imports.PkgImportControl
-
class PkgImportControl extends AbstractImportControl
Represents a tree of import rules for a specific package. Each instance may have zero or more children. A child may be a sub-package, a class, or an allow/disallow rule.
-
-
Field Summary
Fields Modifier and Type Field Description private List<AbstractImportControl>
children
List of childrenAbstractImportControl
objects.private static String
DOT
The package separator: ".".private static String
DOT_ESCAPED_REGEX
The regex for the escaped package separator: "\\\\.".private static String
DOT_REGEX
The regex for the package separator: "\\.".private static Pattern
DOT_REGEX_PATTERN
A pattern matching the package separator: "\.".private String
fullPackageName
The full name for the package.private Pattern
patternForExactMatch
The regex pattern for exact matches - only not null if regex is true.private Pattern
patternForPartialMatch
The regex pattern for partial match (exact and for subpackages) - only not null if regex is true.private boolean
regex
If this package represents a regular expression.
-
Constructor Summary
Constructors Constructor Description PkgImportControl(PkgImportControl parent, String subPackageName, boolean regex, MismatchStrategy strategyOnMismatch)
Construct a sub-package node.PkgImportControl(String packageName, boolean regex, MismatchStrategy strategyOnMismatch)
Construct a root, package node.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addChild(AbstractImportControl importControl)
Adds new child import control.private static Pattern
createPatternForExactMatch(String expression)
Creates a Pattern fromexpression
.private static Pattern
createPatternForPartialMatch(String expression)
Creates a Pattern fromexpression
that matches exactly and child packages.private static String
encloseInGroup(String expression)
Encloseexpression
in a (non-capturing) group.private static String
ensureSelfContainedRegex(String input, boolean alreadyRegex)
Returns a regex that is suitable for concatenation by 1) either converting a plain string into a regular expression (handling special characters) or 2) by enclosinginput
in a (non-capturing) group ifinput
already is a regular expression.AbstractImportControl
locateFinest(String forPkg, String forFileName)
Search down the tree to locate the finest match for a supplied package.private boolean
matchesAtFront(String pkg)
Matches other package name exactly or partially at front.private boolean
matchesAtFrontNoRegex(String pkg)
Non-regex case.protected boolean
matchesExactly(String pkg, String fileName)
Check for equality of this with pkg.private static String
toRegex(String input)
Converts a normal package name into a regex pattern by escaping all special characters that may occur in a java package name.-
Methods inherited from class com.puppycrawl.tools.checkstyle.checks.imports.AbstractImportControl
addImportRule, checkAccess
-
-
-
-
Field Detail
-
DOT
private static final String DOT
The package separator: ".".- See Also:
- Constant Field Values
-
DOT_REGEX
private static final String DOT_REGEX
The regex for the package separator: "\\.".- See Also:
- Constant Field Values
-
DOT_REGEX_PATTERN
private static final Pattern DOT_REGEX_PATTERN
A pattern matching the package separator: "\.".
-
DOT_ESCAPED_REGEX
private static final String DOT_ESCAPED_REGEX
The regex for the escaped package separator: "\\\\.".- See Also:
- Constant Field Values
-
children
private final List<AbstractImportControl> children
List of childrenAbstractImportControl
objects.
-
fullPackageName
private final String fullPackageName
The full name for the package.
-
patternForPartialMatch
private final Pattern patternForPartialMatch
The regex pattern for partial match (exact and for subpackages) - only not null if regex is true.
-
patternForExactMatch
private final Pattern patternForExactMatch
The regex pattern for exact matches - only not null if regex is true.
-
regex
private final boolean regex
If this package represents a regular expression.
-
-
Constructor Detail
-
PkgImportControl
PkgImportControl(String packageName, boolean regex, MismatchStrategy strategyOnMismatch)
Construct a root, package node.- Parameters:
packageName
- the name of the package.regex
- flags interpretation of name as regex pattern.strategyOnMismatch
- strategy in a case if matching allow/disallow rule was not found.
-
PkgImportControl
PkgImportControl(PkgImportControl parent, String subPackageName, boolean regex, MismatchStrategy strategyOnMismatch)
Construct a sub-package node. The concatenation of regular expressions needs special care: seeensureSelfContainedRegex(String, boolean)
for more details.- Parameters:
parent
- the parent package.subPackageName
- the name of the current sub-package.regex
- flags interpretation of name as regex pattern.strategyOnMismatch
- strategy in a case if matching allow/disallow rule was not found.
-
-
Method Detail
-
ensureSelfContainedRegex
private static String ensureSelfContainedRegex(String input, boolean alreadyRegex)
Returns a regex that is suitable for concatenation by 1) either converting a plain string into a regular expression (handling special characters) or 2) by enclosinginput
in a (non-capturing) group ifinput
already is a regular expression.1) When concatenating a non-regex package component (like "org.google") with a regex component (like "[^.]+") the other component has to be converted into a regex too, see
toRegex(String)
.2) The grouping is strictly necessary if a)
input
is a regular expression that b) contains the alteration character ('|') and if c) the pattern is not already enclosed in a group - as you see in this example:parent="com|org", child="common|uncommon"
will result in the pattern"(?:org|com)\.(?common|uncommon)"
what will match"com.common"
,"com.uncommon"
,"org.common"
, and"org.uncommon"
. Without the grouping it would be"com|org.common|uncommon"
which would match"com"
,"org.common"
, and"uncommon"
, which clearly is undesirable. Adding the group fixes this.For simplicity the grouping is added to regular expressions unconditionally.
- Parameters:
input
- the input string.alreadyRegex
- signals if input already is a regular expression.- Returns:
- a regex string.
-
encloseInGroup
private static String encloseInGroup(String expression)
Encloseexpression
in a (non-capturing) group.- Parameters:
expression
- the input regular expression- Returns:
- a grouped pattern.
-
toRegex
private static String toRegex(String input)
Converts a normal package name into a regex pattern by escaping all special characters that may occur in a java package name.- Parameters:
input
- the input string.- Returns:
- a regex string.
-
createPatternForPartialMatch
private static Pattern createPatternForPartialMatch(String expression)
Creates a Pattern fromexpression
that matches exactly and child packages.- Parameters:
expression
- a self-contained regular expression matching the full package exactly.- Returns:
- a Pattern.
-
createPatternForExactMatch
private static Pattern createPatternForExactMatch(String expression)
Creates a Pattern fromexpression
.- Parameters:
expression
- a self-contained regular expression matching the full package exactly.- Returns:
- a Pattern.
-
locateFinest
public AbstractImportControl locateFinest(String forPkg, String forFileName)
Description copied from class:AbstractImportControl
Search down the tree to locate the finest match for a supplied package.- Specified by:
locateFinest
in classAbstractImportControl
- Parameters:
forPkg
- the package to search for.forFileName
- the file name to search for.- Returns:
- the finest match, or null if no match at all.
-
addChild
public void addChild(AbstractImportControl importControl)
Adds new child import control.- Parameters:
importControl
- child import control
-
matchesAtFront
private boolean matchesAtFront(String pkg)
Matches other package name exactly or partially at front.- Parameters:
pkg
- the package to compare with.- Returns:
- if it matches.
-
matchesAtFrontNoRegex
private boolean matchesAtFrontNoRegex(String pkg)
Non-regex case. Ensure a trailing dot for subpackages, i.e. "com.puppy" will match "com.puppy.crawl" but not "com.puppycrawl.tools".- Parameters:
pkg
- the package to compare with.- Returns:
- if it matches.
-
matchesExactly
protected boolean matchesExactly(String pkg, String fileName)
Description copied from class:AbstractImportControl
Check for equality of this with pkg.- Specified by:
matchesExactly
in classAbstractImportControl
- Parameters:
pkg
- the package to compare with.fileName
- the file name to compare with.- Returns:
- if it matches.
-
-