注解与反射16-获取注解信息
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/feb42a1ccb0d6f5f1ecea4d16e18f9ea.png)
package com.kuang.reflection;
import java.lang.annotation.*;
public class Test12 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.kuang.reflection.Student2");
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
Table table = (Table) c1.getAnnotation(Table.class);
System.out.println(table.value());
java.lang.reflect.Field f = c1.getDeclaredField("name");
Field annotation = f.getAnnotation(Field.class);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
@Table("db_student")
class Student2{
@Field(columnName = "db_id",type = "int",length = 10)
private int id;
@Field(columnName = "db_age",type = "int",length = 10)
private int age;
@Field(columnName = "db_name",type = "varchar",length = 10)
private String name;
public Student2(){
}
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;
}
public Student2(int id, int age, String name){
this.id=id;
this.age=age;
this.name=name;
}
@Override
public String toString() {
return "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Field{
String columnName();
String type();
int length();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/10b33f6cfeaebd9d230e44427ee1e588.png)