Friday, 27 June 2014

@SuppressWarnings

It is an annotation to suppress compile warnings that are generated by Compiler. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.

Example
import java.util.*;

public class SuppressEx{    
 
 public static void main(String args[]){
  byte b[] = {1,2,3,4,5};
  String s  = new String(b, 3);
 }
}

When you tries to compile the above program, compiler shows below warnings.

Note: SuppressEx.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

To suppress the warnings change the class file like below.

import java.util.*;

public class SuppressEx{    
 @SuppressWarnings("deprecation")
 public static void main(String args[]){
  byte b[] = {1,2,3,4,5};
  String s  = new String(b, 3);
 }
}

Prevoius                                                 Next                                                 Home

@Override

If a method declaration is annotated with the annotation @Override, but the method does not override or implement a method declared in a supertype, or is not override equivalent to a public method of Object, a compile-time error occurs.

Use it every time you override a method to detect bugs early.

For Example
public class SuperClass {
 public void print(){
 
 }
}

import java.lang.annotation.Annotation;

public class SubClass extends SuperClass{    
 @Override
    public void Print(){
 
 }
}

When you tries to compile the above program below error thrown.

SubClass.java:4: error: method does not override or implement a method from a supertype
        @Override
        ^
1 error

Since Print method in SubClass.java actually not overriding the method in SuperClass.java. Becaue the method Print in SubClass.java starts with Capital 'P' where as SuperClass.java starts with small 'p'.

Now change the method name in SubClass.java and recompile.

import java.lang.annotation.Annotation;

public class SubClass extends SuperClass{    
 @Override
    public void print(){
 
 }
} 




Prevoius                                                 Next                                                 Home

@Inherited

It is used to indicate that annotations on a class C corresponding to a given annotation type are inherited by subclasses of C.

Running without Inherited Annotation
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation1{
    String value();
}

@MyAnnotation1("abcd")
public class SuperClass {

}

import java.lang.annotation.Annotation;

public class SubClass extends SuperClass{    
    public static void main(String args[]){
        Class myClass = SubClass.class;
        Annotation ann[] = myClass.getAnnotations();
        for(int i=0; i< ann.length; i++){
            System.out.println(ann[i].toString());
        }
    }
}

Output prints nothing. Now try to add @Inherited Annotation to MyAnnotation1 and re compile and run the SubClass.java.

Running with Inherited Annotation 
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation1{
    String value();
} 


Output

@MyAnnotation1(value=abcd)




Prevoius                                                 Next                                                 Home

RetentionPolicy.RUNTIME

RetentionPolicy.RUNTIME: The marked annotation is retained by the JVM so it can be used by the runtime environment.

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotation1{
    String value();
}

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.CLASS)
@interface MyAnnotation2{
    String value();
}

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    String value();
}

import java.lang.annotation.*;

@MyAnnotation1("Using MyAnnotation1")
@MyAnnotation2("Using MyAnnotation2")
@MyAnnotation3("Using MyAnnotation3")
public class RetainEx {
    public static void main(String args[]){
        Class myClass = RetainEx.class;
        Annotation ann[] = myClass.getDeclaredAnnotations();
        
        for(int i=0; i< ann.length; i++){
            System.out.println(ann[i].toString());
        }
    }
}

Output
@annotations.MyAnnotation3(value=Using MyAnnotation3)




Prevoius                                                 Next                                                 Home

RetentionPolicy.CLASS

RetentionPolicy.CLASS : The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM). Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.CLASS)
@interface MyAnnotation2{
    String value();
}



Prevoius                                                 Next                                                 Home

RetentionPolicy.SOURCE

RetentionPolicy.SOURCE: The marked annotation is retained only in the source level and is ignored by the compiler. i.e. these are discarded during the compilation. These annotations don't make any sense after the compilation, so they aren't written to the bytecode.

Things like @SuppressWarnings, @Override are annotations used by the compiler not needed at runtime. For those RetentionPolicy.SOURCE would make sense.

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotation1{
    String value();
}



Prevoius                                                 Next                                                 Home

@Retention

@Retention annotation specifies how the marked annotation is stored.
  1. RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
  2. RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
  3. RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.

Prevoius                                                 Next                                                 Home