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);
}
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;
}
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;
}
Aggregations