use of net.sourceforge.pmd.lang.java.ast.ASTStatementExpression 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.ASTStatementExpression in project pmd by pmd.
the class StatementAndBraceFinderTest method testStatementExpressionParentChildLinks.
@Test
public void testStatementExpressionParentChildLinks() {
ASTStatementExpression se = getOrderedNodes(ASTStatementExpression.class, TEST1).get(0);
ASTMethodDeclaration seParent = (ASTMethodDeclaration) se.getDataFlowNode().getParents().get(0).getNode();
assertEquals(se, seParent.getDataFlowNode().getChildren().get(0).getNode());
assertEquals(seParent, se.getDataFlowNode().getParents().get(0).getNode());
}
Aggregations