Search in sources :

Example 26 with ASTName

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

the class TccAttributeAccessCollector method getVariableName.

private String getVariableName(ASTPrimaryExpression node) {
    ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
    if (prefix.usesThisModifier()) {
        List<ASTPrimarySuffix> suffixes = node.findChildrenOfType(ASTPrimarySuffix.class);
        if (suffixes.size() > 1) {
            if (!suffixes.get(1).isArguments()) {
                // not a method call
                return suffixes.get(0).getImage();
            }
        }
    }
    ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
    String variableName = null;
    if (name != null) {
        int dotIndex = name.getImage().indexOf(".");
        if (dotIndex == -1) {
            variableName = name.getImage();
        } else {
            variableName = name.getImage().substring(0, dotIndex);
        }
    }
    return variableName;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 27 with ASTName

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

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

the class ForLoopCanBeForeachRule method getIterableDeclOfIteratorLoop.

private Entry<VariableNameDeclaration, List<NameOccurrence>> getIterableDeclOfIteratorLoop(VariableNameDeclaration indexDecl, Scope scope) {
    Node initializer = indexDecl.getNode().getFirstParentOfType(ASTVariableDeclarator.class).getFirstChildOfType(ASTVariableInitializer.class);
    if (initializer == null) {
        return null;
    }
    ASTName nameNode = initializer.getFirstDescendantOfType(ASTName.class);
    if (nameNode == null) {
        // TODO : This can happen if we are calling a local / statically imported method that returns the iterable - currently unhandled
        return null;
    }
    String name = nameNode.getImage();
    int dotIndex = name.indexOf('.');
    if (dotIndex > 0) {
        name = name.substring(0, dotIndex);
    }
    return findDeclaration(name, scope);
}
Also used : ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ScopedNode(net.sourceforge.pmd.lang.symboltable.ScopedNode)

Example 29 with ASTName

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

the class JUnitUseExpectedRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    List<ASTTryStatement> catches = node.findDescendantsOfType(ASTTryStatement.class);
    List<Node> found = new ArrayList<>();
    if (catches.isEmpty()) {
        return found;
    }
    for (ASTTryStatement trySt : catches) {
        ASTCatchStatement cStatement = getCatch(trySt);
        if (cStatement != null) {
            ASTBlock block = (ASTBlock) cStatement.jjtGetChild(1);
            if (block.jjtGetNumChildren() != 0) {
                continue;
            }
            List<ASTBlockStatement> blocks = trySt.jjtGetChild(0).findDescendantsOfType(ASTBlockStatement.class);
            if (blocks.isEmpty()) {
                continue;
            }
            ASTBlockStatement st = blocks.get(blocks.size() - 1);
            ASTName name = st.getFirstDescendantOfType(ASTName.class);
            if (name != null && st.equals(name.getNthParent(5)) && "fail".equals(name.getImage())) {
                found.add(name);
                continue;
            }
            ASTThrowStatement th = st.getFirstDescendantOfType(ASTThrowStatement.class);
            if (th != null && st.equals(th.getNthParent(2))) {
                found.add(th);
                continue;
            }
        }
    }
    return found;
}
Also used : ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTThrowStatement(net.sourceforge.pmd.lang.java.ast.ASTThrowStatement) Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ArrayList(java.util.ArrayList) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement)

Example 30 with ASTName

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

the class AbstractJUnitRule method doesNodeContainJUnitAnnotation.

private boolean doesNodeContainJUnitAnnotation(Node node, String annotationTypeClassName) {
    List<ASTAnnotation> annotations = node.findDescendantsOfType(ASTAnnotation.class);
    for (ASTAnnotation annotation : annotations) {
        Node annotationTypeNode = annotation.jjtGetChild(0);
        TypeNode annotationType = (TypeNode) annotationTypeNode;
        if (annotationType.getType() == null) {
            ASTName name = annotationTypeNode.getFirstChildOfType(ASTName.class);
            if (name != null && (name.hasImageEqualTo("Test") || name.hasImageEqualTo(annotationTypeClassName))) {
                return true;
            }
        } else if (TypeHelper.isA(annotationType, annotationTypeClassName)) {
            return true;
        }
    }
    return false;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode)

Aggregations

ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)53 Node (net.sourceforge.pmd.lang.ast.Node)25 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)17 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)14 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)11 ArrayList (java.util.ArrayList)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)10 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)7 List (java.util.List)6 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)6 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Map (java.util.Map)4 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)4 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)3