Skip to content

Commit

Permalink
Issue sevntu-checkstyle#552: no exclusions in ForbidCertainImports ga…
Browse files Browse the repository at this point in the history
…ve no violations
  • Loading branch information
rnveach authored and kariem committed Jul 26, 2018
1 parent d1f9a3b commit f1cb2de
Showing 1 changed file with 26 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ public void setForbiddenImportsRegexp(String forbiddenImportsRegexp) {
}
}

/**
* Gets the regexp for excluding imports from checking.
* @return regexp for excluding imports from checking.
*/
public String getForbiddenImportsExcludesRegexp() {
return forbiddenImportsExcludesRegexp.toString();
}

/**
* Sets the regexp for excluding imports from checking.
* @param forbiddenImportsExcludesRegexp
Expand All @@ -140,16 +132,16 @@ public void setForbiddenImportsExcludesRegexp(String

@Override
public int[] getDefaultTokens() {
final int[] defaultTokens;
if (packageNamesRegexp == null || forbiddenImportsRegexp == null
|| forbiddenImportsExcludesRegexp == null) {
defaultTokens = new int[] {};
}
else {
defaultTokens = new int[] {TokenTypes.PACKAGE_DEF,
TokenTypes.IMPORT, TokenTypes.LITERAL_NEW, };
}
return defaultTokens;
return new int[] {
TokenTypes.PACKAGE_DEF,
TokenTypes.IMPORT,
TokenTypes.LITERAL_NEW,
};
}

@Override
public void beginTree(DetailAST rootAST) {
packageMatches = false;
}

@Override
Expand All @@ -163,22 +155,16 @@ public void visitToken(DetailAST ast) {
}
break;
case TokenTypes.IMPORT:
if (packageMatches && forbiddenImportsRegexp != null
&& forbiddenImportsExcludesRegexp != null) {
final String importQualifiedText = getText(ast);
if (isImportForbidden(importQualifiedText)) {
log(ast, importQualifiedText);
}
final String importQualifiedText = getText(ast);
if (isImportForbidden(importQualifiedText)) {
log(ast, importQualifiedText);
}
break;
case TokenTypes.LITERAL_NEW:
if (forbiddenImportsRegexp != null
&& forbiddenImportsExcludesRegexp != null
&& packageMatches
&& ast.findFirstToken(TokenTypes.DOT) != null) {
final String importQualifiedText = getText(ast);
if (isImportForbidden(importQualifiedText)) {
log(ast, importQualifiedText);
if (ast.findFirstToken(TokenTypes.DOT) != null) {
final String classQualifiedText = getText(ast);
if (isImportForbidden(classQualifiedText)) {
log(ast, classQualifiedText);
}
}
break;
Expand All @@ -195,8 +181,11 @@ public void visitToken(DetailAST ast) {
* classes package, false otherwise
*/
private boolean isImportForbidden(String importText) {
return forbiddenImportsRegexp.matcher(importText).matches()
&& !forbiddenImportsExcludesRegexp.matcher(importText).matches();
return packageMatches
&& forbiddenImportsRegexp != null
&& forbiddenImportsRegexp.matcher(importText).matches()
&& (forbiddenImportsExcludesRegexp == null
|| !forbiddenImportsExcludesRegexp.matcher(importText).matches());
}

/**
Expand Down Expand Up @@ -226,12 +215,10 @@ private static String getText(DetailAST packageDefOrImportNode) {

if (identNode == null) {
final DetailAST parentDotAST = packageDefOrImportNode.findFirstToken(TokenTypes.DOT);
if (parentDotAST != null) {
final FullIdent dottedPathIdent = FullIdent
.createFullIdentBelow(parentDotAST);
final DetailAST nameAST = parentDotAST.getLastChild();
result = dottedPathIdent.getText() + "." + nameAST.getText();
}
final FullIdent dottedPathIdent = FullIdent
.createFullIdentBelow(parentDotAST);
final DetailAST nameAST = parentDotAST.getLastChild();
result = dottedPathIdent.getText() + "." + nameAST.getText();
}
else {
result = identNode.getText();
Expand Down

0 comments on commit f1cb2de

Please sign in to comment.