Java反射机制与动态代理

来源:互联网 发布:游戏 ios 知乎 编辑:程序博客网 时间:2024/05/16 01:00

IoC控制反转:工厂模式

AOP:代理模式,字节码(bytecode instrument[插桩] CGLIB

 

Java语言的反射机制

jaba运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能否调用它的任意一个方法?答案是肯定的。这种动态获取类的信息以及动态调用对象的方法的功能来自于java语言的反射(Reflection)机制。

Java反射机制主要提供了以下功能:

|-在运行时判断任意一个对象所属的类。

|-在运行时构造一个类的对象。

|-在运行时判断任意一个类所具有的成员变量和方法

|-在运行时调用任意一个对象的方法

Reflection Java被视为动态(或准动态)语言的一个关键性质。这个机制允许程序在运行时透过Reflection APIs取得任何一个已知名称class的内部信息,包括其modifiers(诸如public, static 等等)、superclass(例如Object)、实现之interfaces(例如Serializable),也包括fieldsmethods的所有信息,并可于运行时改变fields内容或调用methods

一般而言,开发者社群说到动态语言,大致认同的一个定义是:程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言。从这个观点看,PerlPythonRuby是动态语言,C++JavaC#不是动态语言。

尽管在这样的定义与分类下Java不是动态语言,它却有着一个非常突出的动态相关机制:Reflection。这个字的意思是反射、映象、倒影,用在Java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序可以加载一个运行时才得知名称的class,获悉其完整构造但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods。这种看透class”的能力(the ability of the program to examine itself)被称为introspection(内省、内观、反省)。Reflectionintrospection是常被并提的两个术语

JDK中,主要由以下类来实现Java反射机制,这些类都位于java.lang.reflect包中

§ Class类:代表一个类。

§ Field 类:代表类的成员变量(成员变量也称为类的属性)。

§ Method类:代表类的方法。

§ Constructor 类:代表类的构造方法。

§ Array类:提供了动态创建数组,以及访问数组的元素的静态方法

例程DumpMethods类演示了Reflection API的基本作用,它读取命令行参数指定的类名,然后打印这个类所具有的方法信息

 

Code:
  1. public class DumpMethods {   
  2.   
  3.     
  4.   
  5.     //从命令行接受一个字符串(该字符串是某个类的全名)   
  6.   
  7.     //打印出该类中的所有方法声明   
  8.   
  9.     public static void main(String[] args) throws ClassNotFoundException {   
  10.   
  11.        //Class类是Java反射的入口点   
  12.   
  13.        Class<?> classType = Class.forName(args[0]);   
  14.   
  15.           
  16.   
  17.        Method[] methods = classType.getDeclaredMethods();   
  18.   
  19.           
  20.   
  21.        for(Method method : methods){   
  22.   
  23.            System.out.println(method);   
  24.   
  25.        }   
  26.   
  27.     }   
  28.   
  29. }   
  30.   

 

 

Code:
  1. public class InvokeTester {   
  2.   
  3.     
  4.   
  5.     public int add(int param1, int param2){   
  6.   
  7.        return param1 + param2;   
  8.   
  9.     }   
  10.   
  11.        
  12.   
  13.     public String echo(String msg){   
  14.   
  15.        return "hello: " + msg;   
  16.   
  17.     }   
  18.   
  19.        
  20.   
  21.     public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {   
  22.   
  23.        Class<?> classType = InvokeTester.class;//会获得InvokerTester类所对应的Class对象   
  24.   
  25.           
  26.   
  27.        Object invokerTester = classType.newInstance();   
  28.   
  29.        //以上两行代码等价于   
  30.   
  31.        //InvokeTester i = new InvokeTester();   
  32.   
  33.           
  34.   
  35.        Method addMethod = classType.getMethod("add"new Class[]{int.class,int.class});   
  36.   
  37.        Object result = addMethod.invoke(invokerTester, new Object[] {100200});   
  38.   
  39.        //以上两行代码等价于   
  40.   
  41.        //i.add(100, 200);   
  42.   
  43.           
  44.   
  45.        System.out.println((Integer)result);   
  46.   
  47.           
  48.   
  49.        Method echoMethod = classType.getMethod("echo"new Class[]{String.class});   
  50.   
  51.        Object result2 = echoMethod.invoke(invokerTester, new Object[] {"Hello word"});   
  52.   
  53.        //以上两行代码等价于   
  54.   
  55.        //i.echo("Hello word");   
  56.   
  57.        System.out.println((String)result2);   
  58.   
  59.     }   
  60.   
  61. }   
  62.   

 

原创粉丝点击