5

I have a DeclaredType of a field and I would like to get the fully qualified type (raw type?) of the field. For example if the field is:

public static Optional<String> foo;

I would like to get java.util.Optional.

Currently I can get the package name with:

env.getElementUtils().getPackageOf(declaredType.asElement());

I can get the type arguments of the type however I come back to the same problem I end up with a List of TypeMirror which I don't know how to get the qualified name of the types.

I have noticed that I can call TypeMirror#toString() and that will return (for the above) something like:

java.util.Optional<java.lang.String>

I guess I could then cut of everything in front of < but that feels like a hack.

For reference this is how I am getting the field:

private VariableElement getFieldWithName(DocletEnvironment environment, TypeElement classDoc, String fieldName) {
    for(VariableElement e : ElementFilter.fieldsIn(environment.getElementUtils().getAllMembers(classDoc))) {
        if(e.getSimpleName().toString().equals(fieldName)) {
            return e;
        }
    }
    return null;
}

TypeElement classElement = env.getElementUtils().getTypeElement(MyClass.class.getCanonicalName());
VariableElement fieldDoc = getFieldWithName(env, classElement, "foo");
DeclaredType declaredType = (DeclaredType) fieldDoc.asType();
Luke
  • 884
  • 8
  • 21

2 Answers2

2

I tested this code and it works on Apache Netbeans 10 and Jdk11. Use ((DeclaredType) variableElement.asType()).asElement().toString():

            DeclaredType declaredType = (DeclaredType) e.asType();//e is VariableElement from the loop
            fullyQualifiedName = declaredType.asElement().toString();

Test Classes:

class MyClass1 {
public static Optional<String> foo;
public static List<String> newList;
public static MyClass2 obj;
public Media media;
public void calculate(Double dl) {
}}

class MyClass2 extends MyClass1{
public static Color cl;
}

And the Doclet implementation:

public class TestVarElement implements Doclet {

 public void testFields(DocletEnvironment env) {
    TypeElement typeElement = env.getElementUtils().getTypeElement(MyClass1.class.getCanonicalName());
    System.out.println("Test for 'foo': "+getFieldWithName(env,typeElement,"foo"));
    System.out.println("Test for 'newList': "+getFieldWithName(env,typeElement,"newList"));
    System.out.println("Test for 'obj': "+getFieldWithName(env,typeElement,"obj"));
    System.out.println("Test for 'media': "+getFieldWithName(env,typeElement,"media"));
}

    private String getFieldWithName(DocletEnvironment env, TypeElement classDoc, String fieldName) {
        String fullyQualifiedName = "";
        for (VariableElement e : ElementFilter.fieldsIn(env.getElementUtils().getAllMembers(classDoc))) {
            if (e.getSimpleName().toString().equals(fieldName)) {
                DeclaredType declaredType = (DeclaredType) e.asType(); //The type of the VariableElement
                fullyQualifiedName = declaredType.asElement().toString(); //Get the fqn 
                break;
            }
        }
        return fullyQualifiedName;
    }
   @Override
    public boolean run(DocletEnvironment docEnv) {
        testFields(docEnv);
        return true;
    }
 
    ... Other Overrids

    }

Debug/Run the program:

     public class NewClass {
            public static void main(String[] args) {
                ToolProvider javadoc=ToolProvider.findFirst("javadoc").orElseThrow();
                int result=javadoc.run(System.out, System.err, new String[]{"-doclet",TestVarElement.class.getName(),"C:\\Users\\Super3\\Documents\\NetBeansProjects\\Myproject\\src\\pk\\TestVarElement.java"});
      
//The following is for java 8 or older, the implementation is diferent where `start` method is used instead of `run`.
     //Main.execute (new String[]{"-doclet",TestVarElement.class.getName(),"C:\\Users\\Super3\\Documents\\NetBeansProjects\\Myproject\\src\\pk\\TestVarElement.java"});
          }
        }

Output

Test for 'foo': java.util.Optional

Test for 'newList': java.util.List

Test for 'obj': pk.MyClass2

Test for 'media': javax.print.attribute.standard.Media

Community
  • 1
  • 1
TiyebM
  • 2,684
  • 3
  • 40
  • 66
0

To convert a Generic type to its raw base, resolve it without any type arguments. See Types#getDeclaredType which can be called without specifying any types:

If zero, and if the type element is generic, then the type element's raw type is returned

DeclaredType rawType = env.getTypeUtils().getDeclaredType(typeElement);
drekbour
  • 2,895
  • 18
  • 28