Java Tutorial/Language/Annotations Reflection — различия между версиями

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

Текущая версия на 05:06, 1 июня 2010

Getting all annotations for a method

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// A simple annotation type.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
  String stringValue();
  int intValue();
}
@Retention(RetentionPolicy.RUNTIME)
@interface What {
  String description();
}
@What(description = "An annotation test class")
@MyAnnotation(stringValue = "for class", intValue = 100)
public class MainClass {
  // Annotate a method.
  @What(description = "An annotation test method")
  @MyAnnotation(stringValue = "Annotation Example", intValue = 100)
  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) {
    }
  }
}



All annotations for myMeth:
@What(description=An annotation test method)
@MyAnnotation(stringValue=Annotation Example, intValue=100)


Obtaining All Annotations: getAnnotations( )

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
// A simple annotation type.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
  String stringValue();
  int intValue();
}
@Retention(RetentionPolicy.RUNTIME)
@interface What {
  String description();
}
@What(description = "An annotation test class")
@MyAnnotation(stringValue = "for class", intValue = 100)
public class MainClass {
  // Annotate a method.
  @What(description = "An annotation test method")
  @MyAnnotation(stringValue = "Annotation Example", intValue = 100)
  public static void myMethod(String str, int i) {
  }
  public static void main(String[] arg) {
    try {
      MainClass ob = new MainClass();
      Annotation[] annos = ob.getClass().getAnnotations();
      
      System.out.println("All annotations for Meta2:");
      for(Annotation a : annos)
        System.out.println(a);
      
    } catch (Exception exc) {
    }
  }
}



All annotations for Meta2:
@MyAnnotation(stringValue=for class, intValue=100)
@What(description=An annotation test class)


Obtaining Annotations at Run Time by Use of Reflection

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// A simple annotation type.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
  String stringValue();
  int intValue();
}
public class MainClass {
  // Annotate a method.
  @MyAnnotation(stringValue = "Annotation Example", intValue = 100)
  public static void myMethod() {
  }
  public static void main(String[] a) {
    try {
      MainClass ob = new MainClass();
      Class c = ob.getClass();
      Method m = c.getMethod("myMethod");
      MyAnnotation anno = m.getAnnotation(MyAnnotation.class);
      System.out.println(anno.stringValue() + " " + anno.intValue());
    } catch (NoSuchMethodException exc) {
      System.out.println("Method Not Found.");
    }
  }
}



Annotation Example 100


Reflection: getMethod( ) with parameters

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// A simple annotation type.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
  String stringValue();
  int intValue();
}
public class MainClass {
  // Annotate a method.
  @MyAnnotation(stringValue = "Annotation Example", intValue = 100)
  public static void myMethod(String str, int i) {
  }
  public static void main(String[] a) {
    try {
      MainClass ob = new MainClass();
      Class c = ob.getClass();
      Method m = c.getMethod("myMethod", String.class, int.class);
      MyAnnotation anno = m.getAnnotation(MyAnnotation.class);
      System.out.println(anno.stringValue() + " " + anno.intValue());
    } catch (NoSuchMethodException exc) {
      System.out.println("Method Not Found.");
    }
  }
}



Annotation Example 100