Java Tutorial/Language/Annotations Types

Материал из Java эксперт
Перейти к: навигация, поиск

A marker annotation

   <source lang="java">

import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MyMarker { } class Marker {

 @MyMarker
 public static void myMeth() {
   Marker ob = new Marker();
   try {
     Method m = ob.getClass().getMethod("myMeth");
     if (m.isAnnotationPresent(MyMarker.class))
       System.out.println("MyMarker is present.");
   } catch (NoSuchMethodException exc) {
     System.out.println("Method Not Found.");
   }
 }
 public static void main(String args[]) {
   myMeth();
 }

}</source>





Annotations and Annotation Types

Annotations are notes in Java programs to instruct the Java compiler to do something. Java provides three standard annotations and four standard meta-annotations.

  1. An annotation type is a special interface type.
  2. An annotation is an instance of an annotation type.
  3. An annotation type has a name and members.
  4. The information contained in an annotation takes the form of key/value pairs.
  5. There can be zero or multiple pairs and each key has a specific type.
  6. It can be a String, int, or other Java types.
  7. Annotation types with no key/value pairs are called marker annotation types.
  8. Those with one key/value pair are referred to single-value annotation types.
  1. There are three annotation types in Java 5: Deprecated, Override, and Suppress Warnings.
  2. There are four other annotation types that are part of the java.lang.annotation package: Documented, Inherited, Retention, and Target.
  3. These four annotation types are used to annotate annotations,


Creates and uses a single-member annotation

   <source lang="java">

import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME) @interface MySingle {

 int value(); // this variable name must be value

} class Single {

 @MySingle(100)
 public static void myMeth() {
   Single ob = new Single();
   try {
     Method m = ob.getClass().getMethod("myMeth");
     MySingle anno = m.getAnnotation(MySingle.class);
     System.out.println(anno.value()); // displays 100
   } catch (NoSuchMethodException exc) {
     System.out.println("Method Not Found.");
   }
 }
 public static void main(String args[]) {
   myMeth();
 }

}</source>





Marker Annotations

  1. Marker Annotations are used to mark a declaration.
  2. A marker annotation is a special kind of annotation.
  3. A marker annotation contains no members.
  4. Using isAnnotationPresent( ) to determine if the marker is present.



   <source lang="java">

import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { } @MyAnnotation public class MainClass {

 // Annotate a method.
 @MyAnnotation()
 public static void myMethod() {
 }
 public static void main(String[] arg) {
   try {
     MainClass ob = new MainClass();
     Method m = ob.getClass( ).getMethod("myMethod");
     Annotation[] annos = m.getAnnotations();
     System.out.println("All annotations for myMeth:");
     for(Annotation a : annos)
     System.out.println(a);
   } catch (Exception exc) {
   }
 }

}</source>



All annotations for myMeth:
@MyAnnotation()


Single-Member Annotations

  1. Single-Member Annotations contains only one member.
  2. Single-Member Annotations allow a shorthand form of specifying the value.
  3. The name of the member must be value.



   <source lang="java">

import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation {

 int value();

} @MyAnnotation(102) public class MainClass {

 // Annotate a method.
 @MyAnnotation(101)
 public static void myMethod() {
 }
 public static void main(String[] arg) {
   try {
     MainClass ob = new MainClass();
     Method m = ob.getClass( ).getMethod("myMethod");
     Annotation[] annos = m.getAnnotations();
     System.out.println("All annotations for myMeth:");
     for(Annotation a : annos)
     System.out.println(a);
   } catch (Exception exc) {
   }
 }

}</source>



All annotations for myMeth:
@MyAnnotation(value=101)


Some Restrictions

  1. No annotation can inherit another.
  2. All methods declared by an annotation must be without parameters.
  3. Annotations cannot be generic.
  4. They cannot specify a throws clause.

They must return one of the following:



   <source lang="java">

A simple type, such as int or double,

         An object of type String or Class
         An enum type
         Another annotation type
         An array of one of the preceding types</source>
   
  
 
  



The Annotation Interface

  1. An annotation type is a Java interface.
  2. All annotation types are subinterfaces of the java.lang.annotation.Annotation interface.
  3. It has one method, annotation Type, that returns an java.lang.Class object.



   <source lang="java">

java.lang.Class<? extends Annotation> annotationType()</source>





The single-value syntax

Using the single-value syntax when applying an annotation that has other members, if other members all have default values.



   <source lang="java">

@interface SomeAnno {

 int value();
 int xyz() default 0;

}

@SomeAnno(88)</source>



All annotations for myMeth:
@MyAnnotation(defaultValue=100, value=101)


Using Default Values

  1. Annotation default values is used if no value is specified.
  2. A default value is specified by adding a default clause.
  3. Default value must be of a type compatible with type.

Here is @MyAnnotation rewritten to include default values:



   <source lang="java">

@Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation {

 String stringValue() default "defaultString";
 int intValue() default 101;

}</source>



All annotations for myMeth:
@MyAnnotation(intValue=100, stringValue=defaultString)