深入理解Java:注解(Annotation)--注解处理器
如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了。使用注解的过程中,很重要的一部分就是创建于使用注解处理器。Java SE5 扩展了反射机制的 API,以帮助程序员快速的构造自定义注解处理器。
注解处理器类库 (java.lang.reflect.AnnotatedElement):
Java 使用 Annotation 接口来代表程序元素前面的注解,该接口是所有 Annotation 类型的父接口。除此之外,Java 在 java.lang.reflect 包下新增了 AnnotatedElement 接口,该接口代表程序中可以接受注解的程序元素,该接口主要有如下几个实现类:
Class:类定义
Constructor:构造器定义
Field:累的成员变量定义
Method:类的方法定义
Package:类的包定义
java.lang.reflect 包下主要包含一些实现反射功能的工具类,实际上,java.lang.reflect 包所有提供的反射 API 扩充了读取运行时 Annotation 信息的能力。当一个 Annotation 类型被定义为运行时的 Annotation 后,该注解才能是运行时可见,当 class 文件被装载时被保存在 class 文件中的 Annotation 才会被虚拟机读取。
AnnotatedElement 接口是所有程序元素(Class、Method 和 Constructor)的父接口,所以程序通过反射获取了某个类的 AnnotatedElement 对象之后,程序就可以调用该对象的如下四个个方法来访问 Annotation 信息:
方法 1:<T extends Annotation> T getAnnotation(Class<T> annotationClass): 返回改程序元素上存在的、指定类型的注解,如果该类型注解不存在,则返回 null。
方法 2:Annotation[] getAnnotations(): 返回该程序元素上存在的所有注解。
方法 3:boolean is AnnotationPresent(Class<?extends Annotation> annotationClass): 判断该程序元素上是否包含指定类型的注解,存在则返回 true,否则返回 false.
方法 4:Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。(如果没有注释直接存在于此元素上,则返回长度为零的一个数组。)该方法的调用者可以随意修改返回的数组;这不会对其他调用者返回的数组产生任何影响。
一个简单的注解处理器:
/*********** 注解声明 ***************//**
- 水果名称注解
- @author peida
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
String value() default "";
}
/**
- 水果颜色注解
- @author peida
/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
/**
* 颜色枚举
* @author peida
*
/
public enum Color{BULE,RED,GREEN};
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 颜色属性
* </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
<span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
Color fruitColor() </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> Color.GREEN;
}
/**
- 水果供应者注解
- @author peida
/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitProvider {
/**
* 供应商编号
* @return
/
public int id() default -1;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 供应商名称
* </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
<span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> String name() <span style="color: rgba(0, 0, 255, 1)">default</span> ""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 供应商地址
* </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
<span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> String address() <span style="color: rgba(0, 0, 255, 1)">default</span> ""<span style="color: rgba(0, 0, 0, 1)">;
}
/注解使用****/
public class Apple {
@FruitName(</span>"Apple"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String appleName;
@FruitColor(fruitColor</span>=<span style="color: rgba(0, 0, 0, 1)">Color.RED)
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String appleColor;
@FruitProvider(id</span>=1,name="陕西红富士集团",address="陕西省西安市延安路89号红富士大厦"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String appleProvider;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setAppleColor(String appleColor) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.appleColor =<span style="color: rgba(0, 0, 0, 1)"> appleColor;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getAppleColor() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> appleColor;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setAppleName(String appleName) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.appleName =<span style="color: rgba(0, 0, 0, 1)"> appleName;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getAppleName() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> appleName;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setAppleProvider(String appleProvider) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.appleProvider =<span style="color: rgba(0, 0, 0, 1)"> appleProvider;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getAppleProvider() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> appleProvider;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> displayName(){
System.out.println(</span>"水果的名字是:苹果"<span style="color: rgba(0, 0, 0, 1)">);
}
}
/注解处理器****/
public class FruitInfoUtil {
public static void getFruitInfo(Class<?> clazz){
String strFruitName</span>=" 水果名称:"<span style="color: rgba(0, 0, 0, 1)">;
String strFruitColor</span>=" 水果颜色:"<span style="color: rgba(0, 0, 0, 1)">;
String strFruitProvicer</span>="供应商信息:"<span style="color: rgba(0, 0, 0, 1)">;
Field[] fields </span>=<span style="color: rgba(0, 0, 0, 1)"> clazz.getDeclaredFields();
</span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Field field :fields){
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(field.isAnnotationPresent(FruitName.<span style="color: rgba(0, 0, 255, 1)">class</span>)<span style="color: rgba(0, 0, 0, 1)">){
FruitName fruitName </span>= (FruitName) field.getAnnotation(FruitName.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
strFruitName</span>=strFruitName+<span style="color: rgba(0, 0, 0, 1)">fruitName.value();
System.out.println(strFruitName);
}
<span style="color: rgba(0, 0, 255, 1)">else</span> </span><span style="color: rgba(0, 0, 255, 1)">if</span>(field.isAnnotationPresent(FruitColor.<span style="color: rgba(0, 0, 255, 1)">class</span>)<span style="color: rgba(0, 0, 0, 1)">){
FruitColor fruitColor</span>= (FruitColor) field.getAnnotation(FruitColor.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
strFruitColor</span>=strFruitColor+<span style="color: rgba(0, 0, 0, 1)">fruitColor.fruitColor().toString();
System.out.println(strFruitColor);
}
<span style="color: rgba(0, 0, 255, 1)">else </span></span><span style="color: rgba(0, 0, 255, 1)">if</span>(field.isAnnotationPresent(FruitProvider.<span style="color: rgba(0, 0, 255, 1)">class</span>)<span style="color: rgba(0, 0, 0, 1)">){
FruitProvider fruitProvider</span>= (FruitProvider) field.getAnnotation(FruitProvider.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
strFruitProvicer</span>=" 供应商编号:"+fruitProvider.id()+" 供应商名称:"+fruitProvider.name()+" 供应商地址:"+<span style="color: rgba(0, 0, 0, 1)">fruitProvider.address();
System.out.println(strFruitProvicer);
}
}
}
}
/输出结果****/
public class FruitRun {
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> args
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
FruitInfoUtil.getFruitInfo(Apple.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
}
}
====================================
水果名称:Apple
水果颜色:RED
供应商编号:1 供应商名称:陕西红富士集团 供应商地址:陕西省西安市延安路 89 号红富士大厦
Java 注解的基础知识点(见下面导图)基本都过了一遍,下一篇我们通过设计一个基于注解的简单的 ORM 框架,来综合应用和进一步加深对注解的各个知识点的理解和运用。