Search code examples
javaannotationsaopannotation-processingaspect

What is the fundamental difference between Custom Annotations, Pluggable Annotation Processing and AOP (Aspect Oriented Programming)?


I am having difficulties to differentiate between all these annotations tools and their use cases. My guess, although the literature is confusing, is:

  • Custom Annotations are meant to be used in runtime retention policy as markers to be interpreted by means of Reflection API

  • Annotation Processing is meant to be used in class (for static checking at compile time) and source (for source generation) retention policies

  • AOP is meant to modify the code at runtime

So, do Custom Annotations and AOP make sense out of Runtime Retention Policy? Do Annotation Processing make sense out of Class/Source Retention Policies? Is the difference between AOP and Custom Annotations the sole fact that the laters are passive (you need to receive the annotated object as a param in order to do something)? Why is a framework like Checker needed at all?


Solution

    • Annotations are just a Java syntactic means to add information to classes, methods, fields or method parameters. There are three retention types specifying where the annotations are available:

      • SOURCE means that the annotations are only in the source code and being discarded during compilation.
      • CLASS means that the annotations are in the class file and available for bytecode processing steps after compilation. This is the default value.
      • RUNTIME means that you can read the annotations during runtime via reflection.
    • Annotation processing enables you to read annotations from your source code (i.e. even those with SOURCE retention) and use that information in order to generate more source code which can later be compiled. It is a kind of pre-processing step helping you to create boiler-plate code automatically instead of typing it manually.

    • AOP (Aspect-Oriented Programming) is an unrelated concept, please read Wikipedia or so in order to learn more. It is not meant to modify your code but its behaviour by weaving cross-cutting concerns into your main application logic. There are several ways to implement and apply AOP, e.g.

      • during runtime via dynamic proxies and delegation as Spring AOP does it or
      • via compile-time, binary or load-time weaving, all three of which are supported by AspectJ.

    Incidentally, AOP tools like Spring AOP or AspectJ can access RUNTIME annotations. It does not mean you need to use annotations in order to apply aspects to your code, they are just one of several ways to mark and identify joinpoints in your application logic where aspects should be applied.

    Besides, the AspectJ compiler also supports an annotation processing step, i.e. you can for example write an annotation processor creating new aspects or application classes from your source code annotations before compilation.

    In order to explain all of this in more detail I would need to write a series of articles or a book, so please take it from here by yourself and learn about these concepts by reading other websites or books.