Skip to content

Commit 7714c80

Browse files
authored
Ignore deny listed words in implementation packages (Azure#43369)
1 parent f40dfb3 commit 7714c80

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

eng/code-quality-reports/src/main/java/com/azure/tools/checkstyle/checks/DenyListedWordsCheck.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
77
import com.puppycrawl.tools.checkstyle.api.DetailAST;
8+
import com.puppycrawl.tools.checkstyle.api.FullIdent;
89
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
910
import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption;
1011
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
@@ -21,6 +22,8 @@
2122
public class DenyListedWordsCheck extends AbstractCheck {
2223
private String[] denyListedWords = new String[0];
2324

25+
private boolean implementationPackage = false;
26+
2427
static final String ERROR_MESSAGE = "%s, All Public API Classes, Fields and Methods should follow "
2528
+ "Camelcase standards for the following words: %s.";
2629

@@ -47,14 +50,35 @@ public int[] getAcceptableTokens() {
4750

4851
@Override
4952
public int[] getRequiredTokens() {
50-
return new int[]{TokenTypes.CLASS_DEF,
51-
TokenTypes.METHOD_DEF,
52-
TokenTypes.VARIABLE_DEF};
53+
return new int[] {
54+
TokenTypes.PACKAGE_DEF, TokenTypes.CLASS_DEF, TokenTypes.METHOD_DEF, TokenTypes.VARIABLE_DEF };
55+
}
56+
57+
@Override
58+
public void beginTree(DetailAST rootAST) {
59+
implementationPackage = false;
60+
}
61+
62+
@Override
63+
public void finishTree(DetailAST rootAST) {
64+
implementationPackage = false;
5365
}
5466

5567
@Override
5668
public void visitToken(DetailAST token) {
69+
// If we're in an implementation package ignore this check.
70+
// If a PACKAGE_DEF is not found, then assume it's not an implementation package.
71+
if (implementationPackage) {
72+
return;
73+
}
74+
5775
switch (token.getType()) {
76+
case TokenTypes.PACKAGE_DEF:
77+
// Check if we're in an implementation package.
78+
final String packageName = FullIdent.createFullIdent(token.findFirstToken(TokenTypes.DOT)).getText();
79+
implementationPackage = packageName.contains("implementation");
80+
break;
81+
5882
case TokenTypes.CLASS_DEF:
5983
case TokenTypes.METHOD_DEF:
6084
case TokenTypes.VARIABLE_DEF:
@@ -67,7 +91,7 @@ public void visitToken(DetailAST token) {
6791
break;
6892
}
6993

70-
// In an interface all the fields (variables) are by default public, static and final.
94+
// In an interface all the fields (variables) are by default public, static, and final.
7195
if (token.getType() == TokenTypes.VARIABLE_DEF && ScopeUtil.isInInterfaceBlock(token)) {
7296
break;
7397
}
@@ -91,7 +115,8 @@ private boolean isPublicApi(DetailAST token) {
91115
final DetailAST modifiersAST = token.findFirstToken(TokenTypes.MODIFIERS);
92116
final AccessModifierOption accessModifier = CheckUtil.getAccessModifierFromModifiersToken(token);
93117
final boolean isStatic = modifiersAST.findFirstToken(TokenTypes.LITERAL_STATIC) != null;
94-
return (accessModifier.equals(AccessModifierOption.PUBLIC) || accessModifier.equals(AccessModifierOption.PROTECTED)) && !isStatic;
118+
return (accessModifier.equals(AccessModifierOption.PUBLIC) || accessModifier.equals(
119+
AccessModifierOption.PROTECTED)) && !isStatic;
95120
}
96121

97122
/**

eng/code-quality-reports/src/test/java/com/azure/tools/checkstyle/checks/DenyListedWordsCheckTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import org.junit.jupiter.api.BeforeEach;
1212
import org.junit.jupiter.api.Test;
1313

14+
import java.io.File;
15+
import java.util.Arrays;
16+
1417
public class DenyListedWordsCheckTest extends AbstractModuleTestSupport {
1518
private Checker checker;
1619

@@ -44,6 +47,23 @@ public void denyListedWordsInterface() throws Exception {
4447
verify(checker, getPath("DenyListedWordsInterface.java"));
4548
}
4649

50+
@Test
51+
public void implementationPackageIgnored() throws Exception {
52+
File file = TestUtils.createCheckFile("implementationPackageIgnored", Arrays.asList(
53+
"package com.test.implementation;",
54+
"@JacksonXmlRootElement(localName = \"File-SetHTTPHeaders-Headers\")",
55+
"public class CamelCaseTestData {",
56+
" public void errorHTTPMethod() { throw new RuntimeException(\"Error Messages.\"); }",
57+
" public void validHttpMethod() { throw new RuntimeException(\"Error Messages.\"); }",
58+
" public static void itIsAURLError() { throw new RuntimeException(\"Error Messages.\"); }",
59+
" protected void invalidXMLMethod() { throw new RuntimeException(\"Error Messages.\"); }",
60+
" private void shouldNotSearch() { throw new RuntimeException(\"Error Messages.\"); }",
61+
"}"
62+
));
63+
64+
verify(checker, new File[]{file}, file.getAbsolutePath());
65+
}
66+
4767
private String expectedErrorMessage(int line, int column, String errorMessage) {
4868
return String.format("%d:%d: %s", line, column, errorMessage);
4969
}

0 commit comments

Comments
 (0)