public class Test12 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> aClass = Class.forName("com.fl.reflection.Student2");
//通过反射获得注解
Annotation[] annotations = aClass.getAnnotations();
for (Annotation annotation:annotations) {
System.out.println(annotation);
}
//获取注解value的值
Tablell annotation = (Tablell) aClass.getAnnotation(Tablell.class); //获取指定注解
System.out.println(annotation.value());
//获得类指定的 注解
Field name = aClass.getDeclaredField("name");
Fieldll annotation1 = name.getAnnotation(Fieldll.class);
System.out.println(annotation1.culumeName());
System.out.println(annotation1.type());
System.out.println(annotation1.length());
}
}
@Tablell("db_student")
class Student2{
@Fieldll(culumeName="db_id",type = "int",length = 10)
private int id;
@Fieldll(culumeName="db_age",type = "int",length = 10)
private int age;
@Fieldll(culumeName="db_name",type = "varchar",length = 10)
private String name;
public Student2(){
}
public Student2(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablell{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldll{
String culumeName();
String type();
int length();
}