如何从TypeMirror创建实例 [英] How to create an instance out of a TypeMirror

查看:1348
本文介绍了如何从TypeMirror创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注释,根据这个成语接收动态参数,即接口类型的参数。简而言之:

I have an annotation that receives a "dynamic" parameter according to this idiom, i.e. a parameter of an interface type. In short:

public interface MyInterface {}

public @interface MyAnnotation {
  Class<? extends MyInterface> value();
}

现在,要评估此参数,我需要创建提供的实现的实例。上面链接的答案在运行时执行此操作。然而,我正在按照本教程。使用类型时,您必须考虑它们可能尚未编译。本教程通过以下方式处理(在这种情况下检索类型的名称):

Now, to evaluate this parameter I need to create an instance of the provided implementation. The answer linked above does this at runtime. I am, however, writing a "real" (i.e. compile-time) annotation processor following this tutorial. When working with types you have to consider that they may not be compiled yet. The tutorial handles that (in that case to retrieve the the type's name) in the following way:


// Get the full QualifiedTypeName
try {
  Class<?> clazz = annotation.type();
  qualifiedSuperClassName = clazz.getCanonicalName();
  simpleTypeName = clazz.getSimpleName();
} catch (MirroredTypeException mte) {
  DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
  TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
  qualifiedSuperClassName = classTypeElement.getQualifiedName().toString();
  simpleTypeName = classTypeElement.getSimpleName().toString();
}


因此,为了实例化类型, 尝试块我可以使用 newInstance()。但是我必须在 catch 块中创建实例?或者是不可能,因为尚未编译类型?在这种情况下,如何解决动态参数问题?

So, to instantiate the type, in the try block I can use newInstance(). But what do I have to do in the catch block to create an instance? Or is it not possible because the type has not been compiled yet? In that case, how do I solve the "dynamic parameter" issue?

编辑:在我的具体情况下,我可能会使用字符串参数并将其解释为Groovy模板。仍然在寻找答案。

In my specific case I may resort to using a String parameter and interpreting it as a Groovy template. Still looking for answers though.

推荐答案

在注释处理期间,在一般情况下,不可能构造任何类的实例被编译。请记住,注释处理在编译器中运行,作为多步骤过程的一部分。在注释处理器执行时,无法保证实际编译类及其所有依赖项并准备进行类加载。

During annotation processing, in the general case it is not possible to construct an instance of any class being compiled. Remember that annotation processing runs inside the compiler, as a part of a multi-step process. There's no guarantee that a class and all its dependencies are actually compiled and ready to class-load at the time your annotation processor is executing.

另请注意,注释声明是无效。 Java的第9.6.1节语言规范列出了注释元素的有效类型,用户定义的接口的值不是其中之一。你可以拥有的是 Class<?扩展MyInterface> ,但是你有关于如何实例化它的相同问题。

Note also that your annotation declaration is invalid. Section 9.6.1 of the Java language specification lists the valid types of annotation elements, and a value of an user-defined interface is not one of them. At best what you can have is a Class<? extends MyInterface>, but then you have the same issues about how to instantiate it.

这听起来像你的注释处理用例非常专门。如果你打开一个关于你想要解决的实际问题的新问题,这可能会有所帮助,因为可能有更好的方法来解决它。

It sounds like your use case for annotation processing is very specialized. It would probably help if you opened a new question about the actual problem you're trying to solve, since there may be a better way to solve it than this.

这篇关于如何从TypeMirror创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆