This discussion is archived
1 Reply Latest reply: Jul 12, 2013 2:51 AM by snicoll RSS

Retrieve DeclaredType hierarchy resolved with generics

snicoll Newbie
Currently Being Moderated
I am writing an annotation processor that is meant to create a meta-model of a given class and this meta-model is able to handle generics. Consider the following simple use case

public interface Controller<T> { ... }
public interface FooController<T extends Foo> extends Controller<T> {....}
public abstract class BaseFooController<T extends Foo, O extends Officer<T>> implements FooController<T> { ...}
public class DefaultFooController<T extends Foo> extends BaseFooController<T, MyOfficer<T>> {...}
public class MyFooController extends DefaultFooController<MyFoo> { ...}

Assume that I have the DeclaredType for MyFooController and through a call to _.asElement_ get the super class of it. And suppose I want to do this up to the hierarchy. Here is what I get

parent1: DefaultFooController<MyFoo>
parent2: BaseFooController<T, MyOfficer<T>>

I was expecting a BaseFooController<MyFoo, MyOfficer<MyFoo>> here. Actually I have the proper information in the debugger. My original type is a Type$ClassType and it has a supertype_field public parameter. That parameter has also the same field since it has the same type. Looking at the grand parent in the debugger I can find the resolved type like I expect.

Here is the code I used (for debugging purposes)

private void logHierarchy(DeclaredType originalType) {
System.out.println("Root type:" + originalType);
final DeclaredType parent = (DeclaredType) ((TypeElement) originalType.asElement()).getSuperclass();
System.out.println("Parent1:" + parent);
final DeclaredType grandParent = (DeclaredType) ((TypeElement) parent.asElement()).getSuperclass();
System.out.println("Parent2:" + grandParent);
}
}

Does anybody know which API I should use to retrieve the resolved type? Note that in mycase, parent is a Type$ClassType but grandParent (the one that is not properly resolved) is a ClassReader$1.

Thanks,
S.
  • 1. Re: Retrieve DeclaredType hierarchy resolved with generics
    snicoll Newbie
    Currently Being Moderated

    Replying to myself: instead of using the element to get the super class, we should use the TypesHelper. The following does the trick:

     

    public DeclaredType getSuperType(DeclaredType declaredType) {

            final List<? extends TypeMirror> directSuperTypes = types.directSupertypes(declaredType);

            if (directSuperTypes.isEmpty()) {

                return null; // Reached Object

            }

           return  (DeclaredType) directSuperTypes.get(0); // The first is the super class

    }

     

    where types is the TypesHelper

     

    I am now improving the current code with the support of interfaces. So let's say we have the exact same use case as above but we have interfaces instead of classes. The problem is that the compiler does not seem to support this use case because if I look up the hierarchy in the debugger, the resolved types are lost. Since the information is not stored in the object in the debugger, I guess there is no way to know it. I noticed  this is the case only if I try to process the interface itself. If the interfaces are included as part of a class hierarchy, the types are properly exposed.

     

    Is this a known issue or should I retrieve this information in a different way?

     

    Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points