use of net.sourceforge.pmd.lang.java.ast.ASTLiteral in project pmd by pmd.
the class BooleanInstantiationRule method visit.
@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
if (!customBoolean) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}
if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage()) || "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) {
ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
ASTPrimarySuffix suffix = parent.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (suffix == null) {
return super.visit(node, data);
}
ASTPrimaryPrefix prefix = suffix.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (prefix == null) {
return super.visit(node, data);
}
if (prefix.hasDescendantOfType(ASTBooleanLiteral.class)) {
super.addViolation(data, node);
return data;
}
ASTLiteral literal = prefix.getFirstDescendantOfType(ASTLiteral.class);
if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) {
super.addViolation(data, node);
return data;
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTLiteral in project pmd by pmd.
the class ConsecutiveLiteralAppendsRule method checkInitializerExpressions.
/**
* Determine if during the variable initializer calls to ".append" are done.
*
* @param node
* @return
*/
private int checkInitializerExpressions(ASTVariableDeclaratorId node) {
ASTVariableInitializer initializer = node.jjtGetParent().getFirstChildOfType(ASTVariableInitializer.class);
ASTPrimaryExpression primary = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
int result = 0;
boolean previousWasAppend = false;
for (int i = 0; i < primary.jjtGetNumChildren(); i++) {
Node child = primary.jjtGetChild(i);
if (child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTAllocationExpression) {
// skip the constructor call, that has already been checked
continue;
}
if (child instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) child;
if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) {
previousWasAppend = true;
} else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) {
previousWasAppend = false;
ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class);
if (literal != null && literal.isStringLiteral()) {
result++;
} else {
// checking the remainder of the initializer
break;
}
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTLiteral in project pmd by pmd.
the class ClassTypeResolverTest method testLiterals.
@Test
public void testLiterals() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(Literals.class);
List<ASTLiteral> literals = convertList(acu.findChildNodesWithXPath("//Literal"), ASTLiteral.class);
int index = 0;
// String s = "s";
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(String.class, literals.get(index++).getType());
// boolean boolean1 = false;
assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
assertEquals(Boolean.TYPE, literals.get(index++).getType());
// boolean boolean2 = true;
assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
assertEquals(Boolean.TYPE, literals.get(index++).getType());
// Object obj = null;
assertNull(literals.get(index).getFirstDescendantOfType(ASTNullLiteral.class).getType());
assertNull(literals.get(index++).getType());
// byte byte1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// byte byte2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// byte byte3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// short short1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// short short2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// short short3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char3 = 007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char4 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// int int1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// int int2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// int int3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// int int4 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// long long1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// long long2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// long long3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// long long4 = 0L;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Long.TYPE, literals.get(index++).getType());
// long long5 = 0x0Fl;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Long.TYPE, literals.get(index++).getType());
// long long6 = -007L;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Long.TYPE, literals.get(index++).getType());
// long long7 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// float float1 = 0.0f;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Float.TYPE, literals.get(index++).getType());
// float float2 = -10e+01f;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Float.TYPE, literals.get(index++).getType());
// float float3 = 0x08.08p3f;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Float.TYPE, literals.get(index++).getType());
// float float4 = 0xFF;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// float float5 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// double double1 = 0.0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Double.TYPE, literals.get(index++).getType());
// double double2 = -10e+01;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Double.TYPE, literals.get(index++).getType());
// double double3 = 0x08.08p3;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Double.TYPE, literals.get(index++).getType());
// double double4 = 0xFF;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// double double5 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// Make sure we got them all.
assertEquals("All literals not tested", index, literals.size());
}
Aggregations