-1

I want to know how Java generates annotation implementation like @Mapper and @FeignClient. Does It generates an implementation class in runtime?

Thanks.

Know how Java generates an implementation for an interface in runtime.

  • Are you asking how an implementation of [`java.lang.annotation.Annotation`](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/annotation/Annotation.html) is created? Or are you asking how the functionality "around" the annotation is implemented (e.g., how does `@Inject` from CDI allow dependencies to be injected)? – Slaw Jun 08 '23 at 21:03
  • I'm looking for the funcitonality around the annotation. I've tried to find the implementation of some annotations in Eclipse but using Ctrl + Click for go to implementation does nothing. – daniel-lopez Jun 09 '23 at 14:06
  • 1
    Then as noted by Renis in his answer, it's not "Java" that creates the implementation. If it's an annotation processed at _compile-time_, then an implementation of [`Processor`](https://docs.oracle.com/en/java/javase/20/docs/api/java.compiler/javax/annotation/processing/Processor.html) must be present during compilation. If it's an annotation processed at _run-time_, then some code somewhere is calling methods like [`getAnnotation(Class)`](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/reflect/AnnotatedElement.html#getAnnotation(java.lang.Class)) and acting accordingly. – Slaw Jun 09 '23 at 19:52
  • 1
    And if it's [an agent](https://docs.oracle.com/en/java/javase/20/docs/api/java.instrument/java/lang/instrument/package-summary.html) that processes the annotations (e.g., an AOP library), then some kind of byte-code manipulation library is likely being used. All that said, "Ctrl + Click"-ing on the annotation should only take you to the annotation definition (even with third-party libraries, assuming they're a dependency of your project). If you want to try and find the code that processes the annotation, see if using the "Find Usages" action of your IDE on the annotation helps. – Slaw Jun 09 '23 at 19:55
  • OK. I will try to check "Find Usages". Thanks for your answer. – daniel-lopez Jun 13 '23 at 20:52

1 Answers1

0

Java does not do anything when it comes to Annotation-based class generation. The magic behind these generations is the libraries themselves.

For example, MapStruct goes through all the classes and handles those with a @Mapper Annotation. This is achieved through annotation processing and the Java Annotation Processing API. You can also configure how you want to make your generated classes available to the application (e.g. @Mapper(componentModel = "spring")). In Spring the generated Mapper will be turned into a Spring Bean and will be associated with the Mapper (Interface) you autowire/inject in your other Spring Beans.

In JavaEE Context, it is basically the same thing, it's just that the injection is achieved a bit differently (e.g. CDI).

I have no experience with feign, but I think it should use a similar approach.

Filip
  • 19,269
  • 7
  • 51
  • 60
Renis1235
  • 4,116
  • 3
  • 15
  • 27
  • Yes. I'm looking for the implementation classes. In Eclipse I can go to an own implementation class from an interface using Ctrl + Clikc, but I cannot do that with thrid party libraries annotations. – daniel-lopez Jun 09 '23 at 14:09