Sei sulla pagina 1di 3

Annotations In Java

an overwiev
Annotation is code about the code, that is metadata about the program itself. In other words, organized data about the code, embedded within the code itself. It can be parsed by the compiler, annotation processing tools and can also be made available at runtime too. Annotations are not a replacement for Documentation comments (they have their own other uses), but in many of the cases (especially in the cases where we need similar structured comments) they are preferred over comments. The reason is that annotations are types in Java - just like any other user defined data type. And hence they can be used once they have been defined. Annotations in Java are actually a form of interfaces only and hence the definition of an annotation type resembles to that of an interface. The keyword 'interface' is preceded by the symbol '@' in an annotation definition. Annotations can be used in three ways: Assisting visual inspection of code Custom annotation parsing source tools (such as javadoc*****) Via reflection 3 introduced annotations by default by the JDK: @Deprecated is used to tell the developer that a function shouldn't be used anymore. It generally means that in a later version of the code, that function will go away. When a developer does use this function, the compiler will warn the developer about this. @SuppressWarnings Tells the compiler that even though it might be inclined to warn the developer about what they are doing in the next line, it really is ok and the developer knows about the issue @Override tells the compiler that you intend to have a function override the parent's version of it. If there is a typo, the compiler knows there's a problem and will throw an error. It also tells the developer that this code is overriding a parent's version. Easier to read than a nonexistent comment to the same effect.

At the same time, there is also opportunity to create new annotations for special cases, depends on developer choices.

Example We can create a annotation using interface as follow: public @interface test { // } In the next example, we can see the possible features about annotations: import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Inherited public @interface Sample { String writtenBy() default "unknown"; String lastChanged() default "unknown"; } @Documented appearsInJavadoc(); This allows the annotation to be noted in the Javadoc thats eventually created. @Inherited appearsInChildren(); When a class or method is annotated with @Inherited, Children of that class will also have this annotation (otherwise they wont) @Retention This meta annotation denotes the level till which this annotation will be carried. When an annotation type is annotated with meta annotation Retention, RetentionPolicy has three possible values:

Class When the annotation value is given as class then this annotation will be compiled and included in the class file. Runtime The value name itself says, when the retention value is Runtime this annotation will be available in JVM at runtime. We can write custom code using reflection package and parse the annotation. I have give an example below. Source This annotation will be removed at compile time and will not be available at compiled class.

@Target This marks an annotatation with limitation on how it can be used. It takes a list of ElementType enums to say how it can be used. ElementType.ANNOTATION_TYPE, CONSTRUCTOR, FIELD,LOCAL_VARIABLE,METHOD, PACKAGE, PARAMETER and TYPE.

Recommended to read before using Annotation:


http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html http://javapapers.com/core-java/java-annotations/ http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/package-summary.html http://www.javabeat.net/2007/08/annotations-in-java-5-0/

Potrebbero piacerti anche