use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class LocalScope method addNameOccurrence.
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
nameOccurrences.add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
return declarations;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class InefficientStringBufferingRule method visit.
@Override
public Object visit(ASTAdditiveExpression node, Object data) {
ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
if (bs == null) {
return data;
}
int immediateLiterals = 0;
int immediateStringLiterals = 0;
List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
for (ASTLiteral literal : nodes) {
if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
immediateLiterals++;
if (literal.isStringLiteral()) {
immediateStringLiterals++;
}
}
if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
return data;
}
}
if (immediateLiterals > 1) {
return data;
}
// if literal + public static final, return
List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
for (ASTName name : nameNodes) {
if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
AccessNode accessNodeParent = vnd.getAccessNodeParent();
if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
return data;
}
}
}
// if literal primitive type and not strings variables, then return
boolean stringFound = false;
for (ASTName name : nameNodes) {
if (!isPrimitiveType(name) && isStringType(name)) {
stringFound = true;
break;
}
}
if (!stringFound && immediateStringLiterals == 0) {
return data;
}
if (bs.isAllocation()) {
for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
ASTName name = iterator.next();
if (!name.getImage().endsWith("length")) {
break;
} else if (!iterator.hasNext()) {
// All names end with length
return data;
}
}
if (isAllocatedStringBuffer(node)) {
addViolation(data, node);
}
} else if (isInStringBufferOperation(node, 6, "append")) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method getConstructorLength.
private int getConstructorLength(Node node, int constructorLength) {
int iConstructorLength = constructorLength;
Node block = node.getFirstParentOfType(ASTBlockStatement.class);
if (block == null) {
block = node.getFirstParentOfType(ASTFieldDeclaration.class);
}
if (block == null) {
block = node.getFirstParentOfType(ASTFormalParameter.class);
if (block != null) {
iConstructorLength = -1;
} else {
return DEFAULT_BUFFER_SIZE;
}
}
// if there is any addition/subtraction going on then just use the
// default.
ASTAdditiveExpression exp = block.getFirstDescendantOfType(ASTAdditiveExpression.class);
if (exp != null) {
return DEFAULT_BUFFER_SIZE;
}
ASTMultiplicativeExpression mult = block.getFirstDescendantOfType(ASTMultiplicativeExpression.class);
if (mult != null) {
return DEFAULT_BUFFER_SIZE;
}
List<ASTLiteral> literals;
ASTAllocationExpression constructorCall = block.getFirstDescendantOfType(ASTAllocationExpression.class);
if (constructorCall != null) {
// if this is a constructor call, only consider the literals within
// it.
literals = constructorCall.findDescendantsOfType(ASTLiteral.class);
} else {
// otherwise it might be a setLength call...
literals = block.findDescendantsOfType(ASTLiteral.class);
}
if (literals.isEmpty()) {
List<ASTName> name = block.findDescendantsOfType(ASTName.class);
if (!name.isEmpty()) {
iConstructorLength = -1;
}
} else if (literals.size() == 1) {
ASTLiteral literal = literals.get(0);
String str = literal.getImage();
if (str == null) {
iConstructorLength = 0;
} else if (isStringOrCharLiteral(literal)) {
// since it's not taken into account
// anywhere. only count the extra 16
// characters
// don't add the constructor's length
iConstructorLength = 14 + str.length();
} else if (literal.isIntLiteral()) {
iConstructorLength = literal.getValueAsInt();
}
} else {
iConstructorLength = -1;
}
if (iConstructorLength == 0) {
if (constructorLength == -1) {
iConstructorLength = DEFAULT_BUFFER_SIZE;
} else {
iConstructorLength = constructorLength;
}
}
return iConstructorLength;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class UseStringBufferForStringAppendsRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node, String.class) || node.isArray()) {
return data;
}
Node parent = node.jjtGetParent().jjtGetParent();
if (!(parent instanceof ASTLocalVariableDeclaration)) {
return data;
}
for (NameOccurrence no : node.getUsages()) {
Node name = no.getLocation();
ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
if (statement == null) {
continue;
}
ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in method call
continue;
}
ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in condition
continue;
}
ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
if (conditional != null) {
Node thirdParent = name.getNthParent(3);
Node fourthParent = name.getNthParent(4);
if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// string)
continue;
}
}
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
if (astName != null) {
if (astName.equals(name)) {
ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
if (assignmentOperator != null && assignmentOperator.isCompound()) {
addViolation(data, assignmentOperator);
}
} else if (astName.getImage().equals(name.getImage())) {
ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
if (assignmentOperator != null && !assignmentOperator.isCompound()) {
addViolation(data, astName);
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class NonThreadSafeSingletonRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) {
return super.visit(node, data);
}
List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class);
for (ASTIfStatement ifStatement : ifStatements) {
if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
if (!ifStatement.hasDescendantOfType(ASTNullLiteral.class)) {
continue;
}
ASTName n = ifStatement.getFirstDescendantOfType(ASTName.class);
if (n == null || !fieldDecls.containsKey(n.getImage())) {
continue;
}
List<ASTAssignmentOperator> assigmnents = ifStatement.findDescendantsOfType(ASTAssignmentOperator.class);
boolean violation = false;
for (int ix = 0; ix < assigmnents.size(); ix++) {
ASTAssignmentOperator oper = assigmnents.get(ix);
if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
continue;
}
ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
if (expr.jjtGetChild(0) instanceof ASTPrimaryExpression && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetNumChildren() == 1 && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix) {
ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
String name = null;
if (pp.usesThisModifier()) {
ASTPrimarySuffix priSuf = expr.getFirstDescendantOfType(ASTPrimarySuffix.class);
name = priSuf.getImage();
} else {
ASTName astName = (ASTName) pp.jjtGetChild(0);
name = astName.getImage();
}
if (fieldDecls.containsKey(name)) {
violation = true;
}
}
}
if (violation) {
addViolation(data, ifStatement);
}
}
}
return super.visit(node, data);
}
Aggregations