コード例 #1
0
 @Override
 public void afterCheckProject(@NonNull Context context) {
   if (mAlwaysFields != null && !mHasIfRoomRefs) {
     for (Location location : mAlwaysFields) {
       context.report(
           ISSUE,
           location,
           "Prefer \"`SHOW_AS_ACTION_IF_ROOM`\" instead of \"`SHOW_AS_ACTION_ALWAYS`\"");
     }
   }
 }
コード例 #2
0
  @Override
  public void afterCheckFile(@NonNull Context context) {
    if (mIgnoreFile) {
      mFileAttributes = null;
      return;
    }
    if (mFileAttributes != null) {
      assert context instanceof XmlContext; // mFileAttributes is only set in XML files

      List<Attr> always = new ArrayList<Attr>();
      List<Attr> ifRoom = new ArrayList<Attr>();
      for (Attr attribute : mFileAttributes) {
        String value = attribute.getValue();
        if (value.equals(VALUE_ALWAYS)) {
          always.add(attribute);
        } else if (value.equals(VALUE_IF_ROOM)) {
          ifRoom.add(attribute);
        } else if (value.indexOf('|') != -1) {
          String[] flags = value.split("\\|"); // $NON-NLS-1$
          for (String flag : flags) {
            if (flag.equals(VALUE_ALWAYS)) {
              always.add(attribute);
              break;
            } else if (flag.equals(VALUE_IF_ROOM)) {
              ifRoom.add(attribute);
              break;
            }
          }
        }
      }

      if (!always.isEmpty() && mFileAttributes.size() > 1) {
        // Complain if you're using more than one "always", or if you're
        // using "always" and aren't using "ifRoom" at all (and provided you
        // have more than a single item)
        if (always.size() > 2 || ifRoom.isEmpty()) {
          XmlContext xmlContext = (XmlContext) context;
          Location location = null;
          for (int i = always.size() - 1; i >= 0; i--) {
            Location next = location;
            location = xmlContext.getLocation(always.get(i));
            if (next != null) {
              location.setSecondary(next);
            }
          }
          if (location != null) {
            context.report(ISSUE, location, "Prefer \"`ifRoom`\" instead of \"`always`\"");
          }
        }
      }
    }
  }