use of net.sourceforge.pmd.lang.java.ast.ASTCastExpression 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.ASTCastExpression 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.ASTCastExpression in project pmd by pmd.
the class UnnecessaryCastRule method process.
private Object process(Node node, Object data) {
ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (cit == null || !implClassNames.contains(cit.getImage())) {
return data;
}
cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (cit == null) {
return data;
}
ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
List<NameOccurrence> usages = decl.getUsages();
for (NameOccurrence no : usages) {
ASTName name = (ASTName) no.getLocation();
Node n = name.jjtGetParent().jjtGetParent().jjtGetParent();
if (n instanceof ASTCastExpression) {
addViolation(data, n);
}
}
return null;
}
use of net.sourceforge.pmd.lang.java.ast.ASTCastExpression in project pmd by pmd.
the class PreserveStackTraceRule method visit.
@Override
public Object visit(ASTCatchStatement catchStmt, Object data) {
String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
// Inspect all the throw stmt inside the catch stmt
List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
for (ASTThrowStatement throwStatement : lstThrowStatements) {
Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
if (n instanceof ASTCastExpression) {
ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
RuleContext ctx = (RuleContext) data;
addViolation(ctx, throwStatement);
}
continue;
}
// Retrieve all argument for the throw exception (to see if the
// original exception is preserved)
ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
Node parent = args.jjtGetParent().jjtGetParent();
if (parent instanceof ASTAllocationExpression) {
// maybe it is used inside a anonymous class
ck(data, target, throwStatement, parent);
} else {
// Check all arguments used in the throw statement
ck(data, target, throwStatement, throwStatement);
}
} else {
Node child = throwStatement.jjtGetChild(0);
while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
child = child.jjtGetChild(0);
}
if (child != null) {
if (child instanceof ASTName && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
List<NameOccurrence> occurrences = entry.getValue();
if (decl.getImage().equals(child.getImage())) {
if (!isInitCauseCalled(target, occurrences)) {
// Check how the variable is initialized
ASTVariableInitializer initializer = decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null) {
args = initializer.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
// constructor with args?
ck(data, target, throwStatement, args);
} else if (!isFillInStackTraceCalled(target, initializer)) {
addViolation(data, throwStatement);
}
}
}
}
}
} else if (child instanceof ASTClassOrInterfaceType) {
addViolation(data, throwStatement);
}
}
}
}
return super.visit(catchStmt, data);
}
Aggregations