Search in sources :

Example 21 with DetailAST

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);
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 22 with DetailAST

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;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 23 with DetailAST

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();
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 24 with DetailAST

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;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) ArrayList(java.util.ArrayList)

Example 25 with DetailAST

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());
        }
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Aggregations

DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)397 Test (org.junit.Test)74 CommonHiddenStreamToken (antlr.CommonHiddenStreamToken)14 FullIdent (com.puppycrawl.tools.checkstyle.api.FullIdent)14 ArrayList (java.util.ArrayList)13 AST (antlr.collections.AST)8 Method (java.lang.reflect.Method)7 LinkedList (java.util.LinkedList)7 Scope (com.puppycrawl.tools.checkstyle.api.Scope)6 HashSet (java.util.HashSet)6 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)5 ArrayDeque (java.util.ArrayDeque)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)3 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 HashMap (java.util.HashMap)3