Search in sources :

Example 16 with DetailAST

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

the class EqualsAvoidNullCheck method isObjectValid.

/**
     * Check whether the object equals method is called on is not a String literal
     * and not too complex.
     * @param objCalledOn the object equals method is called on ast.
     * @return true if the object is valid.
     */
private static boolean isObjectValid(DetailAST objCalledOn) {
    boolean result = true;
    final DetailAST previousSibling = objCalledOn.getPreviousSibling();
    if (previousSibling != null && previousSibling.getType() == TokenTypes.DOT) {
        result = false;
    }
    if (isStringLiteral(objCalledOn)) {
        result = false;
    }
    return result;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 17 with DetailAST

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

the class EqualsAvoidNullCheck method isStringFieldOrVariableFromClass.

/**
     * Whether the field or the variable from the specified class is of String type.
     * @param objCalledOn the field or the variable from the specified class to check.
     * @param className the name of the class to check in.
     * @return true if the field or the variable from the specified class is of String type.
     */
private boolean isStringFieldOrVariableFromClass(DetailAST objCalledOn, final String className) {
    boolean result = false;
    final String name = objCalledOn.getText();
    FieldFrame frame = getObjectFrame(currentFrame);
    while (frame != null) {
        if (className.equals(frame.getFrameName())) {
            final DetailAST field = frame.findField(name);
            if (field != null) {
                result = STRING.equals(getFieldType(field));
            }
            break;
        }
        frame = getObjectFrame(frame.getParent());
    }
    return result;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 18 with DetailAST

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

the class EqualsAvoidNullCheck method isStringFieldOrVariable.

/**
     * Whether the field or the variable is of String type.
     * @param objCalledOn the field or the variable to check.
     * @return true if the field or the variable is of String type.
     */
private boolean isStringFieldOrVariable(DetailAST objCalledOn) {
    boolean result = false;
    final String name = objCalledOn.getText();
    FieldFrame frame = currentFrame;
    while (frame != null) {
        final DetailAST field = frame.findField(name);
        if (field != null && (frame.isClassOrEnumOrEnumConstDef() || checkLineNo(field, objCalledOn))) {
            result = STRING.equals(getFieldType(field));
            break;
        }
        frame = frame.getParent();
    }
    return result;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 19 with DetailAST

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

the class EqualsHashCodeCheck method finishTree.

@Override
public void finishTree(DetailAST rootAST) {
    objBlockWithEquals.entrySet().stream().filter(detailASTDetailASTEntry -> {
        return objBlockWithHashCode.remove(detailASTDetailASTEntry.getKey()) == null;
    }).forEach(detailASTDetailASTEntry -> {
        final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
        log(equalsAST.getLineNo(), equalsAST.getColumnNo(), MSG_KEY_HASHCODE);
    });
    objBlockWithHashCode.entrySet().forEach(detailASTDetailASTEntry -> {
        final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
        log(equalsAST.getLineNo(), equalsAST.getColumnNo(), MSG_KEY_EQUALS);
    });
    objBlockWithEquals.clear();
    objBlockWithHashCode.clear();
}
Also used : AST(antlr.collections.AST) TokenTypes(com.puppycrawl.tools.checkstyle.api.TokenTypes) FullIdent(com.puppycrawl.tools.checkstyle.api.FullIdent) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) CheckUtils(com.puppycrawl.tools.checkstyle.utils.CheckUtils) Map(java.util.Map) HashMap(java.util.HashMap) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 20 with DetailAST

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

the class EqualsHashCodeCheck method isEqualsMethod.

/**
     * Determines if an AST is a valid Equals method implementation.
     *
     * @param ast the AST to check
     * @return true if the {code ast} is a Equals method.
     */
private static boolean isEqualsMethod(DetailAST ast) {
    final DetailAST modifiers = ast.getFirstChild();
    final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
    return CheckUtils.isEqualsMethod(ast) && modifiers.branchContains(TokenTypes.LITERAL_PUBLIC) && isObjectParam(parameters.getFirstChild()) && (ast.branchContains(TokenTypes.SLIST) || modifiers.branchContains(TokenTypes.LITERAL_NATIVE));
}
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