2

我有一个注释处理器,它接受一个带注释的类并尝试创建它的子类:

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只会给出变量名称,而不是边界。关于如何做到这一点的任何想法?

4

1 回答 1

1

的类型参数TypeElement可以通过调用来获取,getTypeParameters()并且可以通过调用getBounds()获取TypeParameterElement的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,使用原始类型”。

于 2016-11-15T08:01:41.777 回答