01: package spoon.vsuite.common;
02:
03: import spoon.processing.AbstractProcessor;
04: import spoon.processing.Property;
05: import spoon.processing.Severity;
06: import spoon.reflect.code.CtBlock;
07: import spoon.reflect.code.CtReturn;
08: import spoon.reflect.code.CtThrow;
09: import spoon.reflect.code.CtTry;
10: import spoon.reflect.visitor.Query;
11: import spoon.reflect.visitor.filter.TypeFilter;
12:
13: /**
14: * Report warnings when wrong finally blocks are found (using returns or
15: * throws).
16: */
17: public class BadFinallyProcessor extends AbstractProcessor<CtTry> {
18:
19: @Property
20: Severity level = Severity.WARNING;
21:
22: public void process(CtTry tryElement) {
23: CtBlock<?> finallyBlock = tryElement.getFinalizer();
24: for (CtReturn<?> r : Query.getElements(finallyBlock,
25: new TypeFilter<CtReturn<?>>(CtReturn.class))) {
26: getEnvironment().report(this , level, r,
27: "return is discouraged in finally blocks");
28: }
29: for (CtThrow r : Query.getElements(finallyBlock,
30: new TypeFilter<CtThrow>(CtThrow.class))) {
31: getEnvironment().report(this , level, r,
32: "Throw is discouraged in finally blocks");
33: }
34: }
35:
36: }
|