01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules.design;
04:
05: import net.sourceforge.pmd.rules.AbstractInefficientZeroCheck;
06: import net.sourceforge.pmd.symboltable.NameOccurrence;
07: import net.sourceforge.pmd.util.CollectionUtil;
08:
09: /**
10: * Detect structures like "foo.size() == 0" and suggest replacing them with
11: * foo.isEmpty(). Will also find != 0 (replacable with !isEmpty()).
12: *
13: * @author Jason Bennett
14: */
15: public class UseCollectionIsEmpty extends AbstractInefficientZeroCheck {
16:
17: public boolean appliesToClassName(String name) {
18: return CollectionUtil.isCollectionType(name, true);
19: }
20:
21: /**
22: * Determine if we're dealing with .size method
23: *
24: * @param occ
25: * The name occurance
26: * @return true if it's .length, else false
27: */
28: public boolean isTargetMethod(NameOccurrence occ) {
29: if (occ.getNameForWhichThisIsAQualifier() != null) {
30: if (occ.getLocation().getImage().endsWith(".size")) {
31: return true;
32: }
33: }
34: return false;
35: }
36: }
|