`

Annotations Reflection 注解之反射

    博客分类:
  • Java
 
阅读更多

1.12.1 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

 1.12.2 Reflection: getMethod( ) with parameters 反射: 带参数调用getMethod()

 

 

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

 新奇的是, int.class 居然在编译器眼里是合法的!

 

1.12.3 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)
*/

 1.12.4 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)
*/

 

分享到:
评论

相关推荐

    java 通过反射获取类上注解,方法上注解,注解里的值及方法参数

    反射(Reflection)是Java的一个强大特性,允许程序在运行时检查类、接口、字段和方法等信息。本教程将深入探讨如何通过反射获取类、方法上的注解以及注解中的值和方法参数。 1. **注解的定义与使用** 注解以`@`...

    一种基于Java注解和反射机制的Excel文件读写操作方法.zip

    在Java编程中,注解(Annotations)和反射(Reflection)是两种强大的工具,它们在处理各种任务时提供了灵活性和便利性。在这个特定的场景中,我们讨论的是如何结合这两种技术来实现Excel文件的读写操作。Excel文件...

    java基础的注解和反射的相关知识点总结

    Java中的注解(Annotation)和反射(Reflection)是两种强大的工具,它们提供了程序设计的灵活性和动态性。本文将深入探讨这两个概念及其相关知识点。 **注解** 是一种元数据,它提供了在代码中附加信息的方式,...

    反射Reflection小应用

    在Java编程语言中,反射(Reflection)是一种强大的工具,它允许程序在运行时检查和操作类、接口、字段和方法的信息。通过反射,我们可以动态地创建对象、调用方法、访问字段,甚至处理私有成员,这在某些情况下非常...

    annotations-4.0.0.20110227-CR1.jar.zip

    除了直接在源代码中使用注解,Java还提供了处理注解的API,称为反射(Reflection)。通过反射,程序可以在运行时检查类、接口、字段和方法上的注解,从而实现动态行为。例如: ```java Class<?> clazz = Class.for...

    Java Reflection in Action

    6. **注解(Annotations)**:反射可以读取类、方法、字段上的注解,`getAnnotation(Class<A> annotationClass)`返回指定类型的注解,`isAnnotationPresent(Class<A> annotationClass)`则检查是否存在该注解。...

    简单注解功能

    - 反射(Reflection):在运行时,通过Class对象、Field对象、Method对象等的getAnnotations()方法获取注解信息。 5. 注解的应用场景: - 编译时验证:例如,@NonNull可以用来检查参数或变量是否为null。 - 框架...

    Reflection_and_Annotations

    在`Reflection_and_Annotations-master`这个压缩包中,可能包含了一个示例项目,演示了如何结合使用反射和注解来实现动态Dao。通过阅读和学习这个项目,你可以更深入地理解这两个Java特性在实际开发中的应用,并提升...

    java-lib-annotated-validator,使用注解的Java验证程序库.zip

    - **注解(Annotations)**:注解是Java语言中的一种元数据,它可以被编译器或JVM在运行时读取,用来提供有关代码的附加信息。在这个库中,自定义的注解用于指定验证规则。 - **反射(Reflection)**:Java反射API...

    PHP Annotations-开源

    注解(Annotations)是一种元编程技术,允许开发者在代码中嵌入特殊标记,这些标记在程序运行时可以通过反射机制被解析,用以提供额外的信息或者执行特定的任务。这一特性在很多现代编程语言中广泛使用,如Java、C#...

    Java-Reflection-Tutorial.pdf

    8. **注解(Annotations)** - 反射可以读取类、方法、字段上的注解,以便在运行时检查元数据。 9. **泛型** - 尽管泛型在编译时被擦除,反射仍然可以提供关于泛型类型信息的线索。 10. **类加载器(Class ...

    spring源代码分析:annotation支持的实现

    在Spring框架中,注解(Annotation)支持是其核心特性之一,它极大地简化了配置,提高了代码的可读性和可维护性。这篇博客"spring源代码分析:annotation支持的实现"探讨了Spring如何通过注解处理来实现组件扫描和...

    《软件开发基础(Java)》实验报告-(概述).docx

    2. **Java注解(Annotations)**: 注解是一种元数据,用于提供有关代码的附加信息,但不直接影响代码的执行。在`Sample.java`中定义了一个名为`@Sample`的自定义注解,它包含一个`value()`方法。在`Main.java`中,...

    jdk5_64.zip

    八、内省(Introspection)和反射(Reflection) 虽然反射在JDK1.2就已经存在,但JDK5对反射API进行了改进,使得在运行时动态访问和修改对象、类、接口、构造器等变得更加方便。内省则是通过反射机制获取类的元数据...

    JAVA上课笔记class_17

    ### 注解(Annotations) 注解在Java中提供了一种机制,用于向代码添加元数据,而不影响代码的执行。它们主要用于工具开发,如IDE插件、构建工具等。 - `@Deprecated`用于标记过时的元素,不推荐使用。 - `@...

    spike-DI:简单的DI实现

    **Java反射API(Reflection API)** 是Java标准库的一部分,它允许程序在运行时检查类、接口、字段和方法的信息,并能动态地创建和调用对象。在Spike-DI中,反射API被用来在运行时解析注解,识别需要注入的对象,并...

    java基础加强

    1. 反射(Reflection) 反射是Java的一项强大功能,它允许程序在运行时动态地获取类的信息(如类名、方法名、参数类型等)并调用这些方法。通过Class类、Constructor类、Method类和Field类,开发者可以访问并操作类...

    良葛格Java JDK 5.0学习笔记

    四、变量注解(Annotations) 注解是元数据的一种形式,用于提供程序信息,不直接影响程序运行。Java 5.0的注解可以用于编译器、JVM或运行时工具,如Spring框架中的@Service、@Controller等。 五、增强的for循环...

    JDK1.5,JDK1.5

    9. **内省(Introspection)**和反射(Reflection)增强 在JDK1.5中,反射API得到了扩展,增加了对泛型的支持,增强了对枚举类型和注解的访问。 10. **基于XML的配置(JSR 175)** JSR 175引入了注解处理,使得...

    JMI规范API,java元数据

    JMI规范API是建立在Java反射API(Reflection API)基础之上,提供了一种更加系统化和模块化的访问元数据的方法。Java反射API允许我们在运行时检查类、接口、字段和方法的详细信息,但JMI API则更专注于元数据的查询...

Global site tag (gtag.js) - Google Analytics