Search in sources :

Example 1 with ASTPrimaryPrefix

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

the class LocalScopeTest method testNameWithThisOrSuperIsNotFlaggedAsUnused.

@Test
public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {
    LocalScope scope = new LocalScope();
    ASTName name = new ASTName(1);
    name.setImage("foo");
    ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
    prefix.setUsesThisModifier();
    name.jjtAddChild(prefix, 1);
    JavaNameOccurrence occ = new JavaNameOccurrence(name, "foo");
    scope.addNameOccurrence(occ);
    assertFalse(scope.getDeclarations().keySet().iterator().hasNext());
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Test(org.junit.Test)

Example 2 with ASTPrimaryPrefix

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

the class InsufficientStringBufferDeclarationRule method processNode.

private int processNode(Node sn) {
    int anticipatedLength = 0;
    if (sn != null) {
        ASTPrimaryPrefix xn = sn.getFirstDescendantOfType(ASTPrimaryPrefix.class);
        if (xn != null) {
            if (xn.jjtGetNumChildren() != 0 && xn.jjtGetChild(0) instanceof ASTLiteral) {
                ASTLiteral literal = (ASTLiteral) xn.jjtGetChild(0);
                String str = xn.jjtGetChild(0).getImage();
                if (str != null) {
                    if (literal.isStringLiteral()) {
                        anticipatedLength += str.length() - 2;
                    } else if (literal.isCharLiteral()) {
                        anticipatedLength += 1;
                    } else if (literal.isIntLiteral()) {
                        // but only if we are not inside a cast expression
                        Node parentNode = literal.jjtGetParent().jjtGetParent().jjtGetParent();
                        if (parentNode instanceof ASTCastExpression && ((ASTCastExpression) parentNode).getType() == char.class) {
                            anticipatedLength += 1;
                        } else {
                            // any number, regardless of the base will be converted to base 10
                            // e.g. 0xdeadbeef -> will be converted to a
                            // base 10 integer string: 3735928559
                            anticipatedLength += String.valueOf(literal.getValueAsLong()).length();
                        }
                    } else {
                        anticipatedLength += str.length();
                    }
                }
            }
        }
    }
    return anticipatedLength;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) Node(net.sourceforge.pmd.lang.ast.Node) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 3 with ASTPrimaryPrefix

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

the class ConstructorCallsOverridableMethodRule method getArgumentTypes.

private static List<String> getArgumentTypes(ASTArguments args) {
    List<String> argumentTypes = new ArrayList<>();
    ASTArgumentList argumentList = args.getFirstChildOfType(ASTArgumentList.class);
    if (argumentList != null) {
        for (int a = 0; a < argumentList.jjtGetNumChildren(); a++) {
            Node expression = argumentList.jjtGetChild(a);
            ASTPrimaryPrefix arg = expression.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            String type = "<unknown>";
            if (arg != null && arg.jjtGetNumChildren() > 0) {
                if (arg.jjtGetChild(0) instanceof ASTLiteral) {
                    ASTLiteral lit = (ASTLiteral) arg.jjtGetChild(0);
                    if (lit.isCharLiteral()) {
                        type = "char";
                    } else if (lit.isFloatLiteral()) {
                        type = "float";
                    } else if (lit.isIntLiteral()) {
                        type = "int";
                    } else if (lit.isStringLiteral()) {
                        type = "String";
                    } else if (lit.jjtGetNumChildren() > 0 && lit.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                        type = "boolean";
                    } else if (lit.isDoubleLiteral()) {
                        type = "double";
                    } else if (lit.isLongLiteral()) {
                        type = "long";
                    }
                } else if (arg.jjtGetChild(0) instanceof ASTName) {
                    // ASTName n = (ASTName)arg.jjtGetChild(0);
                    type = "ref";
                }
            }
            argumentTypes.add(type);
        }
    }
    return argumentTypes;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ArrayList(java.util.ArrayList) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 4 with ASTPrimaryPrefix

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

the class CheckSkipResultRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    ASTType typeNode = node.getTypeNode();
    if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
        return data;
    }
    for (NameOccurrence occ : node.getUsages()) {
        JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
        NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
        if (qualifier != null && "skip".equals(qualifier.getImage())) {
            Node loc = jocc.getLocation();
            if (loc != null) {
                ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
                while (exp != null) {
                    if (exp.jjtGetParent() instanceof ASTStatementExpression) {
                        // if exp is in a bare statement,
                        // the returned value is not used
                        addViolation(data, occ.getLocation());
                        break;
                    } else if (exp.jjtGetParent() instanceof ASTExpression && exp.jjtGetParent().jjtGetParent() instanceof ASTPrimaryPrefix) {
                        // if exp is enclosed in a pair of parenthesis
                        // let's have a look at the enclosing expression
                        // we'll see if it's in a bare statement
                        exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
                    } else {
                        // or assignement so the returned value is used
                        break;
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 5 with ASTPrimaryPrefix

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

the class CloseResourceRule method ensureClosed.

private void ensureClosed(ASTLocalVariableDeclaration var, ASTVariableDeclaratorId id, Object data) {
    // What are the chances of a Connection being instantiated in a
    // for-loop init block? Anyway, I'm lazy!
    String variableToClose = id.getImage();
    Node n = var;
    while (!(n instanceof ASTBlock) && !(n instanceof ASTConstructorDeclaration)) {
        n = n.jjtGetParent();
    }
    Node top = n;
    List<ASTTryStatement> tryblocks = top.findDescendantsOfType(ASTTryStatement.class);
    boolean closed = false;
    ASTBlockStatement parentBlock = id.getFirstParentOfType(ASTBlockStatement.class);
    // block.
    for (ASTTryStatement t : tryblocks) {
        // verifies that there are no critical statements between the
        // variable declaration and
        // the beginning of the try block.
        ASTBlockStatement tryBlock = t.getFirstParentOfType(ASTBlockStatement.class);
        // the variable has been initialized with null
        if (!hasNullInitializer(var) && parentBlock.jjtGetParent() == tryBlock.jjtGetParent()) {
            List<ASTBlockStatement> blocks = parentBlock.jjtGetParent().findChildrenOfType(ASTBlockStatement.class);
            int parentBlockIndex = blocks.indexOf(parentBlock);
            int tryBlockIndex = blocks.indexOf(tryBlock);
            boolean criticalStatements = false;
            for (int i = parentBlockIndex + 1; i < tryBlockIndex; i++) {
                // assume variable declarations are not critical
                ASTLocalVariableDeclaration varDecl = blocks.get(i).getFirstDescendantOfType(ASTLocalVariableDeclaration.class);
                if (varDecl == null) {
                    criticalStatements = true;
                    break;
                }
            }
            if (criticalStatements) {
                break;
            }
        }
        if (t.getBeginLine() > id.getBeginLine() && t.hasFinally()) {
            ASTBlock f = (ASTBlock) t.getFinally().jjtGetChild(0);
            List<ASTName> names = f.findDescendantsOfType(ASTName.class);
            for (ASTName oName : names) {
                String name = oName.getImage();
                if (name != null && name.contains(".")) {
                    String[] parts = name.split("\\.");
                    if (parts.length == 2) {
                        String methodName = parts[1];
                        String varName = parts[0];
                        if (varName.equals(variableToClose) && closeTargets.contains(methodName) && nullCheckIfCondition(f, oName, varName)) {
                            closed = true;
                            break;
                        }
                    }
                }
            }
            if (closed) {
                break;
            }
            List<ASTStatementExpression> exprs = new ArrayList<>();
            f.findDescendantsOfType(ASTStatementExpression.class, exprs, true);
            for (ASTStatementExpression stmt : exprs) {
                ASTPrimaryExpression expr = stmt.getFirstChildOfType(ASTPrimaryExpression.class);
                if (expr != null) {
                    ASTPrimaryPrefix prefix = expr.getFirstChildOfType(ASTPrimaryPrefix.class);
                    ASTPrimarySuffix suffix = expr.getFirstChildOfType(ASTPrimarySuffix.class);
                    if (prefix != null && suffix != null) {
                        if (prefix.getImage() == null) {
                            ASTName prefixName = prefix.getFirstChildOfType(ASTName.class);
                            if (prefixName != null && closeTargets.contains(prefixName.getImage())) {
                                // Found a call to a "close target" that is
                                // a direct
                                // method call without a "ClassName."
                                // prefix.
                                closed = variableIsPassedToMethod(expr, variableToClose);
                                if (closed) {
                                    break;
                                }
                            }
                        } else if (suffix.getImage() != null) {
                            String prefixPlusSuffix = prefix.getImage() + "." + suffix.getImage();
                            if (closeTargets.contains(prefixPlusSuffix)) {
                                // Found a call to a "close target" that is
                                // a method call
                                // in the form "ClassName.methodName".
                                closed = variableIsPassedToMethod(expr, variableToClose);
                                if (closed) {
                                    break;
                                }
                            }
                        }
                        // really check it.
                        if (!closed) {
                            List<ASTPrimarySuffix> suffixes = new ArrayList<>();
                            expr.findDescendantsOfType(ASTPrimarySuffix.class, suffixes, true);
                            for (ASTPrimarySuffix oSuffix : suffixes) {
                                String suff = oSuffix.getImage();
                                if (closeTargets.contains(suff)) {
                                    closed = variableIsPassedToMethod(expr, variableToClose);
                                    if (closed) {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (closed) {
                break;
            }
        }
    }
    if (!closed) {
        // See if the variable is returned by the method, which means the
        // method is a utility for creating the db resource, which means of
        // course it can't be closed by the method, so it isn't an error.
        List<ASTReturnStatement> returns = new ArrayList<>();
        top.findDescendantsOfType(ASTReturnStatement.class, returns, true);
        for (ASTReturnStatement returnStatement : returns) {
            ASTName name = returnStatement.getFirstDescendantOfType(ASTName.class);
            if (name != null && name.getImage().equals(variableToClose)) {
                closed = true;
                break;
            }
        }
    }
    // if all is not well, complain
    if (!closed) {
        ASTType type = var.getFirstChildOfType(ASTType.class);
        ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
        ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
        addViolation(data, id, clazz.getImage());
    }
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

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