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;
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class BlockParentHandler method checkIndentation.
@Override
public void checkIndentation() {
checkTopLevelToken();
// separate to allow for eventual configuration
checkLeftParen(getLeftParen());
checkRightParen(getLeftParen(), getRightParen());
if (hasCurlies()) {
checkLeftCurly();
checkRightCurly();
}
final DetailAST listChild = getListChild();
if (listChild == null) {
checkNonListChild();
} else {
// NOTE: switch statements usually don't have curlies
if (!hasCurlies() || !areOnSameLine(getLeftCurly(), getRightCurly())) {
checkChildren(listChild, getCheckedChildren(), getChildrenExpectedIndent(), true, canChildrenBeNested());
}
}
}
Aggregations