01: package spoon.vsuite.common;
02:
03: import java.util.Map;
04:
05: import spoon.processing.AbstractProblemFixer;
06: import spoon.processing.AbstractProcessor;
07: import spoon.processing.Property;
08: import spoon.processing.Severity;
09: import spoon.reflect.Changes;
10: import spoon.reflect.code.CtCatch;
11: import spoon.reflect.code.CtStatement;
12: import spoon.reflect.cu.CompilationUnit;
13: import spoon.reflect.cu.SourceCodeFragment;
14: import spoon.reflect.declaration.CtClass;
15: import spoon.template.TemplateMatcher;
16: import spoon.vsuite.common.template.BadCatchTemplate;
17:
18: public class BadExceptionForwardingProcessor extends
19: AbstractProcessor<CtCatch> {
20:
21: @Property
22: Severity level = Severity.WARNING;
23:
24: public void process(CtCatch c) {
25: if (c.getBody().getStatements().size() == 0)
26: return;
27: CtClass<BadCatchTemplate> template = getFactory().Template()
28: .get(BadCatchTemplate.class);
29: TemplateMatcher m = new TemplateMatcher(template);
30: CtStatement last = c.getBody().getStatements().get(
31: c.getBody().getStatements().size() - 1);
32: if (m.match(last, BadCatchTemplate.getStatement(template))) {
33: getEnvironment().report(
34: this ,
35: level,
36: last,
37: "This exception forwarding is bad practice because the '"
38: + m.getMatches().get("_e_")
39: + "' stacktrace is discarded");
40: }
41: }
42:
43: public class Fix1 extends AbstractProblemFixer<CtStatement> {
44:
45: Map<Object, Object> matches;
46:
47: public Fix1(Map<Object, Object> matches) {
48: this .matches = matches;
49: }
50:
51: public String getDescription() {
52: return getLabel();
53: }
54:
55: public String getLabel() {
56: return "Wrap the exception";
57: }
58:
59: public Changes run(CtStatement st) {
60: try {
61: CompilationUnit cu = st.getPosition()
62: .getCompilationUnit();
63: cu.addSourceCodeFragment(new SourceCodeFragment(st
64: .getPosition().getSourceStart(), "throw new "
65: + ((Class<?>) matches
66: .get("_replacingThrowable_")).getName()
67: + "(" + matches.get("_e_") + ")", st
68: .getPosition().getSourceEnd()
69: - st.getPosition().getSourceStart()));
70:
71: //CloneTemplate tmp = new CloneTemplate();
72: // Substitution.insertAll(arg0, tmp);
73:
74: Changes lst = new Changes();
75: // lst.getModified().add(arg0);
76: return lst;
77: } catch (Throwable e) {
78: e.printStackTrace();
79: }
80: return null;
81: }
82:
83: }
84:
85: }
|