Java Annotation之应用篇 – 运行期动态解析annotation (3)

我们在前2篇文章中:

分别介绍了annotation的基本概念,以及怎么样自定义annotation
本文介绍怎么在运行期(runtime)动态解析annotation
上面我们介绍过,annotation只是附加在代码里的附加信息,annotation本身不会对代码的执行产生任何影响。

这样说来,annotation到底能起什么作用呢?
1    编译工具或其他工具可以根据被附加在代码里的annotation信息自动生成配置文件或文档等外部文件。
比如,sun公司就提供了apt(Annotation Processing Tool工具,apt工具是一个可以处理annotation的命令行工具,apt提供了在编译期针对源代码级别的解析,并可以在解析时生成新的源代码和其他文件,同时还可以对生成的源代码进行编译。
2
    其他程序可以在运行时动态解析将要被执行的程序里的annotation信息,并根据被附加的annotation信息来执行不同的操作。
比如,EJB3规范就比较广泛地使用了annotation特性。比如只要在POJOclass注明@Stateless注释,EJB容器便会根据此annotation把该POJO注册为无状态的Session BeanEJB3使用了annotation大大地简化了EJB的开发和配置过程。我们会在其他文章里专门介绍EJB Annotation的原理与使用方法,这里不做详述。

本文通过一个简单地例子来说明怎么在运行期动态解析annotationApt工具的使用我们会在近期其他文章里对其加以介绍。

比如,我们定义了MyAnnotation3注释:
MyAnnotation3.java

  

package com.test.annotation;
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation3 {
 public String value();
 public String[] multiValues();
 int number() default 0;
}

 

上面定义了一个名为MyAnnotation3的注释。
我们再定义一个GetMyAnnotation类,该类使用了MyAnnotation3注释:
GetMyAnnotation.java

 

 

package com.test.annotation.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.ejb.EJB;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.test.annotation.MyAnnotation3;

// 为GetMyAnnotation类附加MyAnnotation3 注释   
@MyAnnotation3(value = "Class GetMyAnnotation", multiValues = { "1", "2" })
public class GetMyAnnotation {

	// 为testField1属性附加MyAnnotation3 注释
	@MyAnnotation3(value = "call testField1", multiValues = { "1" }, number = 1)
	private String testField1;

	// 为testMethod1方法附加MyAnnotation3 注释
	@MyAnnotation3(value = "call testMethod1", multiValues = { "1", "2" }, number = 1)
	public void testMethod1() {
	}

	@Deprecated
	@MyAnnotation3(value = "call testMethod2", multiValues = { "3", "4", "5" })
	public void testMethod2() {
	}
}

  

上面的例子GetMyAnnotation非常简单,里面没有任何功能,但是分别为类(class),属性(field),方法(method)申明(附加)了MyAnnotation3 注释。
下面我们用程序TestMyAnnotation3GetMyAnnotationMyAnnotation3注释进行解析。
运行时解析annotation
TestMyAnnotation3.java

 

 

 

public class TestMyAnnotation3 {

	public static void main(String[] args) {

		System.out.println("--Class Annotations--");

		if (GetMyAnnotation.class.isAnnotationPresent(MyAnnotation3.class)) {
			System.out.println("[GetMyAnnotation].annotation:");

			MyAnnotation3 classAnnotation = GetMyAnnotation.class
					.getAnnotation(MyAnnotation3.class);
			printMyAnnotation3(classAnnotation);
		}
		System.out.println("--Fields Annotations--");

		Field[] fields = GetMyAnnotation.class.getDeclaredFields();

		for (Field field : fields) {

			if (field.isAnnotationPresent(MyAnnotation3.class)) {

				System.out.println("[GetMyAnnotation." + field.getName()
						+ "].annotation:");

				MyAnnotation3 fieldAnnotation = field
						.getAnnotation(MyAnnotation3.class);

				printMyAnnotation3(fieldAnnotation);
			}
		}
		System.out.println("--Methods Annotations--");

		Method[] methods = GetMyAnnotation.class.getDeclaredMethods();

		for (Method method : methods) {

			System.out.println("[GetMyAnnotation." + method.getName()
					+ "].annotation:");

			if (method.isAnnotationPresent(MyAnnotation3.class)) {

				MyAnnotation3 methodAnnotation = method
						.getAnnotation(MyAnnotation3.class);

				printMyAnnotation3(methodAnnotation);
			}
		}
	}

	private static void printMyAnnotation3(MyAnnotation3 annotation3) {

		if (annotation3 == null) {
			return;
		}
		System.out.println("{value=" + annotation3.value());
		String multiValues = "";

		for (String value : annotation3.multiValues()) {
			multiValues += "," + value;
		}

		System.out.println("multiValues=" + multiValues);

		System.out.println("number=" + annotation3.number() + "}");
	}
}

 

 

输出:

 

 

--Class Annotations--
[GetMyAnnotation].annotation:
{value=Class GetMyAnnotation
multiValues=,1,2
number=0}
--Fields Annotations--
[GetMyAnnotation.testField1].annotation:
{value=call testField1
multiValues=,1
number=1}
--Methods Annotations--
[GetMyAnnotation.testMethod1].annotation:
{value=call testMethod1
multiValues=,1,2
number=1}
[GetMyAnnotation.testMethod2].annotation:
{value=call testMethod2
multiValues=,3,4,5
number=0}

JDK1.5以后的版本提供的跟annotation有关的接口:

 

 interface java.lang.reflect.AnnotatedElement {   

     boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);   

    <T extends Annotation> T getAnnotation(Class<T> annotationClass);   

    Annotation[] getAnnotations();   

    Annotation[] getDeclaredAnnotations();   

 }   

 

 

该接口主要用来取得附加在类(class),构造方法(constructor),属性(field),方法(method),包(package)上的annotation信息。
JDK1.5
与此有关的几个类都实现了AnnotatedElement接口:  

 

所以可以利用反射(reflection)功能在程序里动态解析附加的annotation信息。
总结:
本文通过举例简单地说明了怎么动态解析annotation,大家可以举一反三,利用Javaannotation特性,完成更复杂功能等。    

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值