01: package spoon.vsuite.common;
02:
03: import java.util.List;
04:
05: import spoon.processing.AbstractProcessor;
06: import spoon.processing.Property;
07: import spoon.processing.Severity;
08: import spoon.reflect.code.CtExpression;
09: import spoon.reflect.code.CtFieldAccess;
10: import spoon.reflect.code.CtInvocation;
11: import spoon.reflect.declaration.CtConstructor;
12: import spoon.reflect.visitor.Query;
13: import spoon.reflect.visitor.filter.TypeFilter;
14:
15: public class DangerousConstructorProcessor extends
16: AbstractProcessor<CtConstructor<?>> {
17:
18: @Property
19: Severity level = Severity.WARNING;
20:
21: public void process(CtConstructor<?> constructor) {
22: List<CtInvocation<?>> invocations = Query.getElements(
23: constructor.getBody(), new TypeFilter<CtInvocation<?>>(
24: CtInvocation.class));
25: for (CtInvocation<?> invocation : invocations) {
26: if (invocation.getExecutable().getSimpleName().equals(
27: "<init>"))
28: continue;
29: boolean reported = false;
30: for (CtExpression<?> e : invocation.getArguments()) {
31: if ((e instanceof CtFieldAccess)
32: && ((CtFieldAccess<?>) e).getVariable()
33: .getSimpleName().equals("this")) {
34: getFactory()
35: .getEnvironment()
36: .report(
37: this ,
38: level,
39: invocation,
40: "This invocation is potentially dangerous as you might pass a not fully initialized object");
41: reported = true;
42: break;
43: }
44: }
45: if (reported)
46: continue;
47: if (!invocation.getExecutable().isFinal()
48: && !invocation.getExecutable().isStatic()) {
49: if (invocation.getTarget() == null
50: || ((invocation.getTarget() instanceof CtFieldAccess) && ((CtFieldAccess<?>) invocation
51: .getTarget()).getVariable()
52: .getSimpleName().equals("this"))) {
53: getFactory()
54: .getEnvironment()
55: .report(
56: this ,
57: Severity.WARNING,
58: invocation,
59: "this invocation is potentially dangerous as it could apply to a not fully initialized object");
60: }
61: }
62: }
63: }
64:
65: }
|