Monday, June 15, 2009

Tattoo Of Footprint On Leg



Level: Intermediate

annotations in Java, are trademarks that are made part of the program, which have no direct effect on it but they are thoughtful in execution, ie can be used by a container at the time of implementation.


Annotations are a special type of switch and can be used with other modifiers such as public, static, private etc ... Say annotations are one way to implement an interface, without the kind you do directly. For example, you create a Dog class, and then you note to say that is an animal, in this way, in implementing the program using reflection is possible to know that any object of class Dog is an animal. Ie we are saying that dog is an animal and serves a similar function to an interface.

 
@ Animal

public class Dog {/ / Some good dog class method}





Define an annotation is very simple, it is the same way as an interface and puts a " @ "at the beginning of the word interface, and methods will contain elements of the annotation, and can only return primitive types, String and Enums.




 
@ interface Animal {public boolean
vertebrate ();}



With the notation presented above, we score some sort and define whether the animal that we are noting is vertebrate or invertebrate

 
@ Animal (vertebrate = true) public class Dog {


}



This will ensure the implementation of the program can tell if an object of class Dog is an animal, and what kind of animal, we can also specify that serves only the annotation to annotate classes, we can not have animal-like methods. To do this we scored our touchdown. Then our notation becomes:

 
package annotations;
import
java.lang.annotation.Retention;
java.lang.annotation.Target import, import
java.lang.annotation.ElementType;
import java.lang. annotation.RetentionPolicy;



@ Retention (RetentionPolicy.RUNTIME)
@ Target (ElementType.TYPE)
Animal {public @ interface

vertebrate boolean ();}





Thus we specify that the scoring only used to define types. Now how to use it in the execution:

 


package annotations;

import java.util.ArrayList;
import java.util.List;

public class Main {

@ SuppressWarnings ("unchecked") public
static void main (String [] args) {

/ / Create a List List
obj = new ArrayList ();

/ / add 2 Dogs
obj.add (new Dog ());
obj.add (new Dog ());

/ / add 1
obj.add String ("I am a String");

System.out.println ("Number of animals in the list:" + contarAnimales (obj));



} public static int contarAnimales (List o)
{if (o == null) return 0
;

else {System.out.println ("Starting to count");
int count = 0;
for (Object object: o) {
/ / System.out.println (object.getClass (). getAnnotation (Animal.class));
if (object.getClass (). isAnnotationPresent (Animal.class)) {
count + +;
}
}
return count;
}
}

}



Although the example is not the most orthodox, gives an example of what can make annotations, and how is the use frameworks such as JAXB or Spring 2.5 Annotated, which can make Autoalambrado, define driver, mapping request with annotations to avoid having to configure a dispatcher servlet great. Greetings

0 comments:

Post a Comment