Search in sources :

Example 1 with ElementImpl

use of org.eclipse.jdt.internal.compiler.apt.model.ElementImpl 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)

Example 2 with ElementImpl

use of org.eclipse.jdt.internal.compiler.apt.model.ElementImpl in project bazel-jdt-java-toolchain by salesforce.

the class AnnotationDiscoveryVisitor method resolveAnnotations.

private void resolveAnnotations(BlockScope scope, Annotation[] annotations, Binding currentBinding) {
    int length = annotations == null ? 0 : annotations.length;
    if (length == 0)
        return;
    boolean old = scope.insideTypeAnnotation;
    scope.insideTypeAnnotation = true;
    currentBinding.getAnnotationTagBits();
    scope.insideTypeAnnotation = old;
    ElementImpl element = (ElementImpl) _factory.newElement(currentBinding);
    // discovery is never in terms of repeating annotation.
    AnnotationBinding[] annotationBindings = element.getPackedAnnotationBindings();
    for (AnnotationBinding binding : annotationBindings) {
        ReferenceBinding annotationType = binding.getAnnotationType();
        if (binding != null && Annotation.isAnnotationTargetAllowed(scope, annotationType, currentBinding)) {
            // binding should be resolved, but in case it's not, ignore it: it could have been wrapped into a container.
            TypeElement anno = (TypeElement) _factory.newElement(annotationType);
            _annoToElement.put(anno, element);
        }
    }
}
Also used : ElementImpl(org.eclipse.jdt.internal.compiler.apt.model.ElementImpl) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) TypeElement(javax.lang.model.element.TypeElement) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)

Aggregations

ElementImpl (org.eclipse.jdt.internal.compiler.apt.model.ElementImpl)2 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)2 ArrayList (java.util.ArrayList)1 TypeElement (javax.lang.model.element.TypeElement)1 BaseProcessingEnvImpl (org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl)1 AnnotationBinding (org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding)1 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)1