Posts

Showing posts from January, 2009

Java Annotation

Annotations on Annotations(Meta Annotation) J2SE 5.0 provides four annotations in the java.lang.annotation package that are used only when writing annotations:     @Documented—Whether to put the annotation in Javadocs     @Retention—When the annotation is needed     @Target—Places the annotation can go     @Inherited—Whether subclasses get the annotation These four annotations are used while creating custom Annotations and are called Meta Annotations. The Target annotation The target annotation indicates the targeted elements of a class in which the annotation type will be applicable. It contains the following enumerated types as its value:      @Target(ElementType.TYPE)—can be applied to any element of a class      @Target(ElementType.FIELD)—can be applied to a field or property      @Target(ElementType.METHOD)—can be applied to a method level annotation      @Target(ElementType.PARAMETER)—can be applied to the parameters of a method      @Target(ElementType.CONSTRU

Java Annotation

Annotation in Java is there to make programmers life a bit easy. Annotation-based development relieves Java developers from the pain of cumbersome configuration. Advantages of using Annotations Annotations are attached to Classes and can be processed via reflection. It is much easier to see the Annotations as they are defined in Java code and no need to refer external configurations (like XML). Compiler will do static type checking for Annotation uses. Compile time the proper uses of annotation will be done. Here are some rules-of-thumb when defining an annotation type: 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 The following shows example of decel