反射是java的高级特性,充分利用了JVM的解释机制。和泛型、注解构成了Java最独特的特性,出现了各类增长代码复用的设计思想,直接致使java生态圈丰富框架的出现,其中最出名的是IOC思想的spring。使用的.java文件在编译器编译后成 .class 字节码。而后在classloader的时候会将.class 文件加载并解析到永久区,并在运行时调用class类的方法。java
a) java.lang.Class:所用的反射的基础,class在建立的时候都是存放在该类下面,从而JVM能够在运行时使用,操做类的数据。spring
b) java.lang.reflect.*:class每个部分的组成都在这个包。例如:构造器、属性框架
a)组成:访问修饰符、类型、超类、接口、泛型类型、属性、方法、注解ide
b)全部超类、接口、类以及实例都是class的子类(在内存中,全部的class内容,除去逻辑代码部分,都是挂载在java.lang.class上面)。spa
/** * @see 反射接口 * @author ssHss * */ public class TestReflect<Lang> extends Reflect1 implements ReIn { private String hello1; public String hello2; public static void main(String[] args) throws Exception { Class<?> c = Class.forName("reflect.TestReflect"); // 查找以 TestReflect的.class 文件对象 Object o = c.newInstance(); // 获取实例 System.out.println("类型:" + c.getTypeName()); System.out.println("field"); // 属性 Field[] files = c.getFields(); for (Field field : files) { System.out.println("属性名 :" + field.getName() + " 类型 :" + field.getGenericType()); } // 公告属性 Field[] declearFiles = c.getDeclaredFields(); for (Field field : declearFiles) { System.out.println("属性名 :" + field.getName() + " 类型 :" + field.getGenericType()); } System.out.println("method"); // 全部方法 Method[] methods = c.getMethods(); for (Method method : methods) { System.out.println("方法名 :" + method.getName() + " 类型 :" + method.getGenericReturnType()); method.setAccessible(true);// 使用私有方法 for (Parameter params : method.getParameters()) { System.out.println("方法参数 :" + params.getName() + " 类型 :" + params.getType()); } } // 公共方法 Method[] declearedMethods = c.getDeclaredMethods(); for (Method method : declearedMethods) { System.out.println("公共方法名 :" + method.getName() + " 返回类型 :" + method.getGenericReturnType()); for (Parameter params : method.getParameters()) { System.out.println("公共方法参数 :" + params.getName() + " 返回类型 :" + params.getType()); } } System.out.println("annotation"); // 所用注解 Annotation[] annotation = c.getAnnotations(); for (Annotation anno : annotation) { System.out.println("注解 :" + anno.toString()); } // 公告注解 Annotation[] declearedAnnotation = c.getDeclaredAnnotations(); for (Annotation a : declearedAnnotation) { System.out.println("公告注解 :" + a.toString()); } TypeVariable<?>[] ces = c.getTypeParameters(); for (TypeVariable<?> typeVariable : ces) { System.out.println("泛型:" + typeVariable.getName()); } System.out.println("superClass"); System.out.println("superClass"); // 超类(父类和接口) Class<?> suc = c.getSuperclass(); System.out.println("超类:" + suc.getName() + " 类型: " + suc.getTypeName()); System.out.println("超类属性"); Field[] superFiles = suc.getFields(); for (Field field : superFiles) { System.out.println("超类属性:" + field.getName()); } System.out.println("超类属性"); TypeVariable<?>[] classes = suc.getTypeParameters(); for (TypeVariable<?> typeVariable : classes) { System.out.println("超类泛型:" + typeVariable.getName()); } System.out.println("接口"); Class<?>[] intefs = c.getInterfaces(); for (Class<?> ints : intefs) { System.out.println("接口:" + ints.getName() + " 接口: " + ints.getTypeName()); for (TypeVariable<?> intsp : ints.getTypeParameters()) { System.out.println("接口泛型: " + intsp.getTypeName()); } } } @Override public void getInterface() { // TODO Auto-generated method stub } @Override public void getInterface(int over) { // TODO Auto-generated method stub } } class Reflect1<Integer, Byte, Charactor> { public String he; public String hello4; } interface ReIn<Short> { static int intefaceNumber = 9; void getInterface(); void getInterface(int over); }