use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator in project pmd by pmd.
the class RedundantFieldInitializerRule method visit.
public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) {
// Finals can only be initialized once.
if (fieldDeclaration.isFinal()) {
return data;
}
// VariableDeclarator/VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal
for (ASTVariableDeclarator variableDeclarator : fieldDeclaration.findChildrenOfType(ASTVariableDeclarator.class)) {
if (variableDeclarator.jjtGetNumChildren() > 1) {
final Node variableInitializer = variableDeclarator.jjtGetChild(1);
if (variableInitializer.jjtGetChild(0) instanceof ASTExpression) {
final Node expression = variableInitializer.jjtGetChild(0);
final Node primaryExpression;
if (expression.jjtGetNumChildren() == 1) {
if (expression.jjtGetChild(0) instanceof ASTPrimaryExpression) {
primaryExpression = expression.jjtGetChild(0);
} else if (expression.jjtGetChild(0) instanceof ASTCastExpression && expression.jjtGetChild(0).jjtGetChild(1) instanceof ASTPrimaryExpression) {
primaryExpression = expression.jjtGetChild(0).jjtGetChild(1);
} else {
continue;
}
} else {
continue;
}
final Node primaryPrefix = primaryExpression.jjtGetChild(0);
if (primaryPrefix.jjtGetNumChildren() == 1 && primaryPrefix.jjtGetChild(0) instanceof ASTLiteral) {
final ASTLiteral literal = (ASTLiteral) primaryPrefix.jjtGetChild(0);
if (isRef(fieldDeclaration, variableDeclarator)) {
// Reference type
if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTNullLiteral) {
addViolation(data, variableDeclarator);
}
} else {
// Primitive type
if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
// boolean type
ASTBooleanLiteral booleanLiteral = (ASTBooleanLiteral) literal.jjtGetChild(0);
if (!booleanLiteral.isTrue()) {
addViolation(data, variableDeclarator);
}
} else if (literal.jjtGetNumChildren() == 0) {
// numeric type
// Note: Not catching NumberFormatException, as
// it shouldn't be happening on valid source
// code.
Number value = -1;
if (literal.isIntLiteral()) {
value = literal.getValueAsInt();
} else if (literal.isLongLiteral()) {
value = literal.getValueAsLong();
} else if (literal.isFloatLiteral()) {
value = literal.getValueAsFloat();
} else if (literal.isDoubleLiteral()) {
value = literal.getValueAsDouble();
} else if (literal.isCharLiteral()) {
value = (int) literal.getImage().charAt(1);
}
if (value.doubleValue() == 0) {
addViolation(data, variableDeclarator);
}
}
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator in project pmd by pmd.
the class ForLoopCanBeForeachRule method indexStartsAtZero.
/* We only report loops with int initializers starting at zero. */
private boolean indexStartsAtZero(VariableNameDeclaration index) {
ASTVariableDeclaratorId name = (ASTVariableDeclaratorId) index.getNode();
ASTVariableDeclarator declarator = name.getFirstParentOfType(ASTVariableDeclarator.class);
if (declarator == null) {
return false;
}
try {
List<Node> zeroLiteral = declarator.findChildNodesWithXPath("./VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0' and " + "@StringLiteral='false']");
if (!zeroLiteral.isEmpty()) {
return true;
}
} catch (JaxenException je) {
throw new RuntimeException(je);
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator in project pmd by pmd.
the class CheckResultSetRule method visit.
@Override
public Object visit(ASTLocalVariableDeclaration node, Object data) {
ASTClassOrInterfaceType type = node.getFirstChildOfType(ASTType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (type != null && (type.getType() != null && "java.sql.ResultSet".equals(type.getType().getName()) || "ResultSet".equals(type.getImage()))) {
ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class);
if (declarator != null) {
ASTName name = declarator.getFirstDescendantOfType(ASTName.class);
if (type.getType() != null || name != null && name.getImage().endsWith("executeQuery")) {
ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class);
resultSetVariables.put(id.getImage(), node);
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator in project pmd by pmd.
the class SingularFieldRule method visit.
@SuppressWarnings("PMD.CompareObjectsWithEquals")
@Override
public Object visit(ASTFieldDeclaration node, Object data) {
boolean checkInnerClasses = getProperty(CHECK_INNER_CLASSES);
boolean disallowNotAssignment = getProperty(DISALLOW_NOT_ASSIGNMENT);
if (node.isPrivate() && !node.isStatic() && !hasClassLombokAnnotation() && !hasLombokAnnotation(node)) {
for (ASTVariableDeclarator declarator : node.findChildrenOfType(ASTVariableDeclarator.class)) {
ASTVariableDeclaratorId declaration = (ASTVariableDeclaratorId) declarator.jjtGetChild(0);
List<NameOccurrence> usages = declaration.getUsages();
Node decl = null;
boolean violation = true;
for (int ix = 0; ix < usages.size(); ix++) {
NameOccurrence no = usages.get(ix);
Node location = no.getLocation();
ASTPrimaryExpression primaryExpressionParent = location.getFirstParentOfType(ASTPrimaryExpression.class);
if (ix == 0 && !disallowNotAssignment) {
if (primaryExpressionParent.getFirstParentOfType(ASTIfStatement.class) != null) {
// the first usage is in an if, so it may be skipped
// on
// later calls to the method. So this might be legit
// code
// that simply stores an object for later use.
violation = false;
// Optimization
break;
}
// Is the first usage in an assignment?
Node potentialStatement = primaryExpressionParent.jjtGetParent();
// Check that the assignment is not to a field inside
// the field object
boolean assignmentToField = no.getImage().equals(location.getImage());
if (!assignmentToField || !isInAssignment(potentialStatement)) {
violation = false;
// Optimization
break;
} else {
if (usages.size() > ix + 1) {
Node secondUsageLocation = usages.get(ix + 1).getLocation();
List<ASTStatementExpression> parentStatements = secondUsageLocation.getParentsOfType(ASTStatementExpression.class);
for (ASTStatementExpression statementExpression : parentStatements) {
if (statementExpression != null && statementExpression.equals(potentialStatement)) {
// The second usage is in the assignment
// of the first usage, which is allowed
violation = false;
// Optimization
break;
}
}
}
}
}
if (!checkInnerClasses) {
// Skip inner classes because the field can be used in
// the outer class and checking this is too difficult
ASTClassOrInterfaceDeclaration clazz = location.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
if (clazz != null && clazz.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) != null) {
violation = false;
// Optimization
break;
}
}
if (primaryExpressionParent.jjtGetParent() instanceof ASTSynchronizedStatement) {
// This usage is directly in an expression of a
// synchronized block
violation = false;
// Optimization
break;
}
if (location.getFirstParentOfType(ASTLambdaExpression.class) != null) {
// This usage is inside a lambda expression
violation = false;
// Optimization
break;
}
Node method = location.getFirstParentOfType(ASTMethodDeclaration.class);
if (method == null) {
method = location.getFirstParentOfType(ASTConstructorDeclaration.class);
if (method == null) {
method = location.getFirstParentOfType(ASTInitializer.class);
if (method == null) {
continue;
}
}
}
if (decl == null) {
decl = method;
continue;
} else if (decl != method && // handle inner classes
decl.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) == method.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class)) {
violation = false;
// Optimization
break;
}
}
if (violation && !usages.isEmpty()) {
addViolation(data, node, new Object[] { declaration.getImage() });
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator in project pmd by pmd.
the class StatementAndBraceFinderTest method testVariableDeclaratorParentChildLinks.
@Test
public void testVariableDeclaratorParentChildLinks() {
ASTVariableDeclarator vd = getOrderedNodes(ASTVariableDeclarator.class, TEST2).get(0);
ASTMethodDeclaration vdParent = (ASTMethodDeclaration) vd.getDataFlowNode().getParents().get(0).getNode();
assertEquals(vd, vdParent.getDataFlowNode().getChildren().get(0).getNode());
assertEquals(vdParent, vd.getDataFlowNode().getParents().get(0).getNode());
}
Aggregations