//创建新的注解
package com.lingzhuo.zhujie;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String nameAnnotation() default "张三";
String sexAnnotation();
int ageAnnotation();
}
//创建Class类,运用注解,
package com.lingzhuo.zhujie;
import java.lang.reflect.Field;
public class Clazz {
@MyAnnotation(ageAnnotation = 25, sexAnnotation = "男")
Student zhangsan = new Student();
@MyAnnotation(ageAnnotation = 24, sexAnnotation = "女",nameAnnotation="李四")
Student lisi = new Student();
@MyAnnotation(ageAnnotation = 26, sexAnnotation = "男",nameAnnotation="王五")
Student wangwu = new Student();
Student zhaoliu = new Student();
public Clazz(){
zhujie();
}
public Student getZhangsan() {
return zhangsan;
}
public void setZhangsan(Student zhangsan) {
this.zhangsan = zhangsan;
}
public Student getLisi() {
return lisi;
}
public void setLisi(Student lisi) {
this.lisi = lisi;
}
public Student getWangwu() {
return wangwu;
}
public void setWangwu(Student wangwu) {
this.wangwu = wangwu;
}
public void zhujie(){
Class<? extends Clazz> clas = Clazz.class;
Field[] filds = clas.getDeclaredFields();
for (int i = 0; i < filds.length; i++) {
Field field = filds[i];
MyAnnotation myannotation = field.getAnnotation(MyAnnotation.class);
if(myannotation!=null){
field.setAccessible(true);
Student student = new Student();
student.setAge(myannotation.ageAnnotation());
student.setName(myannotation.nameAnnotation());
student.setSex(myannotation.sexAnnotation());
try {
field.set(this, student);
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(false);
}
}
}
}
反射中的Class例子
package com.lingzhuo.test;
public class Student {
private int age=20;
private String name;
private String sex;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
//test类
package com.lingzhuo.test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Student zhangsan = new Student();
System.out.println(zhangsan.getAge());
Class<? extends Student> clazz = zhangsan.getClass();
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println(fields[i].getName());
}
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
Class[] p = methods[i].getParameterTypes();
Class p1 = methods[i].getReturnType();
System.out.println(p1.getName()+".....");
for (int j = 0; j < p.length; j++) {
System.out.println(p[j].getName());
}
}
try {
Field field = clazz.getDeclaredField("age");
field.setAccessible(true);
field.setInt(zhangsan, 30);
field.setAccessible(false);
System.out.println(zhangsan.getAge());
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}