diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java index 2474c3ef0..0c98e0dd7 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java @@ -22,6 +22,7 @@ package com.puppycrawl.tools.checkstyle.checks.imports; import java.net.URI; import java.util.Collections; import java.util.Set; +import java.util.regex.Pattern; import org.apache.commons.beanutils.ConversionException; @@ -73,6 +74,11 @@ public class ImportControlCheck extends AbstractCheck implements ExternalResourc /** Location of import control file. */ private String fileLocation; + /** The filepath pattern this check applies to. */ + private Pattern path = Pattern.compile(".*"); + /** Whether to process the current file. */ + private boolean processCurrentFile; + /** The root package controller. */ private ImportControl root; /** The package doing the import. */ @@ -103,27 +109,31 @@ public class ImportControlCheck extends AbstractCheck implements ExternalResourc @Override public void beginTree(final DetailAST rootAST) { currentImportControl = null; + processCurrentFile = path.matcher(getFileContents().getFileName()).find(); } @Override public void visitToken(final DetailAST ast) { - if (ast.getType() == TokenTypes.PACKAGE_DEF) { - if (root == null) { - log(ast, MSG_MISSING_FILE); - } - else { - packageName = getPackageText(ast); - currentImportControl = root.locateFinest(packageName); - if (currentImportControl == null) { - log(ast, MSG_UNKNOWN_PKG); + if (processCurrentFile) { + if (ast.getType() == TokenTypes.PACKAGE_DEF) { + if (root == null) { + log(ast, MSG_MISSING_FILE); + } + else { + packageName = getPackageText(ast); + currentImportControl = root.locateFinest(packageName); + if (currentImportControl == null) { + log(ast, MSG_UNKNOWN_PKG); + } } } - } - else if (currentImportControl != null) { - final String importText = getImportText(ast); - final AccessResult access = currentImportControl.checkAccess(packageName, importText); - if (access != AccessResult.ALLOWED) { - log(ast, MSG_DISALLOWED, importText); + else if (currentImportControl != null) { + final String importText = getImportText(ast); + final AccessResult access = + currentImportControl.checkAccess(packageName, importText); + if (access != AccessResult.ALLOWED) { + log(ast, MSG_DISALLOWED, importText); + } } } } @@ -181,6 +191,14 @@ public class ImportControlCheck extends AbstractCheck implements ExternalResourc } } + /** + * Set the file path pattern that this check applies to. + * @param pattern the file path regex this check should apply to. + */ + public void setPath(Pattern pattern) { + path = pattern; + } + /** * Set the parameter for the url containing the import control * configuration. It will cause the url to be loaded. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java index 6ca0cded2..6c8ded44d 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java @@ -356,6 +356,46 @@ public class ImportControlCheckTest extends BaseCheckTestSupport { verify(checker, pathToEmptyFile, pathToEmptyFile, expected); } + @Test + public void testPathRegexMatches() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", getResourcePath("import-control_one.xml")); + checkConfig.addAttribute("path", "^.*[\\\\/]src[\\\\/]test[\\\\/].*$"); + final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")}; + + verify(checkConfig, getPath("InputImportControl.java"), expected); + } + + @Test + public void testPathRegexMatchesPartially() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", getResourcePath("import-control_one.xml")); + checkConfig.addAttribute("path", "[\\\\/]InputImportControl\\.java"); + final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")}; + + verify(checkConfig, getPath("InputImportControl.java"), expected); + } + + @Test + public void testPathRegexDoesntMatch() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", getResourcePath("import-control_one.xml")); + checkConfig.addAttribute("path", "^.*[\\\\/]src[\\\\/]main[\\\\/].*$"); + final String[] expected = CommonUtils.EMPTY_STRING_ARRAY; + + verify(checkConfig, getPath("InputImportControl.java"), expected); + } + + @Test + public void testPathRegexDoesntMatchPartially() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", getResourcePath("import-control_one.xml")); + checkConfig.addAttribute("path", "[\\\\/]NoMatch\\.java"); + final String[] expected = CommonUtils.EMPTY_STRING_ARRAY; + + verify(checkConfig, getPath("InputImportControl.java"), expected); + } + private Checker createMockCheckerWithCache(DefaultConfiguration checkConfig) throws IOException, CheckstyleException { final DefaultConfiguration treeWalkerConfig = createCheckConfig(TreeWalker.class); diff --git a/src/xdocs/config_imports.xml b/src/xdocs/config_imports.xml index 4316ca181..779a99020 100644 --- a/src/xdocs/config_imports.xml +++ b/src/xdocs/config_imports.xml @@ -723,6 +723,16 @@ import android.*; URI null + + path + + Regular expression of file paths to which this check should apply. Files that + don't match the pattern will not be checked. The pattern will be matched against + the full absolute file path. + + Regular Expression + ".*" + @@ -738,6 +748,19 @@ import android.*; </module> +

+ To configure the check to only check the "src/main" directory + using an import control file called "config/import-control.xml", + then have the following: +

+ + +<module name="ImportControl"> + <property name="file" value="config/import-control.xml"/> + <property name="path" value="^.*[\\/]src[\\/]main[\\/].*$"/> +</module> + +

In the example below access to package com.puppycrawl.tools.checkstyle.checks and its subpackages is