01: package spoon.vsuite.common;
02:
03: import java.math.BigDecimal;
04: import java.util.ArrayList;
05: import java.util.List;
06:
07: import spoon.processing.AbstractProcessor;
08: import spoon.processing.Property;
09: import spoon.processing.Severity;
10: import spoon.reflect.code.CtAbstractInvocation;
11: import spoon.reflect.code.CtAssignment;
12: import spoon.reflect.code.CtInvocation;
13: import spoon.reflect.reference.CtExecutableReference;
14: import spoon.reflect.reference.CtTypeReference;
15:
16: public class NoSideEffectInvocationProcessor extends
17: AbstractProcessor<CtInvocation<?>> {
18:
19: @Property
20: Severity level = Severity.WARNING;
21:
22: // @Property("Specifies the methods wich are stateless or factory so that
23: // they have no impact on the target object and that the result should be
24: // used in some way")
25: public static List<CtExecutableReference<?>> statelessMethods = new ArrayList<CtExecutableReference<?>>();
26:
27: @Override
28: public void init() {
29: CtTypeReference<BigDecimal> bigDecimal = getFactory().Type()
30: .createReference(BigDecimal.class);
31: statelessMethods.add(getFactory().Executable().createReference(
32: bigDecimal, bigDecimal, "add", bigDecimal));
33: }
34:
35: public void process(CtInvocation<?> invocation) {
36: if (!statelessMethods.contains(invocation.getExecutable()))
37: return;
38: if (invocation.getParent() instanceof CtAbstractInvocation)
39: return;
40: if (invocation.getParent() instanceof CtAssignment)
41: return;
42: getFactory().getEnvironment().report(
43: this ,
44: level,
45: invocation,
46: "Code has no effect since '"
47: + invocation.getExecutable().getDeclaringType()
48: .getSimpleName() + "."
49: + invocation.getExecutable().getSimpleName()
50: + "' is stateless");
51: }
52:
53: }
|