java:从spring-core移植的注解(annotation)扫描工具模组common-annotutils(适用JDK 1.7)

AnnotationUtils

spring-core的部件组装基本原理是基于注解(annotation),通过扫描类、方法、成员上定义的注解来决定组装逻辑。
spring-core将注解扫描框架封装为一个工具类:org.springframework.core.annotation.AnnotationUtils,使用这个工具类就可以从一个复杂类型或方法中获取指定的注解信息,大概就是这样:

	AppConstantType annot = AnnotationUtils.findAnnotation(ClassB.class,AppConstantType.class);
	SyStem.out.printf("%s\n",annot);

这个注解扫描框架对于希望建立以注解组装机制的应用就很有用。但它是深度集成在spring-core代码中的。如果希望不依赖spring框架使用这个工具,只能自己想办法将org.springframework.core.annotation包下的代码抽取出来使用,而我的项目需要在JDK 1.7环境下使用,我现在现在使用的spring-core 5.x版本最低要求JDK 1.8。
所以org.springframework.core.annotation包下的代码大量使用了JDK 1.8的新特性:lamda表达、steam等。
所以对于我来说不仅要把org.springframework.core.annotation包下的代码抽取出来,还要对代码进行大量的重构,以适应JDK 1.7 版本。
花了差不多一周时间,总算完成了移植工作,为了验证代码重构的正确性,我同时还将org.springframework.core.annotation包对应的单元测试代码(spring-core/src/test/java/org/springframework/core/annotation)也同步移植出来。最终跑通了所有的单元测试。

common-annotutils

为了方便使用,我将移植出来的spring-core将注解扫描框架封装为jar包并发布到maven中央仓库。
依赖引用方式如下:

maven:

		<dependency>
			<groupId>com.gitee.l0km</groupId>
			<artifactId>common-annotutils</artifactId>
			<version>2.9.0</version>
		</dependency>

gradle:

implementation group: 'com.gitee.l0km', name: 'common-annotutils', version: '2.9.0'

调用示例

import static org.junit.Assert.*;

import java.lang.reflect.Method;

import static net.gdface.utils.SimpleLog.log;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.gitee.l0km.common.annotations.AppConstantField;
import com.gitee.l0km.common.annotations.AppConstantType;
import com.gitee.l0km.common.annotations.CellLogTag;
import com.gitee.l0km.common.annotations.TestLogTag;
import com.google.common.collect.Multimap;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class AnnotationTest {

	@Test
	public void test2AnnotationUtils() {
		try {
			{
				AppConstantType annot = AnnotationUtils.findAnnotation(ClassB.class,AppConstantType.class);
				log("{}",annot);
			}
			{
				AppConstantType annot = AnnotationUtils.findAnnotation(In2.class,AppConstantType.class);
				log("{}",annot);
			}
			{
				AppConstantField annot = AnnotationUtils.findAnnotation(ClassB.class.getMethod("m0"),AppConstantField.class);
				log("{}",annot);
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}
	@Test
	public void test3GetAnnotation() {
		try {
			{
				Method method = ClassB.class.getMethod("m0");
				TestLogTag annot = AnnotationUtils.findAnnotation(method,TestLogTag.class);
				log("{}",annot);
				CellLogTag cellLogTag= AnnotationUtils.getAnnotation(annot, CellLogTag.class);
				log("{}",cellLogTag);
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}
	@Test
	public void test4GetAllAnnotationAttributes() {
		try {
			{
				Method method = ClassA.class.getMethod("m0");
				Multimap<String, Object> attrs = AnnotatedElementUtils.getAllAnnotationAttributes(method,TestLogTag.class.getName());
				log("{}",attrs);
			}
			{
				Method method = ClassB.class.getMethod("m0");
				Multimap<String, Object> attrs = AnnotatedElementUtils.getAllAnnotationAttributes(method,TestLogTag.class.getName());
				log("{}",attrs);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}
	@AppConstantType
	public static class ClassA {
		@AppConstantField
		@TestLogTag(TestEamLogTag.DT_ADD_LOG)
		public void m0() {};
	}
	public static class ClassB extends ClassA {
		@Override
		@TestLogTag(value=TestEamLogTag.DT_ADD_DEV,tag="hello")
		public void m0() {};
		
	}
	@AppConstantType
	public static interface In1{}
	public static interface In2 extends In1{}
	public static class ClassC implements In1{}

}

完整代码

完整代码参见码云仓库:
https://gitee.com/l0km/casban/tree/master/casban-annotutils
单元测试代码位置:
https://gitee.com/l0km/casban/tree/master/casban-annotutils-test

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值