2016-11-14 89 views
2

我有一个注释处理器需要一个注解类,并试图创建它的一个子类:如何查找TypeMirror的边界并将它们转换为JavaPoet TypeSpec?

package test; 

import com.squareup.javapoet.ClassName; 
import com.squareup.javapoet.JavaFile; 
import com.squareup.javapoet.TypeSpec; 

import java.io.IOException; 
import java.util.Set; 

import javax.annotation.processing.AbstractProcessor; 
import javax.annotation.processing.ProcessingEnvironment; 
import javax.annotation.processing.RoundEnvironment; 
import javax.annotation.processing.SupportedAnnotationTypes; 
import javax.annotation.processing.SupportedSourceVersion; 
import javax.lang.model.SourceVersion; 
import javax.lang.model.element.Element; 
import javax.lang.model.element.Modifier; 
import javax.lang.model.element.TypeElement; 

@SupportedAnnotationTypes("java.lang.SuppressWarnings") 
@SupportedSourceVersion(SourceVersion.RELEASE_7) 
public class BSProcessor extends AbstractProcessor { 
    @Override 
    public synchronized void init(ProcessingEnvironment processingEnv) { 
     super.init(processingEnv); 
    } 

    @Override 
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) { 
     for (TypeElement baseClassAnnotation : annotations) { 
      for (Element annotatedElement : roundEnvironment.getElementsAnnotatedWith(baseClassAnnotation)) { 
       handleAnnotatedTypeElement((TypeElement) annotatedElement); 
      } 
     } 
     return true; 
    } 

    private void handleAnnotatedTypeElement(TypeElement annotatedTypeElement) { 
     try { 
      javaFile(annotatedTypeElement).writeTo(System.out); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private JavaFile javaFile(TypeElement annotatedTypeElement) { 
     return JavaFile.builder(packageName(annotatedTypeElement), typeSpec(annotatedTypeElement)) 
       .build(); 
    } 

    private TypeSpec typeSpec(TypeElement annotatedTypeElement) { 
     return TypeSpec.classBuilder(className(annotatedTypeElement)) 
       .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) 
       .build(); 
    } 

    private ClassName className(TypeElement annotatedTypeElement) { 
     return ClassName.get(packageName(annotatedTypeElement), String.format("AutoGenerated_%s", 
       annotatedTypeElement.getSimpleName())); 
    } 

    private String packageName(TypeElement annotatedTypeElement) { 
     return annotatedTypeElement.getEnclosingElement().toString(); 
    } 
} 

这适用于类中,无类型参数,但我不知道如何与他们这样做。在类型变量上执行toString将只给出变量名称,而不是边界。任何想法如何做到这一点?

回答

1

TypeElement的型号参数可以通过调用getTypeParameters()进行检索,可以通过调用getBounds()获得 s来获取其边界。我假设,你对以下步骤感到困惑 - 应该将哪些具体类型传递给这些类型参数以满足这些边界。

不幸的是,这是一个问题,不能轻易解决任意类型(这是你似乎以后)。例如,看这个类型:

public abstract class Recursive<UU extends Callable<UU>> { 
} 

您可以直观地得出结论,认为它可以通过这样一个类实现:

public class Solution extends Recursive<Solution> implements Callable<Solution> { 
    @Override 
    public Solution call() throws Exception { 
    return new Solution(); 
    } 
} 

但是这不平凡的自动化,而你(可能)不想在代码中包含所需的机器。

,而不是试图自己解决这个问题,我建议你利用类型擦除,让编译器为你解决这个问题:

// returns something like "Map<K, V>", this is NOT what you want! 
DeclaredType classType = (DeclaredType) typeElement.asType(); 

Types types = processingEnvironment.getTypeUtils(); 
Elements elements = processingEnvironment.getElementUtils(); 

// this obtains raw type (plain "Map"), with all methods type-erased, 
// the compiler is much better at solving type riddles than you! 
DeclaredType rawType = types.getDeclaredType(typeElement); 

final Collection<? extends ExecutableElement> methods = 
    ElementFilter.methodsIn(elements.getAllMembers(typeElement)); 

// To create a MethodSpec, suitable for the raw type, you should 
// call 3-parameter MethodSpec#overriding with type-erased raw class type 
// as second parameter and javax.lang.model.util.Types instance as third 
MethodSpec newMethod = MethodSpec.overriding(methods.get(0), rawType, types); 

因此,回答您的具体问题是“唐将任何类型参数传递给JavaPoet,使用原始类型“。

+0

谢谢。帮了很多! –

相关问题