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.CtInvocation;
08: import spoon.reflect.reference.CtExecutableReference;
09: import spoon.reflect.reference.CtTypeReference;
10:
11: /**
12: * Dm: Method invokes dubious String.toUpperCase() or String.toLowerCase; use
13: * the Locale parameterized version instead (DM_CONVERT_CASE) A String is being
14: * converted to upper or lowercase, using the platform's default encoding. This
15: * may result in improper conversions when used with international characters.
16: * Use the String.toUpperCase( Locale l ) String.toLowerCase( Locale l )
17: * versions instead.
18: *
19: * @author Nicolas Petitprez
20: */
21: public class ConvertCase extends AbstractProcessor<CtInvocation<?>> {
22:
23: @Property
24: static Severity level = Severity.WARNING;
25:
26: static CtExecutableReference<?> ref;
27:
28: public static CtExecutableReference<?> getRef(Factory factory) {
29: if (ref == null) {
30: ref = factory.Constructor().createReference(
31: factory.Type().createReference(String.class),
32: factory.Type().createReference(String.class),
33: "toLowerCase", new CtTypeReference[] {});
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(String.class),
44: factory.Type().createReference(String.class),
45: "toUpperCase", new CtTypeReference[] {});
46: }
47: return ref2;
48: }
49:
50: public void process(CtInvocation<?> element) {
51: if (element.getExecutable().equals(getRef(getFactory()))
52: || element.getExecutable()
53: .equals(getRef2(getFactory()))) {
54: getFactory()
55: .getEnvironment()
56: .report(
57: this ,
58: level,
59: element,
60: "Method invokes dubious "
61: + element.getExecutable()
62: .getSimpleName().toString()
63: + "; use the Locale parameterized version instead");
64: }
65:
66: }
67: }
|