AvoidModifiersForTypesCheck extends AbstractCheck { @Override public void visitToken(DetailAST ast) { final String classNameAndPath = getClassNameAndPath(ast); if (classNameAndPath != null) { final String className = getClassName(classNameAndPath); final Set<Integer> modifiersSet = getModifiers(ast); if (ast.getParent().getType() == TokenTypes.OBJBLOCK && !modifiersSet.contains(TokenTypes.LITERAL_PUBLIC) && !modifiersSet.contains(TokenTypes.LITERAL_PROTECTED) && !modifiersSet.contains(TokenTypes.LITERAL_PRIVATE) && forbiddenClassesRegexpPackagePrivate.matcher(className).matches()) { log(ast, MSG_KEY, className, "package-private"); } for (int modifierType : modifiersSet) { if (match(modifierType, className)) { String tokenName = TokenUtil.getTokenName(modifierType); tokenName = tokenName.toLowerCase(Locale.ENGLISH).replaceAll("literal_", ""); log(ast, MSG_KEY, className, tokenName); } } } }  void setForbiddenClassesRegexpAnnotation(String forbiddenClassesRegexpAnnotation); void setForbiddenClassesRegexpFinal(String forbiddenClassesRegexpFinal); void setForbiddenClassesRegexpStatic(String forbiddenClassesRegexpStatic); void setForbiddenClassesRegexpTransient(String forbiddenClassesRegexpTransient); void setForbiddenClassesRegexpVolatile(String forbiddenClassesRegexpVolatile); void setForbiddenClassesRegexpPrivate(String forbiddenClassesRegexpPrivate); void setForbiddenClassesRegexpPackagePrivate(
            String forbiddenClassesRegexpPackagePrivate); void setForbiddenClassesRegexpProtected(String forbiddenClassesRegexpProtected); void setForbiddenClassesRegexpPublic(String forbiddenClassesRegexpPublic); @Override int[] getDefaultTokens(); @Override int[] getAcceptableTokens(); @Override int[] getRequiredTokens(); @Override void visitToken(DetailAST ast); static final String MSG_KEY; }
@Test public void testUnsupportedModifier() { final DetailAstImpl sync = new DetailAstImpl(); sync.setType(TokenTypes.LITERAL_SYNCHRONIZED); final DetailAstImpl ident = new DetailAstImpl(); ident.setType(TokenTypes.IDENT); ident.setText("dummy"); final DetailAstImpl type = new DetailAstImpl(); type.setType(TokenTypes.TYPE); type.addChild(ident); final DetailAstImpl child = new DetailAstImpl(); child.setType(TokenTypes.MODIFIERS); child.addChild(sync); final DetailAstImpl node = new DetailAstImpl(); node.addChild(type); node.addChild(child); final DetailAstImpl parent = new DetailAstImpl(); parent.setType(TokenTypes.OBJBLOCK); parent.addChild(node); final AvoidModifiersForTypesCheck check = new AvoidModifiersForTypesCheck(); try { check.visitToken(node); fail("exception expected"); } catch (IllegalArgumentException ex) { Assert.assertEquals("Found unsupported token: LITERAL_SYNCHRONIZED", ex.getMessage()); } }