is Assignable From TypeElement - Java javax.lang.model.element

Java examples for javax.lang.model.element:TypeElement

Description

is Assignable From TypeElement

Demo Code


//package com.java2s;
import javax.lang.model.element.Element;

import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.NoType;
import javax.lang.model.type.TypeMirror;

public class Main {
    public static boolean isAssignableFrom(TypeMirror targetType,
            TypeMirror type) {//w  w  w  .  j a v a  2  s  .  co  m
        if ((targetType instanceof DeclaredType)
                && (type instanceof DeclaredType)) {
            Element targetElement = ((DeclaredType) targetType).asElement();
            Element element = ((DeclaredType) type).asElement();
            if ((targetElement instanceof TypeElement)
                    && (element instanceof TypeElement)) {
                return isAssignableFrom((TypeElement) targetElement,
                        (TypeElement) element);
            }
        }
        return false;
    }

    public static boolean isAssignableFrom(TypeElement targetElement,
            TypeElement element) {
        TypeElement currentElement = element;

        //Compare at interface level
        for (TypeMirror interfaceType : element.getInterfaces()) {
            Element interfaceElement = ((DeclaredType) interfaceType)
                    .asElement();
            if (targetElement.equals(interfaceElement)) {
                return true;
            }
        }

        //Compare at class level
        while (currentElement != null) {
            if (targetElement.equals(currentElement)) {
                return true;
            }
            currentElement = getSuperClass(currentElement);
        }
        return false;
    }

    public static boolean isAssignableFrom(Class clazz, TypeElement element) {
        TypeElement currElement = element;
        if (clazz.isInterface()) {
            for (TypeMirror interfaceType : element.getInterfaces()) {
                Element interfaceElement = ((DeclaredType) interfaceType)
                        .asElement();
                if (clazz.getName().equals(
                        getClassName((TypeElement) interfaceElement))) {
                    return true;
                }
            }
        }

        while (currElement != null) {
            if (getClassName(currElement).equals(clazz.getName())) {
                return true;
            }
            TypeMirror parent = currElement.getSuperclass();
            if (parent instanceof NoType) {
                break;
            }
            if (parent instanceof DeclaredType) {
                currElement = (TypeElement) ((DeclaredType) parent)
                        .asElement();
            }
        }

        return false;
    }

    public static TypeElement getSuperClass(TypeElement element) {
        TypeMirror parent = element.getSuperclass();
        if (parent instanceof DeclaredType) {
            Element elt = ((DeclaredType) parent).asElement();
            if (elt instanceof TypeElement) {
                return (TypeElement) elt;
            }
        }
        return null;
    }

    public static String getClassName(TypeElement element) {
        Element currElement = element;
        String result = element.getSimpleName().toString();
        while (currElement.getEnclosingElement() != null) {
            currElement = currElement.getEnclosingElement();
            if (currElement instanceof TypeElement) {
                result = currElement.getSimpleName() + "$" + result;
            } else if (currElement instanceof PackageElement) {
                if (!"".equals(currElement.getSimpleName())) {
                    result = ((PackageElement) currElement)
                            .getQualifiedName() + "." + result;
                }
            }
        }
        return result;
    }
}

Related Tutorials