Search in sources :

Example 16 with ASTVariableDeclaratorId

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

the class CheckResultSetRule method visit.

@Override
public Object visit(ASTLocalVariableDeclaration node, Object data) {
    ASTClassOrInterfaceType type = node.getFirstChildOfType(ASTType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (type != null && (type.getType() != null && "java.sql.ResultSet".equals(type.getType().getName()) || "ResultSet".equals(type.getImage()))) {
        ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class);
        if (declarator != null) {
            ASTName name = declarator.getFirstDescendantOfType(ASTName.class);
            if (type.getType() != null || name != null && name.getImage().endsWith("executeQuery")) {
                ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class);
                resultSetVariables.put(id.getImage(), node);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 17 with ASTVariableDeclaratorId

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

the class SymbolTableTestRule method visit.

@Override
public Object visit(ASTFieldDeclaration node, Object data) {
    for (ASTVariableDeclaratorId declaration : node.findDescendantsOfType(ASTVariableDeclaratorId.class)) {
        for (NameOccurrence no : declaration.getUsages()) {
            Node location = no.getLocation();
            System.out.println(declaration.getImage() + " is used here: " + location.getImage());
        }
    }
    return data;
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 18 with ASTVariableDeclaratorId

use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId 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 19 with ASTVariableDeclaratorId

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

the class VariableNameDeclaration method isVarargs.

public boolean isVarargs() {
    ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
    ASTFormalParameter parameter = astVariableDeclaratorId.getFirstParentOfType(ASTFormalParameter.class);
    return parameter != null && parameter.isVarargs();
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Example 20 with ASTVariableDeclaratorId

use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId 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)

Aggregations

ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)28 Test (org.junit.Test)13 Node (net.sourceforge.pmd.lang.ast.Node)10 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)9 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)5 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)5 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 List (java.util.List)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 ASTVariableDeclarator (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator)3 ArrayList (java.util.ArrayList)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)2