01: package spoon.vsuite.findbugs.dm;
02:
03: import spoon.processing.AbstractProcessor;
04: import spoon.processing.Property;
05: import spoon.processing.Severity;
06: import spoon.reflect.Factory;
07: import spoon.reflect.code.CtNewClass;
08: import spoon.reflect.reference.CtExecutableReference;
09: import spoon.reflect.reference.CtTypeReference;
10:
11: /**
12: * Dm: Method invokes dubious Boolean constructor; use Boolean.valueOf(...)
13: * instead (DM_BOOLEAN_CTOR) Creating new instances of java.lang.Boolean wastes
14: * memory, since Boolean objects are immutable and there are only two useful
15: * values of this type. Use the Boolean.valueOf() method to create Boolean
16: * objects instead.
17: *
18: * @author Nicolas Petitprez
19: */
20: public class BooleanCtor extends AbstractProcessor<CtNewClass<?>> {
21: @Property
22: static Severity level = Severity.WARNING;
23:
24: static CtExecutableReference<?> ref;
25:
26: public static CtExecutableReference<?> getRef(Factory factory) {
27: if (ref == null) {
28: ref = factory.Constructor().createReference(
29: factory.Type().createReference(Boolean.class),
30: factory.Type().createReference(Boolean.class),
31: "<init>",
32: new CtTypeReference[] { factory.Type()
33: .createReference(boolean.class) });
34: }
35: return ref;
36: }
37:
38: static CtExecutableReference<?> ref2;
39:
40: public static CtExecutableReference<?> getRef2(Factory factory) {
41: if (ref2 == null) {
42: ref2 = factory.Constructor().createReference(
43: factory.Type().createReference(Boolean.class),
44: factory.Type().createReference(Boolean.class),
45: "<init>",
46: new CtTypeReference[] { factory.Type()
47: .createReference(String.class) });
48: }
49: return ref2;
50: }
51:
52: public void process(CtNewClass<?> element) {
53: // getFactory().getEnvironment().reportMessage(
54: // element.getExecutable().toString());
55:
56: if (element.getExecutable().equals(getRef(getFactory()))
57: || element.getExecutable()
58: .equals(getRef2(getFactory()))) {
59: getFactory()
60: .getEnvironment()
61: .report(this , level, element,
62: "Method invokes Boolean constructor; use Boolean.valueOf(...) instead ");
63: }
64: }
65: }
|