com.android.tools.lint.detector.api.Location.setSecondary()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中com.android.tools.lint.detector.api.Location.setSecondary()方法的一些代码示例,展示了Location.setSecondary()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Location.setSecondary()方法的具体详情如下:
包路径:com.android.tools.lint.detector.api.Location
类名称:Location
方法名:setSecondary

Location.setSecondary介绍

[英]Sets a secondary location for this location.
[中]设置此位置的辅助位置。

代码示例

代码示例来源:origin: com.amazon.device.tools.lint/lint-checks

private void recordLocation(String resource, Location location) {
  Location oldLocation = mUnused.get(resource);
  if (oldLocation != null) {
    location.setSecondary(oldLocation);
  }
  mUnused.put(resource, location);
}

代码示例来源:origin: com.android.tools.lint/lint-checks

private static Location chainLocations(List<File> files) {
  // Chain locations together
  Collections.sort(files);
  Location location = null;
  for (File file : files) {
    Location linkedLocation = location;
    location = Location.create(file);
    location.setSecondary(linkedLocation);
  }
  return location;
}

代码示例来源:origin: com.amazon.device.tools.lint/lint-checks

private static Location chainLocations(List<File> files) {
  // Chain locations together
  Collections.sort(files);
  Location location = null;
  for (File file : files) {
    Location linkedLocation = location;
    location = Location.create(file);
    location.setSecondary(linkedLocation);
  }
  return location;
}

代码示例来源:origin: com.android.tools.lint/lint-checks

public void recordLocation(@NonNull Location location) {
  Location oldLocation = this.locations;
  if (oldLocation != null) {
    location.setSecondary(oldLocation);
  }
  this.locations = location;
}

代码示例来源:origin: com.android.tools.lint/lint-api

/**
 * Reverses the secondary location list initiated by the given location
 *
 * @param location the first location in the list
 * @return the first location in the reversed list
 */
public static Location reverse(@NonNull Location location) {
  Location next = location.getSecondary();
  location.setSecondary(null);
  while (next != null) {
    Location nextNext = next.getSecondary();
    next.setSecondary(location);
    location = next;
    next = nextNext;
  }
  return location;
}

代码示例来源:origin: com.amazon.device.tools.lint/lint-checks

@NonNull
private static Location chainLocations(@NonNull List<Location> locations) {
  assert !locations.isEmpty();
  // Sort locations by the file parent folders
  if (locations.size() > 1) {
    Collections.sort(locations, new Comparator<Location>() {
      @Override
      public int compare(Location location1, Location location2) {
        File file1 = location1.getFile();
        File file2 = location2.getFile();
        String folder1 = file1.getParentFile().getName();
        String folder2 = file2.getParentFile().getName();
        return folder1.compareTo(folder2);
      }
    });
    // Chain locations together
    Iterator<Location> iterator = locations.iterator();
    assert iterator.hasNext();
    Location prev = iterator.next();
    while (iterator.hasNext()) {
      Location next = iterator.next();
      prev.setSecondary(next);
      prev = next;
    }
  }
  return locations.get(0);
}

代码示例来源:origin: com.android.tools.lint/lint-checks

@NonNull
private static Location chainLocations(@NonNull List<Location> locations) {
  assert !locations.isEmpty();
  // Sort locations by the file parent folders
  if (locations.size() > 1) {
    Collections.sort(locations, (location1, location2) -> {
      File file1 = location1.getFile();
      File file2 = location2.getFile();
      String folder1 = file1.getParentFile().getName();
      String folder2 = file2.getParentFile().getName();
      return folder1.compareTo(folder2);
    });
    // Chain locations together
    Iterator<Location> iterator = locations.iterator();
    assert iterator.hasNext();
    Location prev = iterator.next();
    while (iterator.hasNext()) {
      Location next = iterator.next();
      prev.setSecondary(next);
      prev = next;
    }
  }
  return locations.get(0);
}

代码示例来源:origin: com.android.tools.lint.checks/lint_checks

private void reportError(Context context, String id, File first, File second, String includer,
    List<String> chain) {
  String msg = null;
  if (chain.size() > 2) { // < 2: it's a directly include & obvious
    StringBuilder sb = new StringBuilder();
    for (String layout : chain) {
      if (sb.length() > 0) {
        sb.append(" => ");
      }
      sb.append(layout);
    }
    msg = String.format(
      "Duplicate id %1$s, already defined in layout %2$s which is included in this layout (%3$s)",
      id, includer, sb.toString());
  } else {
    msg = String.format(
      "Duplicate id %1$s, already defined in layout %2$s which is included in this layout",
      id, includer);
  }
  Location location = new Location(first, null, null);
  if (second != null) {
    // Also record the secondary location
    location.setSecondary(new Location(second, null, null));
  }
  context.client.report(context, CROSS_LAYOUT, location, msg, null);
}

代码示例来源:origin: com.android.tools.lint.checks/lint_checks

File otherFile = fileMap.get(name);
Location location = new Location(otherFile, null, null);
location.setSecondary(new Location(file, null, null));
String otherName = otherFile.getParentFile().getName() + File.separator
    + otherFile.getName();

代码示例来源:origin: com.amazon.device.tools.lint/lint-checks

private static void checkUniqueNames(XmlContext context, Element parent) {
  List<Element> items = LintUtils.getChildren(parent);
  if (items.size() > 1) {
    Set<String> names = Sets.newHashSet();
    for (Element item : items) {
      Attr nameNode = item.getAttributeNode(ATTR_NAME);
      if (nameNode != null) {
        String name = nameNode.getValue();
        if (names.contains(name) && context.isEnabled(ISSUE)) {
          Location location = context.getLocation(nameNode);
          for (Element prevItem : items) {
           Attr attribute = item.getAttributeNode(ATTR_NAME);
           if (attribute != null && name.equals(attribute.getValue())) {
              assert prevItem != item;
              Location prev = context.getLocation(prevItem);
              prev.setMessage("Previously defined here");
              location.setSecondary(prev);
              break;
            }
          }
          String message = String.format(
              "`%1$s` has already been defined in this `<%2$s>`",
              name, parent.getTagName());
          context.report(ISSUE, nameNode, location, message);
        }
        names.add(name);
      }
    }
  }
}

代码示例来源:origin: com.android.tools.lint/lint-checks

private static void checkUniqueNames(XmlContext context, Element parent) {
  List<Element> items = LintUtils.getChildren(parent);
  if (items.size() > 1) {
    Set<String> names = Sets.newHashSet();
    for (Element item : items) {
      Attr nameNode = item.getAttributeNode(ATTR_NAME);
      if (nameNode != null) {
        String name = nameNode.getValue();
        if (names.contains(name) && context.isEnabled(ISSUE)) {
          Location location = context.getLocation(nameNode);
          for (Element prevItem : items) {
           Attr attribute = item.getAttributeNode(ATTR_NAME);
           if (attribute != null && name.equals(attribute.getValue())) {
              assert prevItem != item;
              Location prev = context.getLocation(prevItem);
              prev.setMessage("Previously defined here");
              location.setSecondary(prev);
              break;
            }
          }
          String message = String.format(
              "`%1$s` has already been defined in this `<%2$s>`",
              name, parent.getTagName());
          context.report(ISSUE, nameNode, location, message);
        }
        names.add(name);
      }
    }
  }
}

代码示例来源:origin: com.android.tools.lint/lint-checks

@Override
  public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    Node parentNode = element.getParentNode();
    if (parentNode != null && parentNode.getNodeType() == Node.ELEMENT_NODE) {
      Element parent = (Element)parentNode;
      Attr width = parent.getAttributeNodeNS(ANDROID_URI, ATTR_LAYOUT_WIDTH);
      Attr height = parent.getAttributeNodeNS(ANDROID_URI, ATTR_LAYOUT_HEIGHT);
      Attr attr = null;
      if (width != null && VALUE_WRAP_CONTENT.equals(width.getValue())) {
        attr = width;
      }
      if (height != null && VALUE_WRAP_CONTENT.equals(height.getValue())) {
        attr = height;
      }
      if (attr != null) {
        String message = String.format("Placing a `<WebView>` in a parent element that "
            + "uses a `wrap_content %1$s` can lead to subtle bugs; use `match_parent` "
            + "instead", attr.getLocalName());
        Location location = context.getLocation(element);
        Location secondary = context.getLocation(attr);
        secondary.setMessage("`wrap_content` here may not work well with WebView below");
        location.setSecondary(secondary);
        context.report(ISSUE, element, location, message);
      }
    }
  }
}

代码示例来源:origin: com.amazon.device.tools.lint/lint-checks

@Override
  public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    Node parentNode = element.getParentNode();
    if (parentNode != null && parentNode.getNodeType() == Node.ELEMENT_NODE) {
      Element parent = (Element)parentNode;
      Attr width = parent.getAttributeNodeNS(ANDROID_URI, ATTR_LAYOUT_WIDTH);
      Attr height = parent.getAttributeNodeNS(ANDROID_URI, ATTR_LAYOUT_HEIGHT);
      Attr attr = null;
      if (width != null && VALUE_WRAP_CONTENT.equals(width.getValue())) {
        attr = width;
      }
      if (height != null && VALUE_WRAP_CONTENT.equals(height.getValue())) {
        attr = height;
      }
      if (attr != null) {
        String message = String.format("Placing a `<WebView>` in a parent element that "
            + "uses a `wrap_content %1$s` can lead to subtle bugs; use `match_parent` "
            + "instead", attr.getLocalName());
        Location location = context.getLocation(element);
        Location secondary = context.getLocation(attr);
        secondary.setMessage("`wrap_content` here may not work well with WebView below");
        location.setSecondary(secondary);
        context.report(ISSUE, element, location, message);
      }
    }
  }
}

代码示例来源:origin: com.amazon.device.tools.lint/lint-checks

private static void reportMismatch(XmlContext context, Attr idNode, Node inputTypeNode,
    String message) {
  Location location;
  if (inputTypeNode != null) {
    location = context.getLocation(inputTypeNode);
    Location secondary = context.getLocation(idNode);
    secondary.setMessage("id defined here");
    location.setSecondary(secondary);
  } else {
    location = context.getLocation(idNode);
  }
  context.report(ISSUE, idNode.getOwnerElement(), location, message);
}

代码示例来源:origin: com.android.tools.lint/lint-checks

location.setClientData(element);
location.setMessage(occurrence.message);
location.setSecondary(occurrence.location);
occurrence.location = location;

代码示例来源:origin: com.android.tools.lint/lint-checks

private static void reportMismatch(XmlContext context, Attr idNode, Node inputTypeNode,
    String message) {
  Location location;
  if (inputTypeNode != null) {
    location = context.getLocation(inputTypeNode);
    Location secondary = context.getLocation(idNode);
    secondary.setMessage("id defined here");
    location.setSecondary(secondary);
  } else {
    location = context.getLocation(idNode);
  }
  context.report(ISSUE, idNode.getOwnerElement(), location, message);
}

代码示例来源:origin: com.android.tools.lint/lint-checks

private void reportIfFound(@NonNull XmlContext context, @NonNull  String qualifiers,
    @NonNull  String name, @NonNull  ResourceType type, @Nullable Node secondary) {
  Multimap<ResourceType, Location> typeMap = mManifestLocations.get(name);
  if (typeMap != null) {
    Collection<Location> locations = typeMap.get(type);
    if (locations != null) {
      for (Location location : locations) {
        String message = getErrorMessage(qualifiers);
        if (secondary != null) {
          Location secondaryLocation = context.getLocation(secondary);
          secondaryLocation.setSecondary(location.getSecondary());
          secondaryLocation.setMessage("This value will not be used");
          location.setSecondary(secondaryLocation);
        }
        context.report(ISSUE, location, message);
      }
    }
  }
}

代码示例来源:origin: com.android.tools.lint/lint-api

/**
 * Sets a secondary location with the given message and returns the current location
 * updated with the given secondary location.
 *
 * @param secondary       a secondary location associated with this location
 * @param message         a message to be set on the secondary location
 * @param selfExplanatory if true, the message is itself self-explanatory; see
 *                        {@link #isSelfExplanatory()}}
 * @return current location updated with the secondary location
 */
@NonNull
public Location withSecondary(@NonNull Location secondary, @NonNull String message,
               boolean selfExplanatory) {
  secondary.setMessage(message, selfExplanatory);
  setSecondary(secondary);
  return this;
}
/**

代码示例来源:origin: com.android.tools.lint/lint-checks

location.setMessage(String.format("Declaration with array size (%1$d)",
        LintUtils.getChildCount(element)));
location.setSecondary(mLocations.get(name));
mLocations.put(name, location);

代码示例来源:origin: com.android.tools.lint/lint-checks

/**
 * Checks a String.format call that is using a string that doesn't contain format placeholders.
 * @param context the context to report errors to
 * @param call the AST node for the {@link String#format}
 * @param name the string name
 * @param handle the string location
 */
private static void checkNotFormattedHandle(
    JavaContext context,
    PsiMethodCallExpression call,
    String name,
    Handle handle) {
  Object clientData = handle.getClientData();
  if (clientData instanceof Node) {
    if (context.getDriver().isSuppressed(null, INVALID, (Node) clientData)) {
      return;
    }
  }
  Location location = context.getLocation(call);
  Location secondary = handle.resolve();
  secondary.setMessage("This definition does not require arguments");
  location.setSecondary(secondary);
  String message = String.format(
      "Format string '`%1$s`' is not a valid format string so it should not be " +
          "passed to `String.format`",
      name);
  context.report(INVALID, call, location, message);
}

相关文章