JAVA 注解入门

什么是注解

注解时jdk5之后引入的新特性,是一种特殊的注释,它可以存在于源码里、class文件里、运行时,正是由于这种特性,我们可以利用注解搭配反射来自动生成一些代码。

JAVA自带的一些注解

  • @Deprecated 表明当前的元素已经不推荐使用
  • @Override 表明当前方法是覆盖了父类方法
  • @SuppressWarnings 关闭不当的编译器警告信息

如何自定义注解

自定义注解需要用到元注解,所谓元注解就是用来注解‘普通注解’的注解,元注解有四种@Target @Documented @Retention @Inherited

@Target

@Target用于描述注解的使用范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。

  • 作用:用于描述注解的使用范围
  • 取值(ElementType)有:
    • CONSTRUCTOR:用于描述构造器
    • FIELD:用于描述域即类成员变量
    • LOCAL_VARIABLE:用于描述局部变量
    • METHOD:用于描述方法
    • PACKAGE:用于描述包
    • PARAMETER:用于描述参数
    • TYPE:用于描述类、接口(包括注解类型) 或enum声明
    • ANNOTATION_TYPE:用于描述普通注解,元注解使用的target就是这种类型
  • 示例
1
2
3
4
@Target(ElementType.TYPE)//表示该注解使用在类、接口或enum上  
public @interface Table {
public String tableName() default "className";
}

@Retention

@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。

  • 作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
  • 取值(RetentionPoicy)有:
    1. SOURCE:在源文件中有效(即源文件保留)
    2. CLASS:在class文件中有效(即class保留)
    3. RUNTIME:在运行时有效(即运行时保留)
  • 示例
1
2
3
4
@Target(ElementType.FIELD)  
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
}

@Documented

@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。

  • 示例
1
2
3
4
5
@Target(ElementType.FIELD)  
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
}

@Inherited

@Inherited 元注解也是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,那么这个class的子类也将被该注解修饰。

1
2
3
4
5
@Target(ElementType.FIELD)  
@Retention(RetentionPolicy.RUNTIME)
@@Inherited
public @interface Column {
}

注解的作用

  1. 编译前提示信息:注解可以被编译器用来发现错误,或者清除不必要的警告;
  2. 编译时生成代码:一些处理器可以在编译时根据注解信息生成代码,比如 Java 代码,xml 代码等,Android中使用apt指定注解处理器来实现,例如ButterKnife、 Dagger2。
  3. 运行时处理:我们可以在运行时根据注解,通过反射获取具体信息,然后做一些操作。下面简单介绍下运行时注解的使用方式。

自定义注解的实现

要实现的效果:通过给数据类Bean添加相应的注解@Table ,@Column,就可以获取到创建表的sql语句。
由于我们搭建的是运行时框架,需要再运行时通过反射来进行,所以注解的级别必须设置成Runtime级别,这样运行时才能反射到相应的注解
通过下面的定义,我们就能在运行时动态获取@Table与@Column两个注解与其对应的名称

  • 定义对应表的注解
1
2
3
4
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String name(); //name用来设置表名
}
  • 定义对应字段的注解
1
2
3
4
@Retention(RetentionPolicy.RUNTIME)
@interface Column{
String name(); //name用来设置字段名
}
  • 实体类
1
2
3
4
5
6
7
8
9
10
11
12
/**
* @author liuzundong
* @version 1.0
* @since: 2017/9/29 下午5:10
*/
@Table(name = "entity")
public class Entity {
@Column(name = "field")
private int field;
@Column(name = "name")
private String name;
}

反射API

1
<T extends Annotation> T getAnnotation(Class<T> annotationType) //获取注解在其上的annotationType.
1
Annotation[] getAnnotations() //获取所有注解.
1
boolean isAnnotationPresent(Class<T> annotationType) //判断当前元素是否被annotationType注解
1
Annotation[] getDeclareAnnotations()// 与getAnnotations() 类似,但是不包括父类中被Inherited修饰的注解.

获取表名

1
2
3
4
5
6
7
private static String getTableName(Class bean) {
if (bean.isAnnotationPresent(Table.class)) {
Table table = (Table) bean.getAnnotation(Table.class);
return table.name();
}
return null;
}

获取字段名与类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static Map<String, String> getNameType(Class bean) {
Map<String, String> result = new HashMap<>();
List<Field> fields = Arrays.asList(bean.getDeclaredFields());
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
String name = field.getAnnotation(Column.class).name();
String type;
if (field.getType().isAssignableFrom(int.class)) {
type = "integer";
} else if (field.getType().isAssignableFrom(String.class)) {
type = "text";
} else {
throw new IllegalArgumentException("not supported type");
}
result.put(name, type);
}
}
return result;
}

结果

1
2
3
4
5
public static void main(String[] args) {
String tableName = getTableName(Entity.class);
Map<String, String> nameType = getNameType(Entity.class);
System.out.print("tableName:" + tableName + "\n" + nameType.keySet() + "," + nameType.values());
}

输出

1
2
tableName:entity
[field, name],[integer, text]

参考文章 http://blog.csdn.net/duo2005duo/article/details/50505884