use of com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption in project checkstyle by checkstyle.
the class JavadocMethodCheck method shouldCheck.
/**
* Whether we should check this node.
*
* @param ast a given node.
* @return whether we should check a given node.
*/
private boolean shouldCheck(final DetailAST ast) {
final AccessModifierOption surroundingAccessModifier = CheckUtil.getSurroundingAccessModifier(ast);
final AccessModifierOption accessModifier = CheckUtil.getAccessModifierFromModifiersToken(ast);
return surroundingAccessModifier != null && Arrays.stream(accessModifiers).anyMatch(modifier -> modifier == surroundingAccessModifier) && Arrays.stream(accessModifiers).anyMatch(modifier -> modifier == accessModifier);
}
use of com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption in project checkstyle by checkstyle.
the class CheckUtil method getAccessModifierFromModifiersToken.
/**
* Returns the access modifier of the method/constructor at the specified AST. If
* the method is in an interface or annotation block, the access modifier is assumed
* to be public.
*
* @param ast the token of the method/constructor.
* @return the access modifier of the method/constructor.
*/
public static AccessModifierOption getAccessModifierFromModifiersToken(DetailAST ast) {
final DetailAST modsToken = ast.findFirstToken(TokenTypes.MODIFIERS);
AccessModifierOption accessModifier = getAccessModifierFromModifiersTokenDirectly(modsToken);
if (accessModifier == AccessModifierOption.PACKAGE) {
if (ScopeUtil.isInEnumBlock(ast) && ast.getType() == TokenTypes.CTOR_DEF) {
accessModifier = AccessModifierOption.PRIVATE;
} else if (ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) {
accessModifier = AccessModifierOption.PUBLIC;
}
}
return accessModifier;
}
use of com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption in project checkstyle by checkstyle.
the class RecordComponentNumberCheck method visitToken.
@Override
public void visitToken(DetailAST ast) {
final AccessModifierOption accessModifier = CheckUtil.getAccessModifierFromModifiersToken(ast);
if (matchAccessModifiers(accessModifier)) {
final DetailAST recordComponents = ast.findFirstToken(TokenTypes.RECORD_COMPONENTS);
final int componentCount = countComponents(recordComponents);
if (componentCount > max) {
log(ast, MSG_KEY, componentCount, max);
}
}
}
use of com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption in project checkstyle by checkstyle.
the class CheckUtilTest method testGetAccessModifierFromModifiersToken.
@Test
public void testGetAccessModifierFromModifiersToken() throws Exception {
final DetailAST interfaceDef = getNodeFromFile(TokenTypes.INTERFACE_DEF);
final AccessModifierOption modifierInterface = CheckUtil.getAccessModifierFromModifiersToken(interfaceDef.findFirstToken(TokenTypes.OBJBLOCK).findFirstToken(TokenTypes.METHOD_DEF));
assertWithMessage("Invalid access modifier").that(modifierInterface).isEqualTo(AccessModifierOption.PUBLIC);
final DetailAST privateVariable = getNodeFromFile(TokenTypes.VARIABLE_DEF);
final AccessModifierOption modifierPrivate = CheckUtil.getAccessModifierFromModifiersToken(privateVariable);
assertWithMessage("Invalid access modifier").that(modifierPrivate).isEqualTo(AccessModifierOption.PRIVATE);
final DetailAST protectedVariable = privateVariable.getNextSibling();
final AccessModifierOption modifierProtected = CheckUtil.getAccessModifierFromModifiersToken(protectedVariable);
assertWithMessage("Invalid access modifier").that(modifierProtected).isEqualTo(AccessModifierOption.PROTECTED);
final DetailAST publicVariable = protectedVariable.getNextSibling();
final AccessModifierOption modifierPublic = CheckUtil.getAccessModifierFromModifiersToken(publicVariable);
assertWithMessage("Invalid access modifier").that(modifierPublic).isEqualTo(AccessModifierOption.PUBLIC);
final DetailAST packageVariable = publicVariable.getNextSibling();
final AccessModifierOption modifierPackage = CheckUtil.getAccessModifierFromModifiersToken(packageVariable);
assertWithMessage("Invalid access modifier").that(modifierPackage).isEqualTo(AccessModifierOption.PACKAGE);
}
Aggregations