01: package spoon.vsuite.findbugs.dmi;
02:
03: import spoon.processing.AbstractProcessor;
04: import spoon.processing.ProblemFixer;
05: import spoon.processing.Property;
06: import spoon.processing.Severity;
07: import spoon.reflect.Changes;
08: import spoon.reflect.code.CtExpression;
09: import spoon.reflect.code.CtInvocation;
10: import spoon.reflect.code.CtLiteral;
11: import spoon.reflect.reference.CtExecutableReference;
12: import spoon.reflect.reference.CtTypeReference;
13:
14: /**
15: * DMI: Invocation of substring(0), which returns the original value
16: * (DMI_USELESS_SUBSTRING) This code invokes substring(0) on a String, which
17: * returns the original value.
18: *
19: * @author Nicolas Petitprez
20: */
21: public class UselessSubstring extends
22: AbstractProcessor<CtInvocation<?>> implements
23: ProblemFixer<CtExpression<?>> {
24:
25: @Property
26: static Severity level = Severity.WARNING;
27:
28: @SuppressWarnings("unchecked")
29: public void process(CtInvocation<?> arg0) {
30:
31: CtTypeReference stringref = getFactory().Type()
32: .createReference(String.class);
33: CtExecutableReference exeref = getFactory().Executable()
34: .createReference(
35: stringref,
36: stringref,
37: "substring",
38: new CtTypeReference[] { getFactory().Type()
39: .createReference(int.class) });
40:
41: if (arg0.getExecutable().equals(exeref)) {
42:
43: CtExpression e = arg0.getArguments().get(0);
44: e = getFactory().Eval().createPartialEvaluator().evaluate(
45: arg0, e);
46:
47: if (e instanceof CtLiteral
48: && ((CtLiteral) e).getValue() instanceof Integer
49: && ((Integer) ((CtLiteral) e).getValue())
50: .intValue() == 0) {
51: getFactory()
52: .getEnvironment()
53: .report(
54: this ,
55: level,
56: arg0,
57: "This code invokes substring(0) on a String, which returns the original value.",
58: this );
59: }
60: }
61: }
62:
63: public String getDescription() {
64: return getLabel();
65: }
66:
67: public String getLabel() {
68: return "remove useless substring";
69: }
70:
71: public Changes run(CtExpression<?> arg0) {
72: CtExpression<?> target = ((CtInvocation<?>) arg0).getTarget();
73: arg0.replace(target);
74:
75: Changes lst = new Changes();
76: lst.getModified().add(target);
77: return lst;
78: }
79: }
|