【转】java反射--注解
【译】8. Java反射——注解
翻译原文地址:http://tutorials.jenkov.com/java-reflection/annotations.html
=====================================================================================
使用Java反射机制,在运行时你可以访问到Java类中所附属的一些注解。下面是本文所涵盖的主题列表:
- What are Java Annotations? (什么是Java注解)
- Class Annotations (类注解)
- Method Annotations (方法注解)
- Parameter Annotations (参数注解)
- Field Annotations (字段注解)
=====================================================================================
- What are Java Annotations?
注解是Java 5的一个新特性。注解是一种可以写到代码中的注释或者元数据。这些注解可以在编译时由预编译工具进行处理,或者在运行时通过Java反射处理。下面是一个类注解的例子:
@MyAnnotation(name="someName", value = "Hello World") public class TheClass { }
类TheClass有一个注解@MyAnnotation。定义注解就和定义接口一样。下面是一个定义注解MyAnnotation的例子:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { public String name(); public String value(); }
在interface之前加一个@符号来表示它是一个注解。一旦你定义了一个注解之后,你可以在代码中用它,和之前的例子一样。
在注解的定义中,@Retention(RetentionPolicy.RUNTIME)和@Target(ElementType.TYPE)这两个注解指定了如何去使用它们。
@Retention(RetentionPolicy.RUNTIME)表示该注解可以在运行时通过Java反射访问。如果你没有设置该指令,该注解在运行时就不会被保存,那么也就不能通过Java反射来使用。
@Target(ElementType.TYPE)表示该注解只能被用于类型上(如类、接口)。你也可以指定METHOD或者FIELD,或者你也可以省略不写,即该注解可以被用在类、方法和字段上。
关于Java注解更详细的介绍在Java Annotation tutorial中。
=====================================================================================
- Class Annotations
你可以在运行时访问类、方法或字段的注解。下面是一个访问类注解的例子:
Class aClass = TheClass.class; Annotation[] annotations = aClass.getAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); System.out.println("value: " + myAnnotation.value()); } }
你可以像这样访问一个给定类的注解:
Class aClass = TheClass.class; Annotation annotation = aClass.getAnnotation(MyAnnotation.class); if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); System.out.println("value: " + myAnnotation.value()); }
=====================================================================================
- Method Annotations
下面是一个注解用在方法上的例子:
public class TheClass { @MyAnnotation(name="someName", value = "Hello World") public void doSomething(){} }
你可以像这样访问一个方法的注解:
Method method = ... //obtain method object Annotation[] annotations = method.getDeclaredAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); System.out.println("value: " + myAnnotation.value()); } }
你也可以像这样访问一个给定方法的注解:
Method method = ... // obtain method object Annotation annotation = method.getAnnotation(MyAnnotation.class); if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); System.out.println("value: " + myAnnotation.value()); }
=====================================================================================
- Parameter Annotations
也可以在方法参数的声明上添加注解。下面是一个例子:
public class TheClass { public static void doSomethingElse( @MyAnnotation(name="aName", value="aValue") String parameter){ } }
你可以像这样访问一个Method对象的参数注解:
Method method = ... //obtain method object Annotation[][] parameterAnnotations = method.getParameterAnnotations(); Class[] parameterTypes = method.getParameterTypes(); int i=0; for(Annotation[] annotations : parameterAnnotations){ Class parameterType = parameterTypes[i++]; for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("param: " + parameterType.getName()); System.out.println("name : " + myAnnotation.name()); System.out.println("value: " + myAnnotation.value()); } } }
注意Method.getparameterAnnotations()方法如何返回一个二维的Annotation数组,该数组中包含每一个方法的参数注解。
=====================================================================================
- Field Annotations
下面是一个字段注解的例子:
public class TheClass { @MyAnnotation(name="someName", value = "Hello World") public String myField = null; }
你可以像这样访问字段的注解:
Field field = ... //obtain field object Annotation[] annotations = field.getDeclaredAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); System.out.println("value: " + myAnnotation.value()); } }
你也可以这样访问一个给定字段的注解:
Field field = ... // obtain method object Annotation annotation = field.getAnnotation(MyAnnotation.class); if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); System.out.println("value: " + myAnnotation.value()); }
========================================================================================
- 目录
这是一个本教程到目前为止涵盖的主题列表: