MethodFinder.java 文件源码

java
阅读 44 收藏 0 点赞 0 评论 0

项目:FreeBuilder 作者:
/**
 * Returns all methods, declared and inherited, on {@code type}, except those specified by
 * {@link Object}.
 *
 * <p>If method B overrides method A, only method B will be included in the return set.
 * Additionally, if methods A and B have the same signature, but are on unrelated interfaces,
 * one will be arbitrarily picked to be returned.
 */
public static ImmutableSet<ExecutableElement> methodsOn(TypeElement type, Elements elements)
    throws CannotGenerateCodeException {
  TypeElement objectType = elements.getTypeElement(Object.class.getCanonicalName());
  SetMultimap<Signature, ExecutableElement> methods = LinkedHashMultimap.create();
  for (TypeElement supertype : getSupertypes(type)) {
    for (ExecutableElement method : methodsIn(supertype.getEnclosedElements())) {
      if (method.getEnclosingElement().equals(objectType)) {
        continue;  // Skip methods specified by Object.
      }
      Signature signature = new Signature(method);
      Iterator<ExecutableElement> iterator = methods.get(signature).iterator();
      while (iterator.hasNext()) {
        ExecutableElement otherMethod = iterator.next();
        if (elements.overrides(method, otherMethod, type)
            || method.getParameters().equals(otherMethod.getParameters())) {
          iterator.remove();
        }
      }
      methods.put(signature, method);
    }
  }
  return ImmutableSet.copyOf(methods.values());
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号