View Javadoc

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