Search in sources :

Example 1 with BaseProcessingEnvImpl

use of org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl in project mapstruct by mapstruct.

the class EclipseAsMemberOfWorkaround method asMemberOf.

/**
 * Eclipse-specific implementation of {@link Types#asMemberOf(DeclaredType, Element)}.
 * <p>
 * Returns {@code null} if the implementation could not determine the result.
 */
static TypeMirror asMemberOf(ProcessingEnvironment environment, DeclaredType containing, Element element) {
    ElementImpl elementImpl = tryCast(element, ElementImpl.class);
    BaseProcessingEnvImpl env = tryCast(environment, BaseProcessingEnvImpl.class);
    if (elementImpl == null || env == null) {
        return null;
    }
    ReferenceBinding referenceBinding = (ReferenceBinding) ((ElementImpl) environment.getTypeUtils().asElement(containing))._binding;
    MethodBinding methodBinding = (MethodBinding) elementImpl._binding;
    // matches in super-classes have priority
    MethodBinding inSuperclassHiearchy = findInSuperclassHierarchy(methodBinding, referenceBinding);
    if (inSuperclassHiearchy != null) {
        return env.getFactory().newTypeMirror(inSuperclassHiearchy);
    }
    // if nothing was found, traverse the interfaces and collect all candidate methods that match
    List<MethodBinding> candidatesFromInterfaces = new ArrayList<MethodBinding>();
    collectFromInterfaces(methodBinding, referenceBinding, new HashSet<ReferenceBinding>(), candidatesFromInterfaces);
    // there can be multiple matches for the same method name from adjacent interface hierarchies.
    Collections.sort(candidatesFromInterfaces, MostSpecificMethodBindingComparator.INSTANCE);
    if (!candidatesFromInterfaces.isEmpty()) {
        // return the most specific match
        return env.getFactory().newTypeMirror(candidatesFromInterfaces.get(0));
    }
    return null;
}
Also used : BaseProcessingEnvImpl(org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl) ElementImpl(org.eclipse.jdt.internal.compiler.apt.model.ElementImpl) ArrayList(java.util.ArrayList) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)

Aggregations

ArrayList (java.util.ArrayList)1 BaseProcessingEnvImpl (org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl)1 ElementImpl (org.eclipse.jdt.internal.compiler.apt.model.ElementImpl)1 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1