01: package spoon.vsuite.common;
02:
03: import spoon.processing.AbstractManualProcessor;
04: import spoon.processing.Property;
05: import spoon.processing.Severity;
06: import spoon.reflect.code.CtLocalVariable;
07: import spoon.reflect.declaration.CtVariable;
08: import spoon.reflect.declaration.ModifierKind;
09:
10: /**
11: * Report warnings when unused variable are found.
12: */
13: public class UnusedVariableProcessor extends AbstractManualProcessor {
14:
15: @Property
16: Severity level = Severity.WARNING;
17:
18: @Property("do not report unused warnings for static final fields")
19: public static boolean ignoreStaticFinalVariables = false;
20:
21: @Property("do not report unused warnings for local variables (done by default in most IDEs)")
22: public static boolean ignoreLocalVariables = true;
23:
24: @Property("do not report unused warnings for private fields (done by default in most IDEs)")
25: public static boolean ignorePrivateVariables = false;
26:
27: public void process() {
28: for (CtVariable<?> v : VariableProcessor.getVariables()) {
29: if ((!VariableProcessor.getReadVariables().contains(v))
30: && (!ignore(v))) {
31: getFactory().getEnvironment().report(
32: this ,
33: level,
34: v,
35: "Variable '" + v.getSimpleName()
36: + "' is never read in the source code");
37: }
38: }
39: }
40:
41: private boolean ignore(CtVariable<?> v) {
42: return (v.hasModifier(ModifierKind.FINAL)
43: && v.hasModifier(ModifierKind.STATIC) && ignoreStaticFinalVariables)
44: || ((v instanceof CtLocalVariable) && ignoreLocalVariables)
45: || (v.hasModifier(ModifierKind.PRIVATE) && ignorePrivateVariables);
46:
47: }
48: }
|