use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class ConsecutiveLiteralAppendsRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!isStringBuffer(node)) {
return data;
}
threshold = getProperty(THRESHOLD_DESCRIPTOR);
int concurrentCount = checkConstructor(node, data);
if (hasInitializer(node)) {
concurrentCount += checkInitializerExpressions(node);
}
Node lastBlock = getFirstParentBlock(node);
Node currentBlock = lastBlock;
Map<VariableNameDeclaration, List<NameOccurrence>> decls = node.getScope().getDeclarations(VariableNameDeclaration.class);
Node rootNode = null;
// only want the constructor flagged if it's really containing strings
if (concurrentCount >= 1) {
rootNode = node;
}
for (List<NameOccurrence> decl : decls.values()) {
for (NameOccurrence no : decl) {
JavaNameOccurrence jno = (JavaNameOccurrence) no;
Node n = jno.getLocation();
// variable
if (!node.getImage().equals(jno.getImage())) {
continue;
}
currentBlock = getFirstParentBlock(n);
if (!InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "append")) {
if (!jno.isPartOfQualifiedName()) {
checkForViolation(rootNode, data, concurrentCount);
concurrentCount = 0;
}
continue;
}
ASTPrimaryExpression s = n.getFirstParentOfType(ASTPrimaryExpression.class);
int numChildren = s.jjtGetNumChildren();
for (int jx = 0; jx < numChildren; jx++) {
Node sn = s.jjtGetChild(jx);
if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
continue;
}
// see if it changed blocks
if (currentBlock != null && lastBlock != null && !currentBlock.equals(lastBlock) || currentBlock == null ^ lastBlock == null) {
checkForViolation(rootNode, data, concurrentCount);
concurrentCount = 0;
}
// here
if (concurrentCount == 0) {
rootNode = sn;
}
if (isAdditive(sn)) {
concurrentCount = processAdditive(data, concurrentCount, sn, rootNode);
if (concurrentCount != 0) {
rootNode = sn;
}
} else if (!isAppendingStringLiteral(sn)) {
checkForViolation(rootNode, data, concurrentCount);
concurrentCount = 0;
} else {
concurrentCount++;
}
lastBlock = currentBlock;
}
}
}
checkForViolation(rootNode, data, concurrentCount);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class ClassTypeResolverJava8Test method testThisExpression.
@Test
public void testThisExpression() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass18(ThisExpression.class);
List<ASTPrimaryExpression> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), ASTPrimaryExpression.class);
List<ASTPrimaryPrefix> prefixes = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), ASTPrimaryPrefix.class);
int index = 0;
assertEquals(ThisExpression.class, expressions.get(index).getType());
assertEquals(ThisExpression.class, prefixes.get(index++).getType());
assertEquals(ThisExpression.PrimaryThisInterface.class, expressions.get(index).getType());
assertEquals(ThisExpression.PrimaryThisInterface.class, prefixes.get(index++).getType());
// Make sure we got them all
assertEquals("All expressions not tested", index, expressions.size());
assertEquals("All expressions not tested", index, prefixes.size());
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression 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.ASTPrimaryExpression in project pmd by pmd.
the class UselessOverridingMethodRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
// them.
if (node.isAbstract() || node.isFinal() || node.isNative() || node.isSynchronized()) {
return super.visit(node, data);
}
// implement them anyway ( see bug 1522517)
if (CLONE.equals(node.getMethodName()) && node.isPublic() && !this.hasArguments(node) && this.isMethodType(node, OBJECT) && this.isMethodThrowingType(node, exceptions)) {
return super.visit(node, data);
}
ASTBlock block = node.getBlock();
if (block == null) {
return super.visit(node, data);
}
// Only process functions with one BlockStatement
if (block.jjtGetNumChildren() != 1 || block.findDescendantsOfType(ASTStatement.class).size() != 1) {
return super.visit(node, data);
}
Node statement = block.jjtGetChild(0).jjtGetChild(0);
if (statement.jjtGetChild(0).jjtGetNumChildren() == 0) {
// skips empty return statements
return data;
}
Node statementGrandChild = statement.jjtGetChild(0).jjtGetChild(0);
ASTPrimaryExpression primaryExpression;
if (statementGrandChild instanceof ASTPrimaryExpression) {
primaryExpression = (ASTPrimaryExpression) statementGrandChild;
} else {
List<ASTPrimaryExpression> primaryExpressions = findFirstDegreeChildrenOfType(statementGrandChild, ASTPrimaryExpression.class);
if (primaryExpressions.size() != 1) {
return super.visit(node, data);
}
primaryExpression = primaryExpressions.get(0);
}
ASTPrimaryPrefix primaryPrefix = findFirstDegreeChildrenOfType(primaryExpression, ASTPrimaryPrefix.class).get(0);
if (!primaryPrefix.usesSuperModifier()) {
return super.visit(node, data);
}
List<ASTPrimarySuffix> primarySuffixList = findFirstDegreeChildrenOfType(primaryExpression, ASTPrimarySuffix.class);
if (primarySuffixList.size() != 2) {
// extra method call on result of super method
return super.visit(node, data);
}
ASTMethodDeclarator methodDeclarator = findFirstDegreeChildrenOfType(node, ASTMethodDeclarator.class).get(0);
ASTPrimarySuffix primarySuffix = primarySuffixList.get(0);
if (!primarySuffix.hasImageEqualTo(methodDeclarator.getImage())) {
return super.visit(node, data);
}
// Process arguments
primarySuffix = primarySuffixList.get(1);
ASTArguments arguments = (ASTArguments) primarySuffix.jjtGetChild(0);
ASTFormalParameters formalParameters = (ASTFormalParameters) methodDeclarator.jjtGetChild(0);
if (formalParameters.jjtGetNumChildren() != arguments.jjtGetNumChildren()) {
return super.visit(node, data);
}
if (!ignoreAnnotations) {
ASTClassOrInterfaceBodyDeclaration parent = (ASTClassOrInterfaceBodyDeclaration) node.jjtGetParent();
for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
Node n = parent.jjtGetChild(i);
if (n instanceof ASTAnnotation) {
if (n.jjtGetChild(0) instanceof ASTMarkerAnnotation) {
// @Override is ignored
if ("Override".equals(((ASTName) n.jjtGetChild(0).jjtGetChild(0)).getImage())) {
continue;
}
}
return super.visit(node, data);
}
}
}
if (arguments.jjtGetNumChildren() == 0) {
addViolation(data, node, getMessage());
} else {
ASTArgumentList argumentList = (ASTArgumentList) arguments.jjtGetChild(0);
for (int i = 0; i < argumentList.jjtGetNumChildren(); i++) {
Node expressionChild = argumentList.jjtGetChild(i).jjtGetChild(0);
if (!(expressionChild instanceof ASTPrimaryExpression) || expressionChild.jjtGetNumChildren() != 1) {
// The arguments are not simply passed through
return super.visit(node, data);
}
ASTPrimaryExpression argumentPrimaryExpression = (ASTPrimaryExpression) expressionChild;
ASTPrimaryPrefix argumentPrimaryPrefix = (ASTPrimaryPrefix) argumentPrimaryExpression.jjtGetChild(0);
if (argumentPrimaryPrefix.jjtGetNumChildren() == 0) {
// The arguments are not simply passed through (using "this" for instance)
return super.visit(node, data);
}
Node argumentPrimaryPrefixChild = argumentPrimaryPrefix.jjtGetChild(0);
if (!(argumentPrimaryPrefixChild instanceof ASTName)) {
// The arguments are not simply passed through
return super.visit(node, data);
}
if (formalParameters.jjtGetNumChildren() < i + 1) {
// different number of args
return super.visit(node, data);
}
ASTName argumentName = (ASTName) argumentPrimaryPrefixChild;
ASTFormalParameter formalParameter = (ASTFormalParameter) formalParameters.jjtGetChild(i);
ASTVariableDeclaratorId variableId = findFirstDegreeChildrenOfType(formalParameter, ASTVariableDeclaratorId.class).get(0);
if (!argumentName.hasImageEqualTo(variableId.getImage())) {
// The arguments are not simply passed through
return super.visit(node, data);
}
}
// All arguments are passed through directly
addViolation(data, node, getMessage());
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class BrokenNullCheckRule method checkForViolations.
private void checkForViolations(ASTIfStatement node, Object data, Node conditionalExpression) {
ASTEqualityExpression equalityExpression = conditionalExpression.getFirstChildOfType(ASTEqualityExpression.class);
if (equalityExpression == null) {
return;
}
if (conditionalExpression instanceof ASTConditionalAndExpression && !"==".equals(equalityExpression.getImage())) {
return;
}
if (conditionalExpression instanceof ASTConditionalOrExpression && !"!=".equals(equalityExpression.getImage())) {
return;
}
ASTNullLiteral nullLiteral = equalityExpression.getFirstDescendantOfType(ASTNullLiteral.class);
if (nullLiteral == null) {
// No null check
return;
}
// because things get too complex
if (conditionalExpression.hasDescendantOfType(ASTAssignmentOperator.class)) {
return;
}
// Find the expression used in the null compare
ASTPrimaryExpression nullCompareExpression = findNullCompareExpression(equalityExpression);
if (nullCompareExpression == null) {
// No good null check
return;
}
// Now we find the expression to compare to and do the comparison
for (int i = 0; i < conditionalExpression.jjtGetNumChildren(); i++) {
Node conditionalSubnode = conditionalExpression.jjtGetChild(i);
// We skip the null compare branch
ASTEqualityExpression nullEqualityExpression = nullLiteral.getFirstParentOfType(ASTEqualityExpression.class);
if (conditionalSubnode.equals(nullEqualityExpression)) {
continue;
}
ASTPrimaryExpression conditionalPrimaryExpression;
if (conditionalSubnode instanceof ASTPrimaryExpression) {
conditionalPrimaryExpression = (ASTPrimaryExpression) conditionalSubnode;
} else {
// The ASTPrimaryExpression is hidden (in a negation, braces or
// EqualityExpression)
conditionalPrimaryExpression = conditionalSubnode.getFirstDescendantOfType(ASTPrimaryExpression.class);
}
if (primaryExpressionsAreEqual(nullCompareExpression, conditionalPrimaryExpression)) {
// We have a match
addViolation(data, node);
}
}
}
Aggregations