use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class RedundantModifierCheck method isClassPublic.
/**
* Checks if given class is accessible from "public" scope.
* @param ast class def to check
* @return true if class is accessible from public scope,false otherwise
*/
private static boolean isClassPublic(DetailAST ast) {
boolean isAccessibleFromPublic = false;
final boolean isMostOuterScope = ast.getParent() == null;
final DetailAST modifiersAst = ast.findFirstToken(TokenTypes.MODIFIERS);
final boolean hasPublicModifier = modifiersAst.branchContains(TokenTypes.LITERAL_PUBLIC);
if (isMostOuterScope) {
isAccessibleFromPublic = hasPublicModifier;
} else {
final DetailAST parentClassAst = ast.getParent().getParent();
if (hasPublicModifier || parentClassAst.getType() == TokenTypes.INTERFACE_DEF) {
isAccessibleFromPublic = isClassPublic(parentClassAst);
}
}
return isAccessibleFromPublic;
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class RedundantModifierCheck method processMethods.
/**
* Process validation of Methods.
* @param ast method AST
*/
private void processMethods(DetailAST ast) {
final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
// private method?
boolean checkFinal = modifiers.branchContains(TokenTypes.LITERAL_PRIVATE);
// declared in a final class?
DetailAST parent = ast.getParent();
while (parent != null) {
if (parent.getType() == TokenTypes.CLASS_DEF) {
final DetailAST classModifiers = parent.findFirstToken(TokenTypes.MODIFIERS);
checkFinal = checkFinal || classModifiers.branchContains(TokenTypes.FINAL);
parent = null;
} else if (parent.getType() == TokenTypes.LITERAL_NEW || parent.getType() == TokenTypes.ENUM_CONSTANT_DEF) {
checkFinal = true;
parent = null;
} else {
parent = parent.getParent();
}
}
if (checkFinal && !isAnnotatedWithSafeVarargs(ast)) {
checkForRedundantModifier(ast, TokenTypes.FINAL);
}
if (!ast.branchContains(TokenTypes.SLIST)) {
processAbstractMethodParameters(ast);
}
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class RedundantModifierCheck method isAnnotatedWithSafeVarargs.
/**
* Checks if method definition is annotated with
* <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/SafeVarargs.html">
* SafeVarargs</a> annotation
* @param methodDef method definition node
* @return true or false
*/
private static boolean isAnnotatedWithSafeVarargs(DetailAST methodDef) {
boolean result = false;
final List<DetailAST> methodAnnotationsList = getMethodAnnotationsList(methodDef);
for (DetailAST annotationNode : methodAnnotationsList) {
if ("SafeVarargs".equals(annotationNode.getLastChild().getText())) {
result = true;
break;
}
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class RedundantModifierCheck method processInterfaceOrAnnotation.
/**
* Do validation of interface of annotation.
* @param ast token AST
*/
private void processInterfaceOrAnnotation(DetailAST ast) {
final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
DetailAST modifier = modifiers.getFirstChild();
while (modifier != null) {
// javac does not allow final or static in interface methods
// order annotation fields hence no need to check that this
// is not a method or annotation field
final int type = modifier.getType();
if (type == TokenTypes.LITERAL_PUBLIC || type == TokenTypes.LITERAL_STATIC && ast.getType() != TokenTypes.METHOD_DEF || type == TokenTypes.ABSTRACT && ast.getType() != TokenTypes.CLASS_DEF || type == TokenTypes.FINAL && ast.getType() != TokenTypes.CLASS_DEF) {
log(modifier.getLineNo(), modifier.getColumnNo(), MSG_KEY, modifier.getText());
break;
}
modifier = modifier.getNextSibling();
}
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class RedundantModifierCheck method getMethodAnnotationsList.
/**
* Gets the list of annotations on method definition.
* @param methodDef method definition node
* @return List of annotations
*/
private static List<DetailAST> getMethodAnnotationsList(DetailAST methodDef) {
final List<DetailAST> annotationsList = new ArrayList<>();
final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS);
DetailAST modifier = modifiers.getFirstChild();
while (modifier != null) {
if (modifier.getType() == TokenTypes.ANNOTATION) {
annotationsList.add(modifier);
}
modifier = modifier.getNextSibling();
}
return annotationsList;
}
Aggregations