Search in sources :

Example 76 with DetailAST

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

Example 77 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 78 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 79 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 80 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)

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