Example usage for javax.lang.model.util Elements overrides

List of usage examples for javax.lang.model.util Elements overrides

Introduction

In this page you can find the example usage for javax.lang.model.util Elements overrides.

Prototype

boolean overrides(ExecutableElement overrider, ExecutableElement overridden, TypeElement type);

Source Link

Document

Tests whether one method, as a member of a given type, overrides another method.

Usage

From source file:auto.parse.processor.AutoParseProcessor.java

private void findLocalAndInheritedMethods(TypeElement type, List<ExecutableElement> methods) {
    note("Looking at methods in " + type);
    Types typeUtils = processingEnv.getTypeUtils();
    Elements elementUtils = processingEnv.getElementUtils();
    for (TypeMirror superInterface : type.getInterfaces()) {
        findLocalAndInheritedMethods((TypeElement) typeUtils.asElement(superInterface), methods);
    }/* ww  w  .  j  a  v a 2s.  co  m*/
    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());
    eclipseHack().sortMethodsIfSimulatingEclipse(theseMethods);
    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) {
                methods.add(method);
            }
        }
    }
}