Sunday, January 1, 2012

SCJP

package test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;

// Annotation
@MyAnnotationType(version = 1.1, clients = { "Programs" })
class Main
{
    public void method1()
    {
    }
}

public class Main1 extends Main
{
    public static void main(String[] args)
    {
        Main1 m = new Main1();
        m.method1();
    }

    // Annotation provided by java
    // annotation tells the compiler to suppress specific warnings that it would otherwise generate.
    @SuppressWarnings({ "unchecked", "deprecation" })
    // annotation informs the compiler that the element is meant to override an element declared in a superclass.
    @Override
    // annotation indicates that the marked element is deprecated and should no longer be used.
    @Deprecated
    public void method1()
    {
        List l;
    }
}

// The filename should be named same as public class or interface
// meta annotation - annotation to annotation
@Documented
@Inherited
// public enum ElementType {
//    TYPE,         // Class, interface, or enum (but not annotation)
//    FIELD,        // Field (including enumerated values)
//    METHOD,       // Method (does not include constructors)
//    PARAMETER,        // Method parameter
//    CONSTRUCTOR,      // Constructor
//    LOCAL_VARIABLE,   // Local variable or catch clause
//    ANNOTATION_TYPE,  // Annotation Types (meta-annotations)
//    PACKAGE       // Java package
//  }
@Target({ ElementType.FIELD, ElementType.TYPE })
// public enum RetentionPolicy {
//    SOURCE,       // Annotation is discarded by the compiler
//    CLASS,        // Annotation is stored in the class file, but ignored by the VM
//    RUNTIME       // Annotation is stored in the class file and read by the VM
//  }
@Retention(RetentionPolicy.SOURCE)
// simple annotation type
@interface MyAnnotationType {

    public enum MyEnum {
        ONE, TWO, THREE
    };

    //Annotation declaration should start with an 'at' sign like @, following with an interface keyword, following with the annotation name.
    //Method declarations should not have any parameters.
    //Method declarations should not have any throws clauses.
    //Return types of the method should be one of the following: primitives,String,Class,enum,array of the above types

    // string type
    String author() default "n/a";

    // array type
    String[] clients() default { "Programs", "GUI", "TestJig" };

    // enum
    MyEnum update() default MyEnum.ONE;

    // double type
    double version() default 1.0;

}
References:
http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html
http://www.developer.com/java/other/article.php/3556176/An-Introduction-to-Java-Annotations.htm
http://technicalmumbojumbo.wordpress.com/2008/01/13/java-custom-annotations/
http://www.ibm.com/developerworks/library/j-annotate2/index.html

No comments: