本文总结的注解annotation的创建、使用以及得到注解信息,本文总结的是类上的、方法上的和字段上的注解,创建的工程如下:
一、首先介绍创建注解所用到的元注解(先有个了解即可,可略过)
1、@Target表示该注解可以用到什么地方,可能得ElementType参数包括:
CONSTRUCTOR构造器的声明
FIELD域声明(我理解的就是字段,也就是变量)
LOCAL_VARIABLE局部变量声明
METHOD方法声明
PACKAGE包声明
PARAMETEY参数声明
TYPE类、接口或enum声明
@Retention表示需要在什么级别保存该注解信息,可选的RetentionPolicy参数包括:
SOURCE注解将被编译器丢弃
CLASS注解在class文件中可用,但会被VM丢弃
RUNTIME在运行期也可以保存注解,因此能够通过反射机制读取注解信息
@Document将此注解包含在文档中
@Inherited允许子类继承父类中的注解
二、类 级别注解的创建、使用以及得到信息
1、创建注解:
package com.wang.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 作用在类、接口、或enum声明上的注解
* @author HeJW
*
*/
@Target(ElementType.TYPE)//作用在类、接口、或enum声明
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAnnotation {
public String name();
public String description() default "no description ";
}
2、使用,也就是只能在类上面才能使用上面的注解:
@ClassAnnotation(name = "class name is annotationBean")
public class AnnotationBean {.......}
3、得到注解信息
Class<?> cl = Class.forName("com.wang.annotation.bean.AnnotationBean");
//得到类上的annotation
ClassAnnotation ca = cl.getAnnotation(ClassAnnotation.class);
System.out.println(ca.name());
System.out.println(ca.description());
三、方法上的
1、定义注解:
package com.wang.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 作用在方法上的注解
* @author HeJW
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
public String name();
public String description() default "no description";
}
2、使用,只能在方法上使用:
@MethodAnnotation(name = "method name is methodTest", description = "method's name")
public void methodTest(){
System.out.println("test");
}
@MethodAnnotation(name = "method name is getId", description = "get id")
public int getId() {
return id;
}
3、得到信息:
//得到方法上的annotation
//for( Method m : cl.getDeclaredMethods() ){
for( Method m : cl.getMethods() ){
MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class);
//有的方法上有annotation,有的方法上没有annotation
if( ma != null ){
System.out.println(ma.name());
System.out.println(ma.description());
}
}
四、变量上的
1、为了下一步的说明,定义两个地段上的注解:
package com.wang.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnnotation1 {
public String name();
public String description() default "no description ";
}
另一个与这个基本相同:
package com.wang.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnnotation2 {
public int value();
public String type();
}
2、使用:
@FieldAnnotation1(name="attribute name is id")
@FieldAnnotation2(value=99,type="int")
private int id;
@FieldAnnotation1(name="attribute name is desc", description="descript attribute")
private String desc;
3、得到信息:
//得到属性上的annotation
for ( Field field : cl.getDeclaredFields() ) {
FieldAnnotation1 fa1 = field.getAnnotation(FieldAnnotation1.class);
if(fa1 != null){
System.out.println(fa1.name());
System.out.println(fa1.description());
}
FieldAnnotation2 fa2 = field.getAnnotation(FieldAnnotation2.class);
if(fa2 != null){
System.out.println(fa2.value());
System.out.println(fa2.type());
}
}
五、除了能够得到指定的注解信息外,还能够通过得到annotation数组的形式得到注解信息
//以获取属性上的annotation为例,获取注解的annotation数组
for ( Field field : cl.getDeclaredFields() ) {
Annotation[] anns = field.getDeclaredAnnotations();
if( anns[0] instanceof FieldAnnotation1 ){
FieldAnnotation1 fa1 = (FieldAnnotation1)anns[0];
System.out.println(fa1.name());
System.out.println(fa1.description());
}
}
六、使用注解和得到注解信息的 完整代码
1、使用注解的完整代码:
package com.wang.annotation.bean;
import com.wang.annotation.ClassAnnotation;
import com.wang.annotation.FieldAnnotation1;
import com.wang.annotation.FieldAnnotation2;
import com.wang.annotation.MethodAnnotation;
/**
* 使用自定义annotation
* @author HeJW
*
*/
@ClassAnnotation(name = "class name is annotationBean")
public class AnnotationBean {
@FieldAnnotation1(name="attribute name is id")
@FieldAnnotation2(value=99,type="int")
private int id;
@FieldAnnotation1(name="attribute name is desc", description="descript attribute")
private String desc;
@MethodAnnotation(name = "method name is methodTest", description = "method's name")
public void methodTest(){
System.out.println("test");
}
@MethodAnnotation(name = "method name is getId", description = "get id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
2、得到注解信息的完整代码:
package com.wang.annotation.app;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.wang.annotation.ClassAnnotation;
import com.wang.annotation.FieldAnnotation1;
import com.wang.annotation.FieldAnnotation2;
import com.wang.annotation.MethodAnnotation;
/**
* 测试类
* @author HeJW
*
*/
public class AnnotationTestApp {
public static void main(String[] args) throws Exception {
Class<?> cl = Class.forName("com.wang.annotation.bean.AnnotationBean");
//得到类上的annotation
ClassAnnotation ca = cl.getAnnotation(ClassAnnotation.class);
System.out.println(ca.name());
System.out.println(ca.description());
//得到方法上的annotation
//for( Method m : cl.getDeclaredMethods() ){
for( Method m : cl.getMethods() ){
MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class);
//有的方法上有annotation,有的方法上没有annotation
if( ma != null ){
System.out.println(ma.name());
System.out.println(ma.description());
}
}
//得到属性上的annotation
for ( Field field : cl.getDeclaredFields() ) {
FieldAnnotation1 fa1 = field.getAnnotation(FieldAnnotation1.class);
if(fa1 != null){
System.out.println(fa1.name());
System.out.println(fa1.description());
}
FieldAnnotation2 fa2 = field.getAnnotation(FieldAnnotation2.class);
if(fa2 != null){
System.out.println(fa2.value());
System.out.println(fa2.type());
}
}
//以获取属性上的annotation为例,获取注解的annotation数组
for ( Field field : cl.getDeclaredFields() ) {
Annotation[] anns = field.getDeclaredAnnotations();
if( anns[0] instanceof FieldAnnotation1 ){
FieldAnnotation1 fa1 = (FieldAnnotation1)anns[0];
System.out.println(fa1.name());
System.out.println(fa1.description());
}
}
}
}