Search in sources :

Example 11 with ASTType

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

the class VariableNameDeclaration method getArrayDepth.

public int getArrayDepth() {
    ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
    ASTType typeNode = astVariableDeclaratorId.getTypeNode();
    if (typeNode != null) {
        return ((Dimensionable) typeNode.jjtGetParent()).getArrayDepth();
    } else {
        return 0;
    }
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Dimensionable(net.sourceforge.pmd.lang.java.ast.Dimensionable)

Example 12 with ASTType

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

the class UselessOperationOnImmutableRule method visit.

@Override
public Object visit(ASTLocalVariableDeclaration node, Object data) {
    ASTVariableDeclaratorId var = getDeclaration(node);
    if (var == null) {
        return super.visit(node, data);
    }
    String variableName = var.getImage();
    for (NameOccurrence no : var.getUsages()) {
        // FIXME - getUsages will return everything with the same name as
        // the variable,
        // see JUnit test, case 6. Changing to Node below, revisit when
        // getUsages is fixed
        Node sn = no.getLocation();
        Node primaryExpression = sn.jjtGetParent().jjtGetParent();
        Class<? extends Node> parentClass = primaryExpression.jjtGetParent().getClass();
        if (parentClass.equals(ASTStatementExpression.class)) {
            String methodCall = sn.getImage().substring(variableName.length());
            ASTType nodeType = node.getTypeNode();
            if (nodeType != null) {
                if (MAP_CLASSES.get(nodeType.getTypeImage()).contains(methodCall)) {
                    addViolation(data, sn);
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 13 with ASTType

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

the class ConstructorCallsOverridableMethodRule method getMethodDeclaratorParameterTypes.

private static List<String> getMethodDeclaratorParameterTypes(Node methodOrConstructorDeclarator) {
    List<ASTFormalParameter> parameters = methodOrConstructorDeclarator.findDescendantsOfType(ASTFormalParameter.class);
    List<String> parameterTypes = new ArrayList<>();
    if (parameters != null) {
        for (ASTFormalParameter p : parameters) {
            ASTType type = p.getFirstChildOfType(ASTType.class);
            if (type.jjtGetChild(0) instanceof ASTPrimitiveType) {
                parameterTypes.add(type.jjtGetChild(0).getImage());
            } else if (type.jjtGetChild(0) instanceof ASTReferenceType) {
                parameterTypes.add("ref");
            } else {
                parameterTypes.add("<unkown>");
            }
        }
    }
    return parameterTypes;
}
Also used : ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ArrayList(java.util.ArrayList) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Example 14 with ASTType

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

the class UnusedFormalParameterRule method isSerializationMethod.

private boolean isSerializationMethod(ASTMethodDeclaration node) {
    ASTMethodDeclarator declarator = node.getFirstDescendantOfType(ASTMethodDeclarator.class);
    List<ASTFormalParameter> parameters = declarator.findDescendantsOfType(ASTFormalParameter.class);
    if (node.isPrivate() && "readObject".equals(node.getMethodName()) && parameters.size() == 1 && throwsOneException(node, InvalidObjectException.class)) {
        ASTType type = parameters.get(0).getTypeNode();
        if (type.getType() == ObjectInputStream.class || ObjectInputStream.class.getSimpleName().equals(type.getTypeImage()) || ObjectInputStream.class.getName().equals(type.getTypeImage())) {
            return true;
        }
    }
    return false;
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) InvalidObjectException(java.io.InvalidObjectException) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with ASTType

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

the class CouplingBetweenObjectsRule method visit.

@Override
public Object visit(ASTFieldDeclaration node, Object data) {
    for (int x = 0; x < node.jjtGetNumChildren(); ++x) {
        Node firstStmt = node.jjtGetChild(x);
        if (firstStmt instanceof ASTType) {
            ASTType tp = (ASTType) firstStmt;
            Node nd = tp.jjtGetChild(0);
            checkVariableType(nd, nd.getImage());
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Aggregations

ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)19 Node (net.sourceforge.pmd.lang.ast.Node)8 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)6 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ArrayList (java.util.ArrayList)4 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)2 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 Dimensionable (net.sourceforge.pmd.lang.java.ast.Dimensionable)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2