get Class Name from TypeElement - Java Reflection

Java examples for Reflection:Class

Description

get Class Name 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;

public class Main {
    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;
                }/*www.  j ava2  s .c o  m*/
            }
        }
        return result;
    }
}

Related Tutorials