Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 523
Gunwant Avatar asked Mar 25 '11 12:03

Gunwant


People also ask

Are annotations compiled?

Annotations provide information to a program at compile time or at runtime based on which the program can take further action. An annotation processor processes these annotations at compile time or runtime to provide functionality such as code generation, error checking, etc. The java.

How are annotations compiled Java?

The annotation processing is done in multiple rounds. Each round starts with the compiler searching for the annotations in the source files and choosing the annotation processors suited for these annotations. Each annotation processor, in turn, is called on the corresponding sources.

What is annotation and how it works?

Annotation can be: A systematic summary of the text that you create within the document. A key tool for close reading that helps you uncover patterns, notice important words, and identify main points. An active learning strategy that improves comprehension and retention of information.


2 Answers

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.

like image 59
Peter Lawrey Avatar answered Nov 02 '22 07:11

Peter Lawrey


@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.

like image 39
Johan Sjöberg Avatar answered Nov 02 '22 05:11

Johan Sjöberg