动态代理

Sun对动态代理的说明:

一个简单例子代码:

动态代理的内部实现——代码生成:

研究JDK源代码,发现在Proxysun实现中调用了sun.misc.ProxyGenerator类的generateProxyClass( proxyName, interfaces)方法,其返回值为byte[]class文件的内存类型一致。于是做如下试验:


public class  ProxyClassFile{

       public static void main(String[] args){

              String proxyName = "TempProxy";

        TempImpl t = new TempImpl("proxy");

              Class[] interfaces =t.getClass().getInterfaces();

             

              byte[] proxyClassFile = ProxyGenerator.generateProxyClass(

                  proxyName, interfaces);

        File f = new File("classes/TempProxy.class");

        try {

            FileOutputStream fos = new FileOutputStream(f);

            fos.write(proxyClassFile);

            fos.flush();

            fos.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

        } catch (IOException e) {

            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

        }

       }

}

运行该类,到class文件夹下,利用反编译技术,发现原来其采用了代码生产技术:

 

public interface Temp{

       public void Talk();

       public void Run();

}

import java.lang.reflect.*;

 

public final class TempProxy extends Proxy

    implements Temp{

 

    private static Method m4;

    private static Method m2;

    private static Method m0;

    private static Method m3;

    private static Method m1;

 

    public TempProxy(InvocationHandler invocationhandler)   {

        super(invocationhandler);

    }

 

    public final void Run()    {

        try {

            h.invoke(this, m4, null);

            return;

        }

        catch(Error _ex) { }

        catch(Throwable throwable)  {

            throw new UndeclaredThrowableException(throwable);

        }

    }

 

    public final String toString(){

        try{

            return (String)h.invoke(this, m2, null);

        }

        catch(Error _ex) { }

        catch(Throwable throwable)  {

            throw new UndeclaredThrowableException(throwable);

        }

        return "";

    }

 

    public final int hashCode() {

        try {

            return ((Integer)h.invoke(this, m0, null)).intValue();

        }

        catch(Error _ex) { }

        catch(Throwable throwable){

            throw new UndeclaredThrowableException(throwable);

        }

        return 123;

    }

 

    public final void Talk(){

        try{

            h.invoke(this, m3, null);

            return;

        }

        catch(Error _ex) { }

        catch(Throwable throwable) {

            throw new UndeclaredThrowableException(throwable);

        }

    }

 

    public final boolean equals(Object obj) {

        try  {

            return ((Boolean)h.invoke(this, m1, new Object[] {

                obj

            })).booleanValue();

        }

        catch(Error _ex) { }

        catch(Throwable throwable) {

            throw new UndeclaredThrowableException(throwable);

        }

        return false;

    }

 

    static{

        try{

     m4 = Class.forName("Temp").getMethod("Run", new Class[0]);

     m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);

     m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);

     m3 = Class.forName("Temp").getMethod("Talk", new Class[0]);

     m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] {

                Class.forName("java.lang.Object")

            });

        }

        catch(NoSuchMethodException nosuchmethodexception) {

            throw new NoSuchMethodError(nosuchmethodexception.getMessage());

        }

        catch(ClassNotFoundException classnotfoundexception) {

            throw new NoClassDefFoundError(classnotfoundexception.getMessage());

        }

    }

}

 
返回JVM技术,反射与动态代理(一)
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐