use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class ClassTypeResolverTest method testUnaryLogicalOperators.
@Test
public void testUnaryLogicalOperators() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'unaryLogicalOperators']]//Expression"), ASTExpression.class);
int index = 0;
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
// Make sure we got them all.
assertEquals("All expressions not tested", index, expressions.size());
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class ClassTypeResolverTest method testBinaryStringPromotion.
@Test
public void testBinaryStringPromotion() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(Promotion.class);
List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryStringPromotion']]//Expression"), ASTExpression.class);
int index = 0;
assertEquals(String.class, expressions.get(index++).getType());
assertEquals(String.class, expressions.get(index++).getType());
assertEquals(String.class, expressions.get(index++).getType());
assertEquals(String.class, expressions.get(index++).getType());
assertEquals(String.class, expressions.get(index++).getType());
// Make sure we got them all.
assertEquals("All expressions not tested", index, expressions.size());
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression 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.ASTExpression 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.ast.ASTExpression in project pmd by pmd.
the class IdempotentOperationsRule method visit.
@Override
public Object visit(ASTStatementExpression node, Object data) {
if (node.jjtGetNumChildren() != 3 || !(node.jjtGetChild(0) instanceof ASTPrimaryExpression) || !(node.jjtGetChild(1) instanceof ASTAssignmentOperator) || ((ASTAssignmentOperator) node.jjtGetChild(1)).isCompound() || !(node.jjtGetChild(2) instanceof ASTExpression) || node.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0 || node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0) {
return super.visit(node, data);
}
Node lhs = node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
if (!(lhs instanceof ASTName)) {
return super.visit(node, data);
}
Node rhs = node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
if (!(rhs instanceof ASTName)) {
return super.visit(node, data);
}
if (!lhs.hasImageEqualTo(rhs.getImage())) {
return super.visit(node, data);
}
if (lhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
Node n = lhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArrayDereference()) {
return super.visit(node, data);
}
}
if (rhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
Node n = rhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArguments() || ((ASTPrimarySuffix) n).isArrayDereference()) {
return super.visit(node, data);
}
}
if (lhs.findDescendantsOfType(ASTPrimarySuffix.class).size() != rhs.findDescendantsOfType(ASTPrimarySuffix.class).size()) {
return super.visit(node, data);
}
List<ASTPrimarySuffix> lhsSuffixes = lhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
List<ASTPrimarySuffix> rhsSuffixes = rhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
if (lhsSuffixes.size() != rhsSuffixes.size()) {
return super.visit(node, data);
}
for (int i = 0; i < lhsSuffixes.size(); i++) {
ASTPrimarySuffix l = lhsSuffixes.get(i);
ASTPrimarySuffix r = rhsSuffixes.get(i);
if (!l.hasImageEqualTo(r.getImage())) {
return super.visit(node, data);
}
}
addViolation(data, node);
return super.visit(node, data);
}
Aggregations