package annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class Client {
public static void main(String[] args) {
try {
Class clazz=Class.forName("annotation.Student");
//获取类的所有注解
Annotation[] annotation= clazz.getAnnotations();
for(Annotation a:annotation){
System.out.println(a); //@annotation.Table(value=tb_student)
}
//获取类指定的注解
Table table=(Table)clazz.getAnnotation(Table.class);
System.out.println(table.value());//tb_student
//获得类属性注解
Field f=clazz.getDeclaredField("name");
annotation.Field field=(annotation.Field) clazz.getAnnotation(annotation.Field.class);
System.out.println(field.columnName());
System.out.println(field.length());
System.out.println(field.type());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用反射读取注解
最新推荐文章于 2024-07-11 01:11:12 发布