use of net.sourceforge.pmd.lang.java.ast.ASTLiteral in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method processAdditive.
private int processAdditive(Node sn) {
ASTAdditiveExpression additive = sn.getFirstDescendantOfType(ASTAdditiveExpression.class);
if (additive == null) {
return 0;
}
int anticipatedLength = 0;
for (int ix = 0; ix < additive.jjtGetNumChildren(); ix++) {
Node childNode = additive.jjtGetChild(ix);
ASTLiteral literal = childNode.getFirstDescendantOfType(ASTLiteral.class);
if (literal != null && literal.getImage() != null) {
anticipatedLength += literal.getImage().length() - 2;
}
}
return anticipatedLength;
}
use of net.sourceforge.pmd.lang.java.ast.ASTLiteral in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method getInitialLength.
private int getInitialLength(Node node) {
Node block = node.getFirstParentOfType(ASTBlockStatement.class);
if (block == null) {
block = node.getFirstParentOfType(ASTFieldDeclaration.class);
if (block == null) {
block = node.getFirstParentOfType(ASTFormalParameter.class);
}
}
List<ASTLiteral> literals = block.findDescendantsOfType(ASTLiteral.class);
if (literals.size() == 1) {
ASTLiteral literal = literals.get(0);
String str = literal.getImage();
if (str != null && isStringOrCharLiteral(literal)) {
// take off the quotes
return str.length() - 2;
}
}
return 0;
}
use of net.sourceforge.pmd.lang.java.ast.ASTLiteral in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method processNode.
private int processNode(Node sn) {
int anticipatedLength = 0;
if (sn != null) {
ASTPrimaryPrefix xn = sn.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (xn != null) {
if (xn.jjtGetNumChildren() != 0 && xn.jjtGetChild(0) instanceof ASTLiteral) {
ASTLiteral literal = (ASTLiteral) xn.jjtGetChild(0);
String str = xn.jjtGetChild(0).getImage();
if (str != null) {
if (literal.isStringLiteral()) {
anticipatedLength += str.length() - 2;
} else if (literal.isCharLiteral()) {
anticipatedLength += 1;
} else if (literal.isIntLiteral()) {
// but only if we are not inside a cast expression
Node parentNode = literal.jjtGetParent().jjtGetParent().jjtGetParent();
if (parentNode instanceof ASTCastExpression && ((ASTCastExpression) parentNode).getType() == char.class) {
anticipatedLength += 1;
} else {
// any number, regardless of the base will be converted to base 10
// e.g. 0xdeadbeef -> will be converted to a
// base 10 integer string: 3735928559
anticipatedLength += String.valueOf(literal.getValueAsLong()).length();
}
} else {
anticipatedLength += str.length();
}
}
}
}
}
return anticipatedLength;
}
use of net.sourceforge.pmd.lang.java.ast.ASTLiteral 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.ASTLiteral in project pmd by pmd.
the class ConstructorCallsOverridableMethodRule method getArgumentTypes.
private static List<String> getArgumentTypes(ASTArguments args) {
List<String> argumentTypes = new ArrayList<>();
ASTArgumentList argumentList = args.getFirstChildOfType(ASTArgumentList.class);
if (argumentList != null) {
for (int a = 0; a < argumentList.jjtGetNumChildren(); a++) {
Node expression = argumentList.jjtGetChild(a);
ASTPrimaryPrefix arg = expression.getFirstDescendantOfType(ASTPrimaryPrefix.class);
String type = "<unknown>";
if (arg != null && arg.jjtGetNumChildren() > 0) {
if (arg.jjtGetChild(0) instanceof ASTLiteral) {
ASTLiteral lit = (ASTLiteral) arg.jjtGetChild(0);
if (lit.isCharLiteral()) {
type = "char";
} else if (lit.isFloatLiteral()) {
type = "float";
} else if (lit.isIntLiteral()) {
type = "int";
} else if (lit.isStringLiteral()) {
type = "String";
} else if (lit.jjtGetNumChildren() > 0 && lit.jjtGetChild(0) instanceof ASTBooleanLiteral) {
type = "boolean";
} else if (lit.isDoubleLiteral()) {
type = "double";
} else if (lit.isLongLiteral()) {
type = "long";
}
} else if (arg.jjtGetChild(0) instanceof ASTName) {
// ASTName n = (ASTName)arg.jjtGetChild(0);
type = "ref";
}
}
argumentTypes.add(type);
}
}
return argumentTypes;
}
Aggregations