2011-03-08 3 views
2

이 두 클래스내가 주석 프로세서에이 코드를 실행하면 자바 1.6 주석 처리

public class MyClass extends MyAbstractClass<Cow> { 
    ... 
} 

public abstract class MyAbstractClass<Foo_ extends AbstractFoo> { 
    ... 
    Key<Foo_> foo; 
    ... 
} 

을 실제 타입 대신의 형식 매개 변수를 감안할 때 얻을 때, 나는 내가 원하는 결과를 얻을 수 없습니다.

for (VariableElement fieldElement : ElementFilter.fieldsIn(env.getElementUtils().getAllMembers((TypeElement)entityElement))) { 
    String fieldType = fieldElement.asType().toString(); 
} 

env는 ProcessingEnvironment입니다. entityElement는 요소입니다. (MyClass)

fieldType이 Key<Foo_>으로 설정됩니다.

fieldType을 Key<MyClass>으로 설정하려면 무엇을 호출해야합니까?

+0

를 사용하여 서브 클래스 MyClass에서 본 Types 유틸리티를 사용하여 필드의 유형을받을 수 있나요? 'MyAbstractClass' 클래스를위한 것인가? –

답변

2

foo의 유형은 코드에있는 그대로 Foo_입니다. 제 생각에는 Key<MyClass> 대신에 Key<Cow>을 사용했습니다. 거기서 사용 된 형식 인수입니다. 당신이 그 출력을 얻을 때 TypeElement가 처리되는 방법 getDeclaredType

// these are the types as declared in the source 
System.out.println(fieldElement.asType());  // Key<Foo_> 
System.out.println(t.getSuperclass());   // MyAbstractClass<Cow> 

// extract the type argument of MyAbstractClass 
TypeMirror superClassParameter = ((DeclaredType) t.getSuperclass()).getTypeArguments().get(0); 
System.out.println(superClassParameter);  // Cow 

// use the type argument and the field's type's type element to construct the fields actual type 
DeclaredType fieldType = typeUtils.getDeclaredType(
     (TypeElement) typeUtils.asElement(fieldElement.asType()), superClassParameter); 

System.out.println(fieldType);     // Key<Cow> 
관련 문제