How to create an instance out of a TypeMirror

后端 未结 2 365
Happy的楠姐
Happy的楠姐 2021-01-28 03:38

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         


        
2条回答
  •  攒了一身酷
    2021-01-28 04:12

    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.

    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, 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.

提交回复
热议问题