package demo4;import java.io.Serializable;import javax.annotation.Resource;public class UserInfo implements Serializable,Runnable{ private static final long serialVersionUID = 1L; private String name="张三"; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } @Resource public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String say(String text){ return "你好,我叫小张!"+text; } @Override public void run() { } public UserInfo(String name, int age) { System.out.println("带参构造已执行"); this.name = name; this.age = age; } public UserInfo() { }}
package demo4;import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.TypeVariable;import org.junit.After;import org.junit.Before;import org.junit.Test;public class MyTest { public static void main(String[] args) throws Exception { //No.1 Class forName = Class.forName("demo4.UserInfo"); // //No.2 // Class clazz=UserInfo.class; // //No.3 // Class class1 = new UserInfo().getClass(); getPackageAndClassName(forName); System.out.println("-----------------------------------"); getClassFields(forName); System.out.println("-----------------------------------"); getSuperClassAndInterface(forName); System.out.println("-----------------------------------"); getClassMethod(forName); System.out.println("-----------------------------------"); getClassConstructor(forName); System.out.println("-----------------------------------"); getClassAnnotion(forName); } //包名和类名同时获取 public static void getPackageAndClassName(Class clazz){ Package pack = clazz.getPackage(); System.out.println("包名为:"+pack.getName()); System.out.println("类的全名为:"+clazz.getName()); System.out.println("类名:"+clazz.getSimpleName()); } //获取类中的字段 public static void getClassFields(Class clazz) throws Exception{ //1、getDeclaredFields() 只能获取当前类的字段 Field[] declaredFields = clazz.getDeclaredFields(); //不知道类名的情况下也可以创建类的对象 Object obj = clazz.newInstance(); for (Field field : declaredFields) { field.setAccessible(true); System.out.println(field.getName()); //取出字段保存的值 System.out.println(field.get(obj)); } Field field = clazz.getDeclaredField("age"); field.setAccessible(true); //给字段赋值 field.set(obj, 12); System.out.println("赋值成功!值为:"+ field.get(obj)); //获取age字段的数据类型 Class type = field.getType(); System.out.println("age字段的数据类型是:"+type.getName()); //获取访问修饰符 int code = field.getModifiers(); System.out.println("该字段的访问修饰符是:"+Modifier.toString(code)); } //3:获取类的父类以及实现的接口 public static void getSuperClassAndInterface(Class clazz){ System.out.println("该类的父类是:"+clazz.getSuperclass().getName()); System.out.println("该类实现的接口有:"); Class[] clazzs = clazz.getInterfaces(); for (Class class1 : clazzs) { System.out.println("接口名称:"+class1.getName()); } } //4:获取类中的方法 public static void getClassMethod(Class clazz) throws Exception{ //可以获取从父类继承过来的方法 Method[] methods = clazz.getMethods(); System.out.println(methods.length); for (Method method : methods) { System.out.println("类和父类的方法有:"+method.getName()); } System.out.println("--------------------------------------------------------"); //获取当前类的方法 Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method method : declaredMethods) { System.out.println("该类的方法有:"+method.getName()); } //获取单个方法 Method method = clazz.getDeclaredMethod("say",String.class); //获取方法的返回值 Class clazzs = method.getReturnType(); System.out.println("该方法的返回值是"+clazzs.getName()); //获取方法的形参类型 Class[] params = method.getParameterTypes(); for (Class class1 : params) { System.out.println("形参有:"+class1.getName()); } //不知道类名的情况下也可以创建类的对象 Object obj = clazz.newInstance(); //invoke让method对象执行 Object obj1= method.invoke(obj, "我是狗"); System.out.println("方法执行后的结果是:"+obj1); } //5:获取构造 public static void getClassConstructor(Class clazz) throws Exception{ //获取所有构造器 Constructor[] constructors = clazz.getConstructors(); for (Constructor constructor : constructors) { System.out.println(constructor.getName()); } //获取单个构造器 Constructor constructor = clazz.getConstructor(String.class,int.class); //执行构造器,返回类的实例 Object obj = constructor.newInstance("呵呵",12); } //6:获取注解 public static void getClassAnnotion(Class clazz) throws Exception{ Method method = clazz.getDeclaredMethod("getAge"); Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { System.out.println("该方法的注解有:"+annotation.toString()); } }}