Java Code Examples for javax.lang.model.util.Elements#overrides()

The following examples show how to use javax.lang.model.util.Elements#overrides() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VarUsageVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isMethodAvailable(TypeElement subTypeElement, 
        ExecutableElement execElem, TypeElement superTypeElement) {
    Elements elements = workingCopy.getElements();
    List<? extends Element> memberElements = elements.getAllMembers(superTypeElement);
    for (Element elem : memberElements) {
        if(ElementKind.METHOD.equals(elem.getKind())){
            if(execElem.getModifiers().contains(Modifier.STATIC) && elements.hides(execElem, elem)){
                return true;
            }else{
                if(execElem.equals(elem) || elements.overrides(execElem, (ExecutableElement)elem, 
                        subTypeElement)){
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 2
Source File: MoreElements.java    From auto-parcel with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the set of all non-private methods from {@code type}, including methods that it
 * inherits from its ancestors. Inherited methods that are overridden are not included in the
 * result. So if {@code type} defines {@code public String toString()}, the returned set will
 * contain that method, but not the {@code toString()} method defined by {@code Object}.
 * <p/>
 * <p>The returned set may contain more than one method with the same signature, if
 * {@code type} inherits those methods from different ancestors. For example, if it
 * inherits from unrelated interfaces {@code One} and {@code Two} which each define
 * {@code void foo();}, and if it does not itself override the {@code foo()} method,
 * then both {@code One.foo()} and {@code Two.foo()} will be in the returned set.
 *
 * @param type         the type whose own and inherited methods are to be returned
 * @param elementUtils an {@link Elements} object, typically returned by
 *                     {@link javax.annotation.processing.AbstractProcessor#processingEnv processingEnv}<!--
 *                     -->.{@link javax.annotation.processing.ProcessingEnvironment.getElementUtils()
 *                     getElementUtils()}
 */
public static ImmutableSet<ExecutableElement> getLocalAndInheritedMethods(
        TypeElement type, Elements elementUtils) {

    SetMultimap<String, ExecutableElement> methodMap = LinkedHashMultimap.create();
    getLocalAndInheritedMethods(getPackage(type), type, methodMap);
    // Find methods that are overridden. We do this using `Elements.overrides`, which means
    // that it is inherently a quadratic operation, since we have to compare every method against
    // every other method. We reduce the performance impact by (a) grouping methods by name, since
    // a method cannot override another method with a different name, and (b) making sure that
    // methods in ancestor types precede those in descendant types, which means we only have to
    // check a method against the ones that follow it in that order.
    Set<ExecutableElement> overridden = new LinkedHashSet<ExecutableElement>();
    for (String methodName : methodMap.keySet()) {
        List<ExecutableElement> methodList = ImmutableList.copyOf(methodMap.get(methodName));
        for (int i = 0; i < methodList.size(); i++) {
            ExecutableElement methodI = methodList.get(i);
            for (int j = i + 1; j < methodList.size(); j++) {
                ExecutableElement methodJ = methodList.get(j);
                if (elementUtils.overrides(methodJ, methodI, type)) {
                    overridden.add(methodI);
                }
            }
        }
    }
    Set<ExecutableElement> methods = new LinkedHashSet<ExecutableElement>(methodMap.values());
    methods.removeAll(overridden);
    return ImmutableSet.copyOf(methods);
}
 
Example 3
Source File: Environment.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@NonNull
public Set<ExecutableElement> getLocalAndInheritedMethods(TypeElement type, ConditionCallback<ExecutableElement> includeMethodCallback) {
  SetMultimap<String, ExecutableElement> methodMap = LinkedHashMultimap.create();
  getLocalAndInheritedMethods(getPackage(type), type, methodMap, includeMethodCallback);
  // Find methods that are overridden. We do this using `Elements.overrides`, which means
  // that it is inherently a quadratic operation, since we have to compare every method against
  // every other method. We reduce the performance impact by (a) grouping methods by name, since
  // a method cannot override another method with a different name, and (b) making sure that
  // methods in ancestor types precede those in descendant types, which means we only have to
  // check a method against the ones that follow it in that order.
  Set<ExecutableElement> overridden = new LinkedHashSet<>();
  final Elements elementUtils = this.elementUtils;
  for (String methodName : methodMap.keySet()) {
    List<ExecutableElement> methodList = ImmutableList.copyOf(methodMap.get(methodName));
    for (int i = 0; i < methodList.size(); i++) {
      ExecutableElement methodI = methodList.get(i);
      for (int j = i + 1; j < methodList.size(); j++) {
        ExecutableElement methodJ = methodList.get(j);
        if (elementUtils.overrides(methodJ, methodI, type)) {
          overridden.add(methodI);
        }
      }
    }
  }
  Set<ExecutableElement> methods = new LinkedHashSet<>(methodMap.values());
  methods.removeAll(overridden);
  return methods;
}
 
Example 4
Source File: MethodFinder.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
/**
 * 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 <E extends Exception> ImmutableSet<ExecutableElement> methodsOn(
    TypeElement type,
    Elements elements,
    ErrorTypeHandling<E> errorTypeHandling) throws E {
  TypeElement objectType = elements.getTypeElement(Object.class.getCanonicalName());
  Map<Signature, ExecutableElement> objectMethods = Maps.uniqueIndex(
      methodsIn(objectType.getEnclosedElements()), Signature::new);
  SetMultimap<Signature, ExecutableElement> methods = LinkedHashMultimap.create();
  for (TypeElement supertype : getSupertypes(type, errorTypeHandling)) {
    for (ExecutableElement method : methodsIn(supertype.getEnclosedElements())) {
      Signature signature = new Signature(method);
      if (method.getEnclosingElement().equals(objectType)) {
        continue;  // Skip methods specified by Object.
      }
      if (objectMethods.containsKey(signature)
          && method.getEnclosingElement().getKind() == ElementKind.INTERFACE
          && method.getModifiers().contains(Modifier.ABSTRACT)
          && elements.overrides(method, objectMethods.get(signature), type)) {
        continue;  // Skip abstract methods on interfaces redelaring Object methods.
      }
      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());
}
 
Example 5
Source File: ComponentDescriptor.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
private static void findLocalAndInheritedMethods(Elements elements, TypeElement type,
    List<ExecutableElement> methods) {
  for (TypeMirror superInterface : type.getInterfaces()) {
    findLocalAndInheritedMethods(
        elements, MoreElements.asType(MoreTypes.asElement(superInterface)), methods);
  }
  if (type.getSuperclass().getKind() != TypeKind.NONE) {
    // Visit the superclass after superinterfaces so we will always see the implementation of a
    // method after any interfaces that declared it.
    findLocalAndInheritedMethods(
        elements, MoreElements.asType(MoreTypes.asElement(type.getSuperclass())), methods);
  }
  // Add each method of this class, and in so doing remove any inherited method it overrides.
  // This algorithm is quadratic in the number of methods but it's hard to see how to improve
  // that while still using Elements.overrides.
  List<ExecutableElement> theseMethods = ElementFilter.methodsIn(type.getEnclosedElements());
  for (ExecutableElement method : theseMethods) {
    if (!method.getModifiers().contains(Modifier.PRIVATE)) {
      boolean alreadySeen = false;
      for (Iterator<ExecutableElement> methodIter = methods.iterator(); methodIter.hasNext();) {
        ExecutableElement otherMethod = methodIter.next();
        if (elements.overrides(method, otherMethod, type)) {
          methodIter.remove();
        } else if (method.getSimpleName().equals(otherMethod.getSimpleName())
            && method.getParameters().equals(otherMethod.getParameters())) {
          // If we inherit this method on more than one path, we don't want to add it twice.
          alreadySeen = true;
        }
      }
      if (!alreadySeen) {
        methods.add(method);
      }
    }
  }
}
 
Example 6
Source File: RetroFacebookProcessor.java    From RetroFacebook with Apache License 2.0 4 votes vote down vote up
private void findLocalAndInheritedMethods(TypeElement type, List<ExecutableElement> methods) {
  Types typeUtils = processingEnv.getTypeUtils();
  Elements elementUtils = processingEnv.getElementUtils();
  for (TypeMirror superInterface : type.getInterfaces()) {
    findLocalAndInheritedMethods((TypeElement) typeUtils.asElement(superInterface), methods);
  }
  if (type.getSuperclass().getKind() != TypeKind.NONE) {
    // Visit the superclass after superinterfaces so we will always see the implementation of a
    // method after any interfaces that declared it.
    findLocalAndInheritedMethods(
        (TypeElement) typeUtils.asElement(type.getSuperclass()), methods);
  }
  // Add each method of this class, and in so doing remove any inherited method it overrides.
  // This algorithm is quadratic in the number of methods but it's hard to see how to improve
  // that while still using Elements.overrides.
  List<ExecutableElement> theseMethods = ElementFilter.methodsIn(type.getEnclosedElements());
  for (ExecutableElement method : theseMethods) {
    if (!method.getModifiers().contains(Modifier.PRIVATE)) {
      boolean alreadySeen = false;
      for (Iterator<ExecutableElement> methodIter = methods.iterator(); methodIter.hasNext(); ) {
        ExecutableElement otherMethod = methodIter.next();
        if (elementUtils.overrides(method, otherMethod, type)) {
          methodIter.remove();
        } else if (method.getSimpleName().equals(otherMethod.getSimpleName())
            && method.getParameters().equals(otherMethod.getParameters())) {
          // If we inherit this method on more than one path, we don't want to add it twice.
          alreadySeen = true;
        }
      }
      if (!alreadySeen) {
        /*
        retrofacebook.RetroFacebook.GET action = method.getAnnotation(retrofacebook.RetroFacebook.GET.class);
        System.out.printf(
            "%s Action value = %s\n",
            method.getSimpleName(),
            action == null ? null : action.value() );
        */
        methods.add(method);
      }
    }
  }
}
 
Example 7
Source File: RetroFacebookProcessor.java    From RetroFacebook with Apache License 2.0 4 votes vote down vote up
private void findLocalAndInheritedMethods(TypeElement type, List<ExecutableElement> methods) {
  Types typeUtils = processingEnv.getTypeUtils();
  Elements elementUtils = processingEnv.getElementUtils();
  for (TypeMirror superInterface : type.getInterfaces()) {
    findLocalAndInheritedMethods((TypeElement) typeUtils.asElement(superInterface), methods);
  }
  if (type.getSuperclass().getKind() != TypeKind.NONE) {
    // Visit the superclass after superinterfaces so we will always see the implementation of a
    // method after any interfaces that declared it.
    findLocalAndInheritedMethods(
        (TypeElement) typeUtils.asElement(type.getSuperclass()), methods);
  }
  // Add each method of this class, and in so doing remove any inherited method it overrides.
  // This algorithm is quadratic in the number of methods but it's hard to see how to improve
  // that while still using Elements.overrides.
  List<ExecutableElement> theseMethods = ElementFilter.methodsIn(type.getEnclosedElements());
  for (ExecutableElement method : theseMethods) {
    if (!method.getModifiers().contains(Modifier.PRIVATE)) {
      boolean alreadySeen = false;
      for (Iterator<ExecutableElement> methodIter = methods.iterator(); methodIter.hasNext(); ) {
        ExecutableElement otherMethod = methodIter.next();
        if (elementUtils.overrides(method, otherMethod, type)) {
          methodIter.remove();
        } else if (method.getSimpleName().equals(otherMethod.getSimpleName())
            && method.getParameters().equals(otherMethod.getParameters())) {
          // If we inherit this method on more than one path, we don't want to add it twice.
          alreadySeen = true;
        }
      }
      if (!alreadySeen) {
        /*
        retrofacebook.RetroFacebook.GET action = method.getAnnotation(retrofacebook.RetroFacebook.GET.class);
        System.out.printf(
            "%s Action value = %s\n",
            method.getSimpleName(),
            action == null ? null : action.value() );
        */
        methods.add(method);
      }
    }
  }
}
 
Example 8
Source File: RetroWeiboProcessor.java    From SimpleWeibo with Apache License 2.0 4 votes vote down vote up
private void findLocalAndInheritedMethods(TypeElement type, List<ExecutableElement> methods) {
  Types typeUtils = processingEnv.getTypeUtils();
  Elements elementUtils = processingEnv.getElementUtils();
  for (TypeMirror superInterface : type.getInterfaces()) {
    findLocalAndInheritedMethods((TypeElement) typeUtils.asElement(superInterface), methods);
  }
  if (type.getSuperclass().getKind() != TypeKind.NONE) {
    // Visit the superclass after superinterfaces so we will always see the implementation of a
    // method after any interfaces that declared it.
    findLocalAndInheritedMethods(
        (TypeElement) typeUtils.asElement(type.getSuperclass()), methods);
  }
  // Add each method of this class, and in so doing remove any inherited method it overrides.
  // This algorithm is quadratic in the number of methods but it's hard to see how to improve
  // that while still using Elements.overrides.
  List<ExecutableElement> theseMethods = ElementFilter.methodsIn(type.getEnclosedElements());
  for (ExecutableElement method : theseMethods) {
    if (!method.getModifiers().contains(Modifier.PRIVATE)) {
      boolean alreadySeen = false;
      for (Iterator<ExecutableElement> methodIter = methods.iterator(); methodIter.hasNext(); ) {
        ExecutableElement otherMethod = methodIter.next();
        if (elementUtils.overrides(method, otherMethod, type)) {
          methodIter.remove();
        } else if (method.getSimpleName().equals(otherMethod.getSimpleName())
            && method.getParameters().equals(otherMethod.getParameters())) {
          // If we inherit this method on more than one path, we don't want to add it twice.
          alreadySeen = true;
        }
      }
      if (!alreadySeen) {
        /*
        retroweibo.RetroWeibo.GET action = method.getAnnotation(retroweibo.RetroWeibo.GET.class);
        System.out.printf(
            "%s Action value = %s\n",
            method.getSimpleName(),
            action == null ? null : action.value() );
        */
        methods.add(method);
      }
    }
  }
}