55
66import com .puppycrawl .tools .checkstyle .api .AbstractCheck ;
77import com .puppycrawl .tools .checkstyle .api .DetailAST ;
8+ import com .puppycrawl .tools .checkstyle .api .FullIdent ;
89import com .puppycrawl .tools .checkstyle .api .TokenTypes ;
910import com .puppycrawl .tools .checkstyle .checks .naming .AccessModifierOption ;
1011import com .puppycrawl .tools .checkstyle .utils .CheckUtil ;
2122public 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 /**
0 commit comments