01: package spoon.vsuite.findbugs.ec;
02:
03: import spoon.processing.AbstractProcessor;
04: import spoon.processing.Property;
05: import spoon.processing.Severity;
06: import spoon.reflect.code.CtInvocation;
07: import spoon.reflect.reference.CtArrayTypeReference;
08: import spoon.reflect.reference.CtTypeReference;
09:
10: /**
11: * EC: equals() used to compare array and nonarray (EC_ARRAY_AND_NONARRAY) This
12: * method invokes the .equals(Object o) to compare an array and a reference that
13: * doesn't seem to be an array. If things being compared are of different types,
14: * they are guaranteed to be unequal and the comparison is almost certainly an
15: * error. Even if they are both arrays, the equals method on arrays only
16: * determines of the two arrays are the same object. To compare the contents of
17: * the arrays, use java.util.Arrays.equals(Object[], Object[]).
18: *
19: * @author Nicolas Petitprez
20: */
21: public class ArrayAndNonArray extends
22: AbstractProcessor<CtInvocation<?>> {
23:
24: @Property
25: static Severity level = Severity.WARNING;
26:
27: public void process(CtInvocation<?> arg0) {
28: if (arg0.getExecutable().getSimpleName().equals("equals")
29: && arg0.getArguments().size() == 1) {
30: CtTypeReference<?> ref1 = arg0.getTarget().getType();
31: CtTypeReference<?> ref2 = arg0.getArguments().get(0)
32: .getType();
33:
34: if ((ref1 instanceof CtArrayTypeReference)
35: ^ (ref2 instanceof CtArrayTypeReference)) {
36: getEnvironment().report(this , level, arg0,
37: "equals() used to compare array and nonarray");
38: }
39: }
40: }
41: }
|