定义并使用注解需要三步:
1.定义注解
1.1 注解的定义需要关键字 @interface
1.2 需要元注解来修饰定义的注解
1.3 注解中一般会包含一些元素以表示某些值
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constraints {
boolean primaryKey() default false;
boolean allowNull() default true;
boolean unique() default false;
}
元注解:
@Retention
Retention 的英文意为保留期的意思。当 @Retention 应用到一个注解上的时候,它解释说明了这个注解的的存活时间。
它的取值如下:
- RetentionPolicy.SOURCE 注解只在源码阶段保留,在编译器进行编译时它将被丢弃忽视。
- RetentionPolicy.CLASS 注解只被保留到编译进行的时候,它并不会被加载到 JVM 中。
- RetentionPolicy.RUNTIME 注解可以保留到程序运行的时候,它会被加载进入到 JVM 中,所以在程序运行时可以获取到它们。
@Documented
顾名思义,这个元注解肯定是和文档有关。它的作用是能够将注解中的元素包含到 Javadoc 中去。
@Target
Target 是目标的意思,@Target 指定了注解运用的地方。@Target 有下面的取值
ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
ElementType.CONSTRUCTOR 可以给构造方法进行注解
ElementType.FIELD 可以给属性进行注解
ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
ElementType.METHOD 可以给方法进行注解
ElementType.PACKAGE 可以给一个包进行注解
ElementType.PARAMETER 可以给一个方法内的参数进行注解
ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举
@Inherited 允许子类继承父类中的注解
@Documented 将此注解包含在Javadoc中
@Repeatable 自然是可重复的意思
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constraints {
boolean primaryKey() default false;
boolean allowNull() default true;
boolean unique() default false;
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DBTable {
public String name() default "";
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLInteger {
String name() default "";
Constraints constraints() default @Constraints;
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLString {
int value() default 0;
String name() default "";
Constraints constraints() default @Constraints;
}
注解元素可选的类型有:
所有基本类型 String Class enum Annotation 以及这些类型的数组。
2.使用注解
@DBTable(name="me")
public class Member {
//名为value 且唯一需要赋值时的注解元素在使用注解时可省略元素名
@SQLString(30) String firstName;
@SQLString(50) String lastName;
@SQLInteger Integer age;
@SQLString(value = 30,constraints = @Constraints(primaryKey = true))
String handle;
static int memberCount;
}
3.解析注解
解析注解需要借助Java反射机制
public class TableCreator {
public static void main(String[] args){
createTable(Member.class);
}
public static void createTable(Class<?> cl){
DBTable dbTable=cl.getAnnotation(DBTable.class);
if(dbTable==null){
System.out.println("no dbtable annotation in class "+cl.getName());
return;
}
String tableName=dbTable.name();
List<String> columnDefs=new ArrayList<String>();
for(Field field:cl.getDeclaredFields()){
String columnName=null;
Annotation[] anns=field.getDeclaredAnnotations();
if(anns==null||anns.length==0) return;
if(anns[0] instanceof SQLInteger){
SQLInteger sqlInteger=(SQLInteger) anns[0];
String name=sqlInteger.name();
//读取注解中类型为注解的元素
Constraints constraints=sqlInteger.constraints();
boolean primaryKey = constraints.primaryKey();
boolean allowNull = constraints.allowNull();
boolean unique=constraints.unique();
System.out.println(field+name+" "+primaryKey +" "+allowNull+" "+unique);
}
if(anns[0] instanceof SQLString){
SQLString sqlInteger=(SQLString) anns[0];
int value=sqlInteger.value();
String name=sqlInteger.name();
Constraints constraints=sqlInteger.constraints();
boolean primaryKey = constraints.primaryKey();
boolean allowNull = constraints.allowNull();
boolean unique=constraints.unique();
System.out.println(field+" "+value+" "+name+" "+primaryKey +" "+allowNull+" "+unique);
}
}
}
}