Showing posts with label retentionpolicy. Show all posts
Showing posts with label retentionpolicy. Show all posts

Saturday, August 3

All about annotations in java

                      All about  annotations in java 

What is annotation ?

Annotation is introduced in java 1.5. It is used to apply meta information on source code in source code file itself

What is the purpose of annotation ?

Annotation help in providing meta information to code source file at as per desired Retention 
policy

What is retention policy?

Retention policy defines at what time do we want to apply the meta information in our source code. Java provides three different levels of applying meta information.
1. SOURCE
2. CLASS
3. RUNTIME 

Source : Meta information is applied at in source code only. At compile time and runtime it won't be available 

Example : @override , @suppress warning

CLASS : Meta information is applied in .class file i.e. bytecode 


RUNTIME : Meta information is applied at runtime .

Example : @deprecated

What is the advantage? 

It helps binding meta information with source code itself with required control . 
It removes dependency on xml file . Annotation replaces XML. Yes , Annotation can be used to apply the same meta information on source what earlier used to be done by XML 

Are there some in built annotation in java?

Yes there are For Example:

 : @Suppress 
   @override
   @deprecated

What is basic syntax of annotation ? 

at minimum it requires combination of @ and interface 

For example 

public @interface myAnnot{
}

Here myAnnot is the annotation defined. This much is sufficient to define a well qualified complete annotation.

How do we apply an annotation?

If you have seen some code written java 1.5 onwards you might have seen something like 


@Override
void mySuperMethod() { ... }


What is @override doing here. This is an annotation saying that this method is overridden. And if any override rule is violated It helps compiler is raising compilation error. If annotation won't have been there compiler would have completely ignored the error and we would have been in big time trouble at runtime. Identifying error at runtime can be very costly and very tedious task to identify.


Similarly 

@SuppressWarnings(value = "unchecked")
void myMethod() { ... }

@suppress annotation is basically instructing compiler to suppress the unchecked warning . After applying this compiler won't highlight any unchecked code warning.

Can we write our own annotation?
Absolutely. myAnnot above is an example of user defined annotation. 

Below is another example of annotation . We generally use this to with our source file.



@interface CodeInfor{
   String author();
   String date();
   String lastModified() default "mycompany";
   String lastModifiedBy() 
  }

What are the rules that are applied on annotations?

 annotation is defined using interface as keyword preceding with @ special character.


Can annotation be applied on another annotation?

Absolutely. @Retention is an example of that . This is discussed above

other useful annotations that are applied on other annotations are : 

@Documented ,
 @Target,@Inherited , 
@Repeatable 

@Target defines at what type this annotation will be applied for example :

constructor 
field
method
parameter

With that we can define at what part of the code a specific annotation should be applied.

Java provides extensive API to identify what annotations are applied at a specific class , method , constructor, variable or parameter.