How to create an instance of an annotation

前端 未结 8 1204
无人及你
无人及你 2021-01-30 12:47

I am trying to do some Java annotation magic. I must say I am still catching up on annotation tricks and that certain things are still not quite clear to me.

So... I hav

8条回答
  •  醉酒成梦
    2021-01-30 12:58

    You can also absolutely stupidly (but simply) create a dummy annotation target and get it from there

    @MyAnnotation(foo="bar", baz=Blah.class)
    private static class Dummy {}
    

    And

    final MyAnnotation annotation = Dummy.class.getAnnotation(MyAnnotation.class)
    

    Creating method/parameter targeted annotation instances may be a little more elaborate, but this approach has the benefit of getting the annotation instance as the JVM would normally do. Needless to say it is as simple as it can get.

提交回复
热议问题