01: package spoon.vsuite.findbugs.bit;
02:
03: import spoon.processing.AbstractProcessor;
04: import spoon.processing.ProblemFixer;
05: import spoon.processing.Property;
06: import spoon.processing.Severity;
07: import spoon.reflect.Changes;
08: import spoon.reflect.code.CtBinaryOperator;
09: import spoon.reflect.code.CtExpression;
10: import spoon.reflect.code.CtLiteral;
11: import spoon.reflect.eval.PartialEvaluator;
12:
13: /**
14: * BIT: Incompatible bit masks (BIT_AND_ZZ). This method compares an expression
15: * of the form (a & 0) to 0, which will always compare equal. This may indicate
16: * a logic error or typo.
17: *
18: * @author Nicolas Petitprez
19: */
20: public class UnecessaryBinaryOperator extends
21: AbstractProcessor<CtBinaryOperator<?>> implements
22: ProblemFixer<CtExpression<?>> {
23: @Property
24: Severity level = Severity.WARNING;
25:
26: public void process(CtBinaryOperator<?> element) {
27: try {
28: PartialEvaluator eval = getFactory().Eval()
29: .createPartialEvaluator();
30: CtExpression<?> op = eval.evaluate(element.getParent(),
31: element);
32: if (op instanceof CtLiteral
33: && ((CtLiteral<?>) op).getValue() instanceof Boolean) {
34: getFactory().getEnvironment().report(
35: this ,
36: level,
37: element,
38: "This operator always return "
39: + ((CtLiteral<?>) op).getValue(), this );
40: }
41: } catch (RuntimeException e) {
42: // PartialEvaluator could throws exceptions
43: }
44:
45: }
46:
47: public String getDescription() {
48: return "Replace this operator by pre-evaluate one";
49: }
50:
51: public String getLabel() {
52: return "Replace operator";
53: }
54:
55: public Changes run(CtExpression<?> element) {
56: PartialEvaluator eval = getFactory().Eval()
57: .createPartialEvaluator();
58: CtExpression<?> op = eval
59: .evaluate(element.getParent(), element);
60:
61: element.replace(op);
62:
63: Changes lst = new Changes();
64: lst.getModified().add(op);
65: return lst;
66: }
67:
68: }
|