class ImportControl
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private java.util.List<ImportControl> |
children
List of children
ImportControl objects. |
private static java.lang.String |
DOT
The package separator: "."
|
private static java.util.regex.Pattern |
DOT_PATTERN
A pattern matching the package separator: "."
|
private static java.lang.String |
DOT_REGEX
The regex for the package separator: "\\.".
|
private java.lang.String |
fullPackage
The full package name for the node.
|
private ImportControl |
parent
The parent.
|
private java.util.regex.Pattern |
patternForExactMatch
The regex pattern for exact matches - only not null if regex is true.
|
private java.util.regex.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.
|
private java.util.Deque<AbstractImportRule> |
rules
List of
AbstractImportRule objects to check. |
private MismatchStrategy |
strategyOnMismatch
Strategy in a case if matching allow/disallow rule was not found.
|
Constructor and Description |
---|
ImportControl(ImportControl parent,
java.lang.String subPkg,
boolean regex)
Construct a child node.
|
ImportControl(ImportControl parent,
java.lang.String subPkg,
boolean regex,
MismatchStrategy strategyOnMismatch)
Construct a child node.
|
ImportControl(java.lang.String pkgName,
boolean regex)
Construct a root node.
|
ImportControl(java.lang.String pkgName,
boolean regex,
MismatchStrategy strategyOnMismatch)
Construct a root node.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addImportRule(AbstractImportRule rule)
Adds an
AbstractImportRule to the node. |
AccessResult |
checkAccess(java.lang.String inPkg,
java.lang.String forImport)
Returns whether a package or class is allowed to be imported.
|
private static java.util.regex.Pattern |
createPatternForExactMatch(java.lang.String expression)
Creates a Pattern from
expression . |
private static java.util.regex.Pattern |
createPatternForPartialMatch(java.lang.String expression)
Creates a Pattern from
expression that matches exactly and child packages. |
private static java.lang.String |
encloseInGroup(java.lang.String expression)
Enclose
expression in a (non-capturing) group. |
private static java.lang.String |
ensureSelfContainedRegex(java.lang.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 enclosing
input in
a (non-capturing) group if input already is a regular expression. |
private AccessResult |
localCheckAccess(java.lang.String inPkg,
java.lang.String forImport)
Checks whether any of the rules for this node control access to
a specified package or class.
|
ImportControl |
locateFinest(java.lang.String forPkg)
Search down the tree to locate the finest match for a supplied package.
|
private boolean |
matchesAtFront(java.lang.String pkg)
Matches other package name exactly or partially at front.
|
private boolean |
matchesAtFrontNoRegex(java.lang.String pkg)
Non-regex case.
|
private boolean |
matchesExactly(java.lang.String pkg)
Check for equality of this with pkg.
|
private static java.lang.String |
toRegex(java.lang.String input)
Converts a normal package name into a regex pattern by escaping all
special characters that may occur in a java package name.
|
private static final java.lang.String DOT
private static final java.util.regex.Pattern DOT_PATTERN
private static final java.lang.String DOT_REGEX
private final java.util.Deque<AbstractImportRule> rules
AbstractImportRule
objects to check.private final java.util.List<ImportControl> children
ImportControl
objects.private final ImportControl parent
private final java.lang.String fullPackage
private final java.util.regex.Pattern patternForPartialMatch
private final java.util.regex.Pattern patternForExactMatch
private final boolean regex
private final MismatchStrategy strategyOnMismatch
ImportControl(java.lang.String pkgName, boolean regex, MismatchStrategy strategyOnMismatch)
pkgName
- the name of the package.regex
- flags interpretation of pkgName as regex pattern.strategyOnMismatch
- strategy in a case if matching allow/disallow rule was not found.ImportControl(java.lang.String pkgName, boolean regex)
pkgName
- the name of the package.regex
- flags interpretation of pkgName as regex pattern.ImportControl(ImportControl parent, java.lang.String subPkg, boolean regex, MismatchStrategy strategyOnMismatch)
ensureSelfContainedRegex(String, boolean)
for more details.parent
- the parent node.subPkg
- the sub package name.regex
- flags interpretation of subPkg as regex pattern.strategyOnMismatch
- strategy in a case if matching allow/disallow rule was not found.ImportControl(ImportControl parent, java.lang.String subPkg, boolean regex)
ensureSelfContainedRegex(String, boolean)
for more details.parent
- the parent node.subPkg
- the sub package name.regex
- flags interpretation of subPkg as regex pattern.private static java.lang.String ensureSelfContainedRegex(java.lang.String input, boolean alreadyRegex)
input
in
a (non-capturing) group if input
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.
input
- the input string.alreadyRegex
- signals if input already is a regular expression.private static java.lang.String encloseInGroup(java.lang.String expression)
expression
in a (non-capturing) group.expression
- the input regular expressionprivate static java.lang.String toRegex(java.lang.String input)
input
- the input string.private static java.util.regex.Pattern createPatternForPartialMatch(java.lang.String expression)
expression
that matches exactly and child packages.expression
- a self-contained regular expression matching the full package exactly.private static java.util.regex.Pattern createPatternForExactMatch(java.lang.String expression)
expression
.expression
- a self-contained regular expression matching the full package exactly.protected void addImportRule(AbstractImportRule rule)
AbstractImportRule
to the node.rule
- the rule to be added.public ImportControl locateFinest(java.lang.String forPkg)
forPkg
- the package to search for.private boolean matchesAtFront(java.lang.String pkg)
pkg
- the package to compare with.private boolean matchesAtFrontNoRegex(java.lang.String pkg)
pkg
- the package to compare with.public AccessResult checkAccess(java.lang.String inPkg, java.lang.String forImport)
AccessResult.UNKNOWN
is returned.forImport
- the import to check on.inPkg
- the package doing the import.AccessResult
.private AccessResult localCheckAccess(java.lang.String inPkg, java.lang.String forImport)
forImport
- the import to check.inPkg
- the package doing the import.AccessResult
.private boolean matchesExactly(java.lang.String pkg)
pkg
- the package to compare with.