01: package net.sourceforge.pmd.rules.basic;
02:
03: import java.math.BigDecimal;
04: import java.math.BigInteger;
05:
06: import net.sourceforge.pmd.AbstractRule;
07: import net.sourceforge.pmd.RuleContext;
08: import net.sourceforge.pmd.SourceType;
09: import net.sourceforge.pmd.ast.ASTAllocationExpression;
10: import net.sourceforge.pmd.ast.ASTArguments;
11: import net.sourceforge.pmd.ast.ASTArrayDimsAndInits;
12: import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
13: import net.sourceforge.pmd.ast.ASTLiteral;
14: import net.sourceforge.pmd.ast.Node;
15: import net.sourceforge.pmd.typeresolution.TypeHelper;
16:
17: public class BigIntegerInstantiation extends AbstractRule {
18:
19: public Object visit(ASTAllocationExpression node, Object data) {
20: Node type = node.jjtGetChild(0);
21:
22: if (!(type instanceof ASTClassOrInterfaceType)) {
23: return super .visit(node, data);
24: }
25:
26: boolean jdk15 = ((RuleContext) data).getSourceType().compareTo(
27: SourceType.JAVA_15) >= 0;
28: if ((TypeHelper.isA((ASTClassOrInterfaceType) type,
29: BigInteger.class) || (jdk15 && TypeHelper.isA(
30: (ASTClassOrInterfaceType) type, BigDecimal.class)))
31: && (node
32: .getFirstChildOfType(ASTArrayDimsAndInits.class) == null)) {
33: ASTArguments args = node
34: .getFirstChildOfType(ASTArguments.class);
35: if (args.getArgumentCount() == 1) {
36: ASTLiteral literal = node
37: .getFirstChildOfType(ASTLiteral.class);
38: if (literal == null
39: || literal.jjtGetParent().jjtGetParent()
40: .jjtGetParent().jjtGetParent()
41: .jjtGetParent() != args) {
42: return super .visit(node, data);
43: }
44:
45: String img = literal.getImage();
46: if ((img.length() > 2 && img.charAt(0) == '"')) {
47: img = img.substring(1, img.length() - 1);
48: }
49:
50: if ("0".equals(img) || "1".equals(img)
51: || (jdk15 && "10".equals(img))) {
52: addViolation(data, node);
53: return data;
54: }
55: }
56: }
57: return super.visit(node, data);
58: }
59:
60: }
|