Search in sources :

Example 1 with TypedNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration in project pmd by pmd.

the class InefficientStringBufferingRule method isInStringBufferOperation.

protected static boolean isInStringBufferOperation(Node node, int length, String methodName) {
    if (!(node.getNthParent(length) instanceof ASTStatementExpression)) {
        return false;
    }
    ASTStatementExpression s = node.getFirstParentOfType(ASTStatementExpression.class);
    if (s == null) {
        return false;
    }
    ASTName n = s.getFirstDescendantOfType(ASTName.class);
    if (n == null || n.getImage().indexOf(methodName) == -1 || !(n.getNameDeclaration() instanceof TypedNameDeclaration)) {
        return false;
    }
    // TODO having to hand-code this kind of dredging around is ridiculous
    // we need something to support this in the framework
    // but, "for now" (tm):
    // if more than one arg to append(), skip it
    ASTArgumentList argList = s.getFirstDescendantOfType(ASTArgumentList.class);
    if (argList == null || argList.jjtGetNumChildren() > 1) {
        return false;
    }
    return TypeHelper.isEither((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class, StringBuilder.class);
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 2 with TypedNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration in project pmd by pmd.

the class StringInstantiationRule method visit.

@Override
public Object visit(ASTAllocationExpression node, Object data) {
    if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
        return data;
    }
    if (!TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), String.class)) {
        return data;
    }
    List<ASTExpression> exp = node.findDescendantsOfType(ASTExpression.class);
    if (exp.size() >= 2) {
        return data;
    }
    if (node.hasDescendantOfAnyType(ASTArrayDimsAndInits.class, ASTAdditiveExpression.class)) {
        return data;
    }
    ASTName name = node.getFirstDescendantOfType(ASTName.class);
    // Literal, i.e., new String("foo")
    if (name == null) {
        addViolation(data, node);
        return data;
    }
    NameDeclaration nd = name.getNameDeclaration();
    if (nd == null) {
        return data;
    }
    if (nd instanceof TypedNameDeclaration && TypeHelper.isA((TypedNameDeclaration) nd, String.class)) {
        addViolation(data, node);
    }
    return data;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 3 with TypedNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration in project pmd by pmd.

the class UseStringBufferLengthRule method visit.

@Override
public Object visit(ASTName decl, Object data) {
    if (!decl.getImage().endsWith("toString")) {
        return data;
    }
    NameDeclaration nd = decl.getNameDeclaration();
    if (nd == null) {
        return data;
    }
    if (alreadySeen.contains(nd) || !(nd instanceof TypedNameDeclaration) || nd instanceof TypedNameDeclaration && TypeHelper.isNeither((TypedNameDeclaration) nd, StringBuffer.class, StringBuilder.class)) {
        return data;
    }
    alreadySeen.add(nd);
    if (isViolation(decl)) {
        addViolation(data, decl);
    }
    return data;
}
Also used : NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration)

Aggregations

TypedNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)2 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)1