Search in sources :

Example 16 with ASTName

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

the class ForLoopCanBeForeachRule method isReplaceableIteratorLoop.

private boolean isReplaceableIteratorLoop(Entry<VariableNameDeclaration, List<NameOccurrence>> indexInfo, ASTExpression guardCondition, Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo, ASTForStatement stmt) {
    if (isIterableModifiedInsideLoop(iterableInfo, stmt)) {
        return false;
    }
    String indexName = indexInfo.getKey().getName();
    if (indexName == null) {
        return false;
    }
    if (!guardCondition.hasDescendantMatchingXPath("./PrimaryExpression/PrimaryPrefix/Name[@Image='" + indexName + ".hasNext']")) {
        return false;
    }
    List<NameOccurrence> occurrences = indexInfo.getValue();
    if (occurrences.size() > 2) {
        return false;
    }
    for (NameOccurrence occ : indexInfo.getValue()) {
        ScopedNode location = occ.getLocation();
        boolean isCallingNext = location instanceof ASTName && (location.hasImageEqualTo(indexName + ".hasNext") || location.hasImageEqualTo(indexName + ".next"));
        if (!isCallingNext) {
            return false;
        }
    }
    return true;
}
Also used : ScopedNode(net.sourceforge.pmd.lang.symboltable.ScopedNode) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 17 with ASTName

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

the class GuardLogStatementRule method getLogLevelName.

/**
 * Determines the log level, that is used. It is either the called method name
 * itself or - if the method has a first argument a primary prefix - the first argument.
 *
 * @param node the method call
 * @param methodCallName the called method name previously determined
 * @return the log level
 */
private String getLogLevelName(Node node, String methodCallName) {
    String logLevel = methodCallName;
    ASTPrimarySuffix suffix = node.getFirstDescendantOfType(ASTPrimarySuffix.class);
    if (suffix != null) {
        ASTArgumentList argumentList = suffix.getFirstDescendantOfType(ASTArgumentList.class);
        if (argumentList != null && argumentList.hasDescendantOfType(ASTName.class)) {
            ASTName name = argumentList.getFirstDescendantOfType(ASTName.class);
            String lastPart = getLastPartOfName(name.getImage());
            lastPart = lastPart.toLowerCase(Locale.ROOT);
            if (!lastPart.isEmpty()) {
                logLevel = lastPart;
            }
        }
    }
    return logLevel;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 18 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(ASTClassOrInterfaceBodyDeclaration node, Object data) {
    boolean inAnnotation = false;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        Node child = node.jjtGetChild(i);
        if (child instanceof ASTAnnotation) {
            ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
            if ("Test".equals(annotationName.getImage())) {
                inAnnotation = true;
                continue;
            }
        }
        if (child instanceof ASTMethodDeclaration) {
            boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
            if (inAnnotation || isJUnitMethod) {
                List<Node> found = new ArrayList<>();
                found.addAll((List<Node>) visit((ASTMethodDeclaration) child, data));
                for (Node name : found) {
                    addViolation(data, name);
                }
            }
        }
        inAnnotation = false;
    }
    return super.visit(node, data);
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation) ArrayList(java.util.ArrayList)

Example 19 with ASTName

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

the class PreserveStackTraceRule method checkForTargetUsage.

/**
 * Checks whether the given target is in the argument list. If this is the
 * case, then the target (root exception) is used as the cause.
 *
 * @param target
 * @param baseNode
 */
private boolean checkForTargetUsage(String target, Node baseNode) {
    boolean match = false;
    if (target != null && baseNode != null) {
        // TODO : use Node.findDescendantsOfType(ASTName.class, true) on 7.0.0
        List<ASTName> nameNodes = new ArrayList<>();
        baseNode.findDescendantsOfType(ASTName.class, nameNodes, true);
        for (ASTName nameNode : nameNodes) {
            if (target.equals(nameNode.getImage())) {
                boolean isPartOfStringConcatenation = isStringConcat(nameNode, baseNode);
                if (!isPartOfStringConcatenation) {
                    match = true;
                    break;
                }
            }
        }
    }
    return match;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ArrayList(java.util.ArrayList)

Example 20 with ASTName

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

the class UnusedFormalParameterRule method hasOverrideAnnotation.

private boolean hasOverrideAnnotation(ASTMethodDeclaration node) {
    int childIndex = node.jjtGetChildIndex();
    for (int i = 0; i < childIndex; i++) {
        Node previousSibling = node.jjtGetParent().jjtGetChild(i);
        List<ASTMarkerAnnotation> annotations = previousSibling.findDescendantsOfType(ASTMarkerAnnotation.class);
        for (ASTMarkerAnnotation annotation : annotations) {
            ASTName name = annotation.getFirstChildOfType(ASTName.class);
            if (name != null && (name.hasImageEqualTo("Override") || name.hasImageEqualTo("java.lang.Override"))) {
                return true;
            }
        }
    }
    return false;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTMarkerAnnotation(net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation)

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