关键字:getAnnotations getAnnotation getDeclaredField value
package com.g.demo;
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;
import com.g.demo.student2.filedg;
import com.g.demo.student2.g;
public class Test12 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
Class<?> c1 = Class.forName("com.g.demo.student2");
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
g annotation = (g)c1.getAnnotation(g.class);
String value = annotation.value();
System.out.println(value);
Field f = c1.getDeclaredField("name");
filedg annotation2 = f.getAnnotation(filedg.class);
System.out.println(annotation2.columnName());
System.out.println(annotation2.length());
System.out.println(annotation2.type());
Field h = c1.getDeclaredField("age");
filedg annotation3 = h.getAnnotation(filedg.class);
System.out.println(annotation2.columnName());
System.out.println(annotation2.length());
System.out.println(annotation2.type());
Field a = c1.getDeclaredField("id");
filedg annotation4 = a.getAnnotation(filedg.class);
System.out.println(annotation2.columnName());
System.out.println(annotation2.length());
System.out.println(annotation2.type());
}
}
@g("kk")
class student2{
@filedg(columnName = "id", length = 10, type = "int")
private int id;
@filedg(columnName = "age", length = 10, type = "int")
private int age;
@filedg(columnName = "name", length = 10, type = "string")
private String name;
public student2() {
super();
// TODO Auto-generated constructor stub
}
public student2(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 "student2 [id=" + id + ", age=" + age + ", name=" + name + "]";
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface g{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface filedg{
String columnName();
String type();
int length();
}
}
@com.g.demo.student2$g(value=kk)
kk
name
10
string
name
10
string
name
10
string