7

In a recent interview for a Java developer , I met with a question on spring annotation:

What is the difference between compile time and runtime annotations in spring?

Is there a concept like this?

An answer would be appreciated.

riorio
  • 6,500
  • 7
  • 47
  • 100
Sam
  • 81
  • 1
  • 4

3 Answers3

7

There is nothing like compile time or run-time annotations.

They're markers that are read different API. Based on how they are processed, one can call them compile/run-time annotation.

Java provides two different options for processing source code annotations. One of them is the annotation processing API used in compile time, and the other is the Reflection API used in runtime. e.g.@Override annotation is read by compiler.

https://ieeexplore.ieee.org/document/7321547/

Somewhat similar to your question. Have a look at it: When are Java annotations executed?

Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
4

Colloquially, when someone refers to a "compile-time" or "runtime" annotation in Java, they are likely referring to the annotation's assigned (or implied) retention policy. So to answer the interview question requires knowledge of Java annotations in general, and the specific annotations defined by the Spring framework. With regard to Java, JLS 15 §9.6.4.2 says:

Annotations may be present only in source code, or they may be present in the binary form of a class or interface. An annotation that is present in the binary form may or may not be available at run time via the reflection libraries of the Java SE Platform. The annotation type java.lang.annotation.Retention is used to choose among these possibilities.

The RetentionPolicy enum offers three possibilities (ordering mine):

SOURCE - Annotations are to be discarded by the compiler.

CLASS - Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.

RUNTIME - Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

All annotations are accessible if you have access to the source code, but annotations with SOURCE retention will never appear in the compiled bytecode; they will never appear to any tools that read the compiled form of the class -- such as when compiling a downstream application -- and without bytecode there is nothing to load at runtime, either.

When an annotation has CLASS retention, the annotation has a presence in the compiled bytecode where the annotation is used -- so that use can be examined by tools that operate directly on bytecode. Examples include compilers, code quality tools (e.g. Spotbugs), or runtime code generators (ASM, javassist, Byte-Buddy, etc.). While the annotation information is present in the bytecode (*.class file), the JVM does not load this information at runtime.

Finally, an annotation with RUNTIME retention has all of the capabilities above, but is also loaded into the JVM at runtime and can be discovered using the Reflection APIs from within running programs.

If retention is not explicitly defined for an annotation, it defaults to RetentionPolicy.CLASS.

William Price
  • 4,033
  • 1
  • 35
  • 54
2

Annotation are a generic way to decorate classes/properties or methods. They have nothing to do with compile time or run time. However, the classes that use annotation to do their work can do this at compile time or at run time.

Example: Spring AOP. For AOP, you decorate your classes with @Aspect annotations and depending on type of proxy you want to create, the weaving can happen compile time doing byte code modification or at run time. You can find more details via ajc compiler/JDK dynamic proxies/CGLib proxies.

hellojava
  • 4,904
  • 9
  • 30
  • 38