Search in sources :

Example 16 with ASTPrimaryPrefix

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

the class AtfdBaseVisitor method getMethodOrAttributeName.

private String getMethodOrAttributeName(ASTPrimaryExpression node) {
    ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
    ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
    String methodOrAttributeName = null;
    if (name != null) {
        int dotIndex = name.getImage().indexOf(".");
        if (dotIndex > -1) {
            methodOrAttributeName = name.getImage().substring(dotIndex + 1);
        }
    }
    return methodOrAttributeName;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 17 with ASTPrimaryPrefix

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

the class AtfdBaseVisitor method getNameImage.

private String getNameImage(ASTPrimaryExpression node) {
    ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
    ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
    String image = null;
    if (name != null) {
        image = name.getImage();
    }
    return image;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 18 with ASTPrimaryPrefix

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

the class UselessStringValueOfRule method isPrimitive.

private static boolean isPrimitive(Node parent) {
    boolean result = false;
    if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
        Node child = parent.jjtGetChild(0);
        if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
            Node gc = child.jjtGetChild(0);
            if (gc instanceof ASTName) {
                ASTName name = (ASTName) gc;
                NameDeclaration nd = name.getNameDeclaration();
                if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
                    result = true;
                }
            } else if (gc instanceof ASTLiteral) {
                result = !((ASTLiteral) gc).isStringLiteral();
            }
        }
    }
    return result;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 19 with ASTPrimaryPrefix

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

the class JavaNameOccurrence method isSelfAssignment.

/**
 * Assert it the occurrence is a self assignment such as:
 * <code>i += 3;</code>
 *
 * @return true, if the occurrence is self-assignment, false, otherwise.
 */
@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop")
public boolean isSelfAssignment() {
    Node l = location;
    while (true) {
        Node p = l.jjtGetParent();
        Node gp = p.jjtGetParent();
        Node node = gp.jjtGetParent();
        if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) {
            return true;
        }
        if (hasAssignmentOperator(gp)) {
            return isCompoundAssignment(gp);
        }
        if (hasAssignmentOperator(node)) {
            return isCompoundAssignment(node);
        }
        // deal with extra parenthesis: "(i)++"
        if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1 && node instanceof ASTExpression && node.jjtGetNumChildren() == 1 && node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) {
            l = node;
            continue;
        }
        // catch this.i++ or ++this.i
        return gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression;
    }
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTPostfixExpression(net.sourceforge.pmd.lang.java.ast.ASTPostfixExpression) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPreDecrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreDecrementExpression) ASTPreIncrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreIncrementExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 20 with ASTPrimaryPrefix

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

the class JavaNameOccurrence method useThisOrSuper.

/**
 * Simply return if the image start with keyword 'this' or 'super'.
 *
 * @return true, if keyword is used, false otherwise.
 */
public boolean useThisOrSuper() {
    Node node = location.jjtGetParent();
    if (node instanceof ASTPrimaryExpression) {
        ASTPrimaryExpression primaryExpression = (ASTPrimaryExpression) node;
        ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0);
        if (prefix != null) {
            return prefix.usesSuperModifier() || prefix.usesThisModifier();
        }
    }
    return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)

Aggregations

ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)30 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)16 Node (net.sourceforge.pmd.lang.ast.Node)15 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)15 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)11 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)5 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 ArrayList (java.util.ArrayList)4 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)4 Test (org.junit.Test)4 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)3 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)2 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2