package com.reflection.test;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
public class test01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
Class c1 = Class.forName("com.reflection.test.Student");
Annotation[] annotations =c1.getAnnotations();
for(Annotation annotation:annotations) {
System.out.println(annotation);
}
Tableliu tableliu =(Tableliu)c1.getAnnotation(Tableliu.class);
String value = tableliu.value();
System.out.println(value);
Field f =c1.getDeclaredField("name");
Fieldliu annotation =f.getAnnotation(Fieldliu.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@Tableliu("db_student")
class Student{
@Fieldliu(columnName="db_id",type="int",length=10)
private int id;
@Fieldliu(columnName="db_age",type="int",length=10)
private int age;
@Fieldliu(columnName="db_name",type="varchar",length=3)
private String name;
public Student() {
}
public Student(int id, int age, String name) {
super();
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", name=" + name + "]";
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tableliu{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldliu{
String columnName();
String type();
int length();
}