ARE4j 是一个用于Java的通用Annotation解析引擎。
1. ARE4j提供了一个通用的引擎来解析Annotation;可以用于解析任何类型的自定义annotation;
2. 之所以设计为引擎,而不是框架或者容器,因为这样,可以尽可能少的减少对现有代码的影响,如果你的项目已经很好的应用的工厂模式,那么只需简单的替换或者重写Factory类即可开始使用ARE4j来解析代码中的Annotation;
3.ARE4j只依赖于javassist,这样不会对现有项目带来任何影响;
4.ARE4j非常简单易用;
Scannotation 和 Jandex是另外两个Annotation工具。Scannotation可以在运行时扫描你的Jar和Classpath,并将index存储于内存中;Jandex在编译时刻扫描代码中annotation,并将index存储于文件中。Scannotation和Jandex可以用来发现annotation,并能在运行时提高annotation的索引速度。而ARE4j不同,它是在运行时对Class中的Annotation进行解析,从而实现设计人员对于Annotation所要实现的逻辑。
使用ARE4j是非常简单的,只需要认识Engine类以及几个为了实现Resolver类需要了解的几个接口。
Engine 是ARE4j最核心的类,需要通过Engine类来完成对ARE4j的各种调用。Engine类完成一下功能:
1. register/unregister annotation resolver class
2. Check if annotation reasonable
3. Create proxy object by javassist
4. Cache annotations while parsing class first time
通过调用 Engine.newProxyInstance(Class<? extends T> clazz) 返回的是一个proxy object, 在执行的时候,proxy object会按照要求对 Class/Constructor/Field/Method/Parameter上的Annotation进行解析。
为了实现Annotation的解析逻辑,需要实现各种AnnotationResolver接口,一共有5个接口:
ClassAnnotationResolver, ConstructorAnnotationResolver, FieldAnnotationResolver, MethodAnnotationResolver, ParameterAnnotationResolver。以 MethodAnnotationResolver 为例,为了完整的解析可用于 Method 的 Annotation,需要实现以下的方法:
这3个方法分别会在对象构造之后,方法执行之前以及方法执行之后会被调用,可以实现相关的代码实现Annotation所要期望的逻辑。
接下来看一个简单的Example来看看如何定义Annotation、实现Resolver以及使用Engine,完整的例子可以参考 ARE4j testcase.
1. 定义Annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestMethodAnnotation {
}
2. 实现 Resolver
public class TestMethodResolver implements MethodAnnotationResolver {
public void resolveMethodAnnotationAfterConstruct(MethodResolveContext methodResolveContext) throws ResolverException {
}
public void resolveMethodAnnotationBeforeMethod(MethodResolveContext methodResolveContext) throws ResolverException {
methodResolveContext.setParameters(new String[]{"value2_changed"});
}
public void resolveMethodAnnotationAfterMethod(MethodResolveContext methodResolveContext) throws ResolverException {
}
}
3. 假设一个TestObject需要使用 Annotation
public class TestObject {
private Object field2 = "value1";
@TestMethodAnnotation
public void setField2_2(Object field2) {
this.field2 = field2;
}
public Object getField2() {
return field2;
}
}
4. TestCase
@Test
public void testMethodAnnotation() throws Exception{
Engine.registerAnnotation(TestMethodAnnotation.class, new TestMethodResolver());
TestObject testObject = Engine.newProxyInstance(TestObject.class);
testObject.setField2_2("");
// TestMethodResolver will check the init value and set value to "value2_change"
Assert.assertEquals("value2_changed", testObject.getField2());
}
这就是例子的全部,实现Resolver以及调用 Engine 都十分简单。
ARE4j能用在什么地方?
1. 可以使用在任何需要自定义和解析 Annotation 的地方;
2. 可以作为基础的类库用在Annotation驱动的 tool/framework/container。 比如:像Junit4一样的使用annotation的测试框架;像ejb3 jpa这样的容器;以及用来做Permance测试的工具。
参考:
https://github.com/yongyang/are4j
http://scannotation.sourceforge.net
https://github.com/jbossas/jandex
附件是ARE4j的讲解PPT,欢迎查看。
分享到:
相关推荐
jar包,亲测可用
jar包,亲测可用
"Common Sense C: Advice and Warnings for C and C++ Programmers" is a comprehensive guide aimed at helping programmers navigate the complexities and pitfalls of the C and C++ languages. Written by Paul...
To reduce annotation efforts, self-supervised semantic segmentation is recently proposed to pre-train a network without any human-provided labels. The key of this new form of learning is to design a ...
In this paper, we introduce Trinity, a general purpose graph engine over a distributed memory cloud. Through optimized memory management and network communication, Trinity supports fast graph ...
在本书中,作者详细介绍了Java语言的基本概念,包括变量、数据类型、运算符、流程控制语句(如if-else、switch、for、while等)、数组、字符串、类与对象、封装、继承、多态等面向对象编程的核心要素。此外,书中还...
Java is a new object-oriented programming language that was developed by Sun Microsystems for programming the Internet and intelligent appliances. In a very short time it has become one of the most ...
We introduce a novel end-to-endtrainableneuralnetworkthatiscapableofgeneratingaSocial Relationship Graph – a structured, unified representationofsocialrelationshipsandattributes–fromagiveninput ...
This book will introduce you to the most popular game development tool called Unreal Engine 4 with hands-on instructions for building stunning video games. You will begin by creating a new project or ...
【Java 入门指南】 Java 是一种广泛应用的高级编程语言,尤其在企业级应用和互联网开发中占有重要地位。学习Java的过程可以分为几个阶段,帮助软件工程师逐步成长为软件设计师、架构设计师或项目管理师。 ### 学习...
An Overview -- New Augmented Reality Taxonomy: Technologies and Features of Augmented Environment -- Visualization Techniques for Augmented Reality -- Mobile Augmented Reality Game Engine -...
文档中还提到,MT8788A支持LPDDR4内存,这是当前高端移动设备广泛使用的内存类型,它具有低功耗和高速数据传输速率的特点。此外,文档提及的GPU(图形处理器)频率为800MHz,表明该处理器具备一定的图形处理能力,这...
Book name: Introduce for machine leaning 4th edition Author: Ethem Alpaydin
The KALDI ASR engine adapted to Italian is described and the results obtained so far on some children speech ASR experiments are reported. We give a brief overview of KALDI, we describe in detail its ...
To introduce a second pass you need only to add the command line option: -passes(2) or, if this syntax presents a problem with your Shell, you may use: -passes[2] or, in some cases, -...
遵循Olga Filipova在“ Vue.js 2和Bootstrap 4 Web开发”一书中的指南进行的一个项目 它有什么作用? 单页Web应用程序,显示提交者的评论。 使用firebase和vue.js构建。 由Matthew Han编码。 请访问此处的网站: ...
Unlike traditional texts, this book uniquely demonstrates how econometrics has moved beyond a set of abstract tools to become genuinely useful for answering questions in business, policy evaluation, ...
- **Use Local Variable Type Inference (var)**: Introduced in Java 10, local variable type inference allows for more concise code by inferring the type of a local variable from its initializer....