Search in sources :

Example 16 with ASTClassOrInterfaceType

use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.

the class UnsynchronizedStaticDateFormatterRule method visit.

@Override
public Object visit(ASTFieldDeclaration node, Object data) {
    if (!node.isStatic()) {
        return data;
    }
    ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (cit == null || !targets.contains(cit.getImage())) {
        return data;
    }
    ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    for (NameOccurrence occ : var.getUsages()) {
        Node n = occ.getLocation();
        if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
            continue;
        }
        // ignore usages, that don't call a method.
        if (!n.getImage().contains(".")) {
            continue;
        }
        ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class);
        if (method != null && !method.isSynchronized()) {
            addViolation(data, n);
        }
    }
    return data;
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 17 with ASTClassOrInterfaceType

use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.

the class OverrideBothEqualsAndHashcodeRule method visit.

@Override
public Object visit(ASTImplementsList node, Object data) {
    for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
        if (node.jjtGetChild(ix) instanceof ASTClassOrInterfaceType) {
            ASTClassOrInterfaceType cit = (ASTClassOrInterfaceType) node.jjtGetChild(ix);
            Class<?> clazz = cit.getType();
            if (clazz != null && node.jjtGetChild(ix).hasImageEqualTo("Comparable")) {
                implementsComparable = true;
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 18 with ASTClassOrInterfaceType

use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.

the class OverrideBothEqualsAndHashcodeRule method visit.

@Override
public Object visit(ASTMethodDeclarator node, Object data) {
    if (implementsComparable) {
        return data;
    }
    int iFormalParams = 0;
    String paramName = null;
    for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
        Node sn = node.jjtGetChild(ix);
        if (sn instanceof ASTFormalParameters) {
            List<ASTFormalParameter> allParams = ((ASTFormalParameters) sn).findChildrenOfType(ASTFormalParameter.class);
            for (ASTFormalParameter formalParam : allParams) {
                iFormalParams++;
                ASTClassOrInterfaceType param = formalParam.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                if (param != null) {
                    paramName = param.getImage();
                }
            }
        }
    }
    if (iFormalParams == 0 && node.hasImageEqualTo("hashCode")) {
        containsHashCode = true;
        nodeFound = node;
    } else if (iFormalParams == 1 && node.hasImageEqualTo("equals") && ("Object".equals(paramName) || "java.lang.Object".equals(paramName))) {
        containsEquals = true;
        nodeFound = node;
    }
    return super.visit(node, data);
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 19 with ASTClassOrInterfaceType

use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.

the class UnnecessaryCastRule method process.

private Object process(Node node, Object data) {
    ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (cit == null || !implClassNames.contains(cit.getImage())) {
        return data;
    }
    cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (cit == null) {
        return data;
    }
    ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    List<NameOccurrence> usages = decl.getUsages();
    for (NameOccurrence no : usages) {
        ASTName name = (ASTName) no.getLocation();
        Node n = name.jjtGetParent().jjtGetParent().jjtGetParent();
        if (n instanceof ASTCastExpression) {
            addViolation(data, n);
        }
    }
    return null;
}
Also used : ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 20 with ASTClassOrInterfaceType

use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.

the class SignatureDeclareThrowsExceptionRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    if (junitImported) {
        return super.visit(node, data);
    }
    ASTImplementsList impl = node.getFirstChildOfType(ASTImplementsList.class);
    if (impl != null && impl.jjtGetParent().equals(node)) {
        for (int ix = 0; ix < impl.jjtGetNumChildren(); ix++) {
            Node child = impl.jjtGetChild(ix);
            if (child.getClass() != ASTClassOrInterfaceType.class) {
                continue;
            }
            ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) child;
            if (isJUnitTest(type)) {
                junitImported = true;
                return super.visit(node, data);
            }
        }
    }
    if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0) instanceof ASTExtendsList) {
        ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0).jjtGetChild(0);
        if (isJUnitTest(type)) {
            junitImported = true;
            return super.visit(node, data);
        }
    }
    return super.visit(node, data);
}
Also used : ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) Node(net.sourceforge.pmd.lang.ast.Node) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList)

Aggregations

ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)25 Node (net.sourceforge.pmd.lang.ast.Node)13 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)7 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)5 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)5 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ArrayList (java.util.ArrayList)4 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)4 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)4 List (java.util.List)3 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)3 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)3 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)3 Map (java.util.Map)2 RuleContext (net.sourceforge.pmd.RuleContext)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2