I have an annotation to which I want to pass a class as a parameter.

@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Documented public @interface PostApiRequest { Class<?> value(); } 

The annotation hangs on the method of the abstract parent class.

 @PostApiRequest(value = ...) @Override public ResponseEntity<D> save(@RequestBody D dto) { 

The application does not know which heir will call the method, so I need to pass the heir class as a parameter in order to work with its annotations through reflection. Of course, I would like to see something like:

 @PostApiRequest(value = this.class) //передаём класс-наследник @Override public ResponseEntity<D> save(@RequestBody D dto) { 

but this design does not work.

Tell me how to pass the class parameter annotations?

  • If a single heir, then pass it. And if there are a lot of them, hang the listener through a proxy and process it already there - GenCloud
  • Can you tell me more about the listener through a proxy? - Vyacheslav Chernyshov
  • Why does not work or what is the problem? - Roman C
  • Why does not work or what is the problem? - Roman C
  • The problem is that the parameter annotations can only pass a constant. - Vyacheslav Chernyshov

1 answer 1

As an example, look here , Cglib is used. The invoke function is processed when any method of the CrudRepository class is called. Creating a proxy can look here

  • But is there an easier example? sorry, but the solid text of the code is hard to read ... - Sanaev
  • @ Sanaev, what exactly is not clear to you? - GenCloud
  • Did I understand correctly that your method is something like using Proxy.newProxyInstance ? - Vyacheslav Chernyshov
  • @ Vyacheslav Chernyshov, yes, but much faster than default api - GenCloud