Gunwant
Gunwant

Reputation: 149

How compiler deals with annotations?

I have some questions about working of annotations in java.

If annotations cannot be converted to bytecode then where does this information go? where does the meta-data goes? How Java Reflection uses this information?

How compiler deals with annotations?

When we say,

@Override
public void doSomething(){
}

What does a java compiler do with it?

I know that it checks the method signature so that the method should be a perfectly overridden method, but how?

Upvotes: 13

Views: 6091

Answers (6)

Buhake Sindi
Buhake Sindi

Reputation: 89199

Annotations have a RetentionPolicy, which specifies if the annotation will be stored in the class file (RetentionPolicy.CLASS, RetentionPolicy.RUNTIME) or will be discarded by the compiler (RetentionPolicy.SOURCE).

The @Override method has a RetentionPolicy of RetentionPolicy.SOURCE (meaning it is discarded by the compiler and not available at runtime via Reflection) and a target of ElementType.METHOD (meaning that this method can only be annotated on method declarations only).

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

Only annotations with a RetentionPolicy of RetentionPolicy.RUNTIME can be read by Reflection.

Upvotes: 4

Ingo
Ingo

Reputation: 36339

Compiled annotations (which are ultimately constant data) live the class file and can be accesses with the appropriate methods getAnnotations() and friends.

Upvotes: -2

evilcroco
evilcroco

Reputation: 531

At first, there were 2 kinds of annotations: Runtime accessible (2 kinds) and not runtime accessible. The behavior of annotation is done by retention policy through annotation of annotation declaration :-). Example:

@Retention(RetentionPolicy.RUNTIME) // effectively visible to JVM Runtime
public @interface MethodInfo {
    String author() default "unspecified";
    String lastModification() default "unspecified"; // Format: yyyy-mm-dd
    ImplementationStatus implementationStatus();
}

JVM runtime visible annotations can be accessed through reflection during annotated code execution (or when it is loaded by the JVM). You utilize those annotations as metadata holders. Metadata can be processed by other code to do various things eg. to check annotated method implementation status before a given method is executed.

But someone must write code to use the annotation matedata through reflection! The best example is an application server (AS) that loads classes on demand from JAR files placed is some particular directory. AS can contain code that checks each loaded class for static methods annotated by the @Initialization annotation and executes those methods right away. This annotation type is defined by AS and those who create JARs and classes for AS use it during development.

Annotations that are not available during runtime are used and worked out during compilation. @Override is good example. Custom source only annotations can be used by compiler plugins or if code is compiled by other code on demand.

Upvotes: 2

Johan Sjöberg
Johan Sjöberg

Reputation: 49237

@Override is a source type annotation - used to check if a particular method follows the contract of an overriden method - hence it isn't available at runtime (and therefore logically doesn't exist in the compiled bytecode).

I know that it checks the method signature so that the method should be a perfectly overridden method, but how

In the process of reading the source files and converting them to bytecode, e.g., not using reflection at all. See The Java Programming Language Compiler - javac for additional compiler information.

Upvotes: 4

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

This one is simple. Compiler just checks the super class for method with same name and same number of parameters, their order and types. If method has @Override annotation and such method hasn't been found an error is generated.

More interesting question is how compiler deals with annotations which has mean at runtime. I assume they are added in byte-code but in some special section with meta information. So, in runtime jvm has access to the meta section. As a prove, you can always call getAnnotations() on any class object.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533870

There are three types of annotations see http://download.oracle.com/javase/6/docs/api/java/lang/annotation/RetentionPolicy.html

@Override is a special case as the compiler does additional checks.

Upvotes: 11

Related Questions