01: package spoon.vsuite.findbugs.am;
02:
03: import spoon.processing.AbstractProblemFixer;
04: import spoon.processing.AbstractProcessor;
05: import spoon.processing.Property;
06: import spoon.processing.Severity;
07: import spoon.reflect.Changes;
08: import spoon.reflect.declaration.CtClass;
09: import spoon.reflect.declaration.CtMethod;
10: import spoon.reflect.reference.CtTypeReference;
11: import spoon.template.Substitution;
12: import spoon.vsuite.findbugs.template.CloneTemplate;
13:
14: /**
15: * CN: Class implements Cloneable but does not define or use clone method
16: * (CN_IDIOM). Class implements Cloneable but does not define or use the clone
17: * method.
18: *
19: * @author Nicolas Petitprez
20: */
21: public class Idiom extends AbstractProcessor<CtClass<?>> {
22:
23: public class Fix1 extends AbstractProblemFixer<CtClass<?>> {
24:
25: public String getDescription() {
26: return getLabel();
27: }
28:
29: public String getLabel() {
30: return "Add empty clone method";
31: }
32:
33: public Changes run(CtClass<?> arg0) {
34:
35: try {
36: CloneTemplate tmp = new CloneTemplate();
37: Substitution.insertAll(arg0, tmp);
38:
39: Changes lst = new Changes();
40: lst.getModified().add(arg0);
41: return lst;
42: } catch (Throwable e) {
43: e.printStackTrace();
44: }
45: return null;
46: }
47:
48: }
49:
50: @Property
51: Severity level = Severity.WARNING;
52:
53: public void process(CtClass<?> arg0) {
54: if (arg0.isSubtypeOf(getFactory().Type().createReference(
55: Cloneable.class))) {
56: CtMethod<?> m = arg0.getMethod("clone",
57: new CtTypeReference[0]);
58: if (m == null) {
59: getFactory()
60: .getEnvironment()
61: .report(
62: this ,
63: level,
64: arg0,
65: "Class implements Cloneable but does not define or use clone method",
66: new Fix1());
67: }
68: }
69:
70: }
71: }
|