Search in sources :

Example 31 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class AbstractSuperCheck method isSuperCallInOverridingMethod.

/**
     * Determines whether a super call in overriding method.
     *
     * @param ast The AST node of a 'dot operator' in 'super' call.
     * @return true if super call in overriding method.
     */
private boolean isSuperCallInOverridingMethod(DetailAST ast) {
    boolean inOverridingMethod = false;
    DetailAST dotAst = ast;
    while (dotAst.getType() != TokenTypes.CTOR_DEF && dotAst.getType() != TokenTypes.INSTANCE_INIT) {
        if (dotAst.getType() == TokenTypes.METHOD_DEF) {
            inOverridingMethod = isOverridingMethod(dotAst);
            break;
        }
        dotAst = dotAst.getParent();
    }
    return inOverridingMethod;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 32 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class CovariantEqualsCheck method visitToken.

@Override
public void visitToken(DetailAST ast) {
    equalsMethods.clear();
    // examine method definitions for equals methods
    final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
    if (objBlock != null) {
        DetailAST child = objBlock.getFirstChild();
        boolean hasEqualsObject = false;
        while (child != null) {
            if (child.getType() == TokenTypes.METHOD_DEF && CheckUtils.isEqualsMethod(child)) {
                if (isFirstParameterObject(child)) {
                    hasEqualsObject = true;
                } else {
                    equalsMethods.add(child);
                }
            }
            child = child.getNextSibling();
        }
        // report equals method definitions
        if (!hasEqualsObject) {
            for (DetailAST equalsAST : equalsMethods) {
                final DetailAST nameNode = equalsAST.findFirstToken(TokenTypes.IDENT);
                log(nameNode.getLineNo(), nameNode.getColumnNo(), MSG_KEY);
            }
        }
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 33 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class CovariantEqualsCheck method isFirstParameterObject.

/**
     * Tests whether a method's first parameter is an Object.
     * @param methodDefAst the method definition AST to test.
     *     Precondition: ast is a TokenTypes.METHOD_DEF node.
     * @return true if ast has first parameter of type Object.
     */
private static boolean isFirstParameterObject(DetailAST methodDefAst) {
    final DetailAST paramsNode = methodDefAst.findFirstToken(TokenTypes.PARAMETERS);
    // parameter type "Object"?
    final DetailAST paramNode = paramsNode.findFirstToken(TokenTypes.PARAMETER_DEF);
    final DetailAST typeNode = paramNode.findFirstToken(TokenTypes.TYPE);
    final FullIdent fullIdent = FullIdent.createFullIdentBelow(typeNode);
    final String name = fullIdent.getText();
    return "Object".equals(name) || "java.lang.Object".equals(name);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FullIdent(com.puppycrawl.tools.checkstyle.api.FullIdent)

Example 34 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class DeclarationOrderCheck method isForwardReference.

/**
     * Checks whether an identifier references a field which has been already defined in class.
     * @param fieldDef a field definition.
     * @return true if an identifier references a field which has been already defined in class.
     */
private boolean isForwardReference(DetailAST fieldDef) {
    final DetailAST exprStartIdent = fieldDef.findFirstToken(TokenTypes.IDENT);
    final Set<DetailAST> exprIdents = getAllTokensOfType(exprStartIdent, TokenTypes.IDENT);
    boolean forwardReference = false;
    for (DetailAST ident : exprIdents) {
        if (classFieldNames.contains(ident.getText())) {
            forwardReference = true;
            break;
        }
    }
    return forwardReference;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 35 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class FallThroughCheck method checkIf.

/**
     * Checks if a given IF terminated by return, throw or,
     * if allowed break, continue.
     * @param ast IF to check
     * @param useBreak should we consider break as terminator.
     * @param useContinue should we consider continue as terminator.
     * @return true if IF is terminated.
     */
private boolean checkIf(final DetailAST ast, boolean useBreak, boolean useContinue) {
    final DetailAST thenStmt = ast.findFirstToken(TokenTypes.RPAREN).getNextSibling();
    final DetailAST elseStmt = thenStmt.getNextSibling();
    boolean isTerminated = isTerminated(thenStmt, useBreak, useContinue);
    if (isTerminated && elseStmt != null) {
        isTerminated = isTerminated(elseStmt.getFirstChild(), useBreak, useContinue);
    } else if (elseStmt == null) {
        isTerminated = false;
    }
    return isTerminated;
}
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