6

When you r checking out that the method invoke(Object proxy, Method method, Object[] args) declaration & the doc statement,you will find that the input parameter proxy

proxy - the proxy instance that the method was invoked on

when I am doing a test on java dynamic proxy,I find this proxy is produced by vm.So I do want to know why the method invoke has this param,which is surely nothing except that is just an object ($proxy0 )but don't have the actual action for our usage?

Eric Chen
  • 79
  • 3
  • I am sorry,maybe you misunderstand what I want to express.I mean the proxy made by vm is just an object which just implements all the interfaces in the interface array(provided in method Proxy.newProxyInstace()).but this $proxy object just has the method but don't have the real action we want,'cause vm dont know what we want to do,isnt it? – Eric Chen Dec 20 '10 at 13:52

3 Answers3

5

This very useful if you have single invocation handle for multiple proxy objects. So you can use hash map to store proxy states information. For example - Mokito test framework store proxy invocation history.

Alex
  • 225
  • 2
  • 7
  • Thanks,Alex.but could you please make a detail on this statement,especially on hash map things? I think the handler is the true proxy or wrap class to wrap the class we provide to do the real business.am I right? – Eric Chen Dec 20 '10 at 14:00
1

if you are chinese ,you can read this article http://rejoy.iteye.com/blog/1627405

he makes a decompliation of $proxy0.class, you should know that $proxy0 extends Proxy

public final class $Proxy0 extends Proxy  
    implements UserService 

and there is a function in $proxy0:

public final void add()  
    {  
        try  
        {  
            //the h(invocationhandler) is in Proxy class,so we need pass this $proxy0 instance to super ,so the super(Proxy) can invoke($proxy0,method,args)
            super.h.invoke(this, m3, null);  
            return;  
        }  
        catch(Error _ex) { }  
        catch(Throwable throwable)  
        {  
            throw new UndeclaredThrowableException(throwable);  
        }  
    }  
Joey
  • 228
  • 1
  • 2
  • 11
0

we can use System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true"); to generate $proxy.class file.

decomplie the .class file, we will find this class every java methods (except native method) are invoked by InvocationHandler

    public final boolean equals(Object var1) throws  {
        try {
            return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final String toString() throws  {
        try {
            return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

super.h object is the InvocationHandler instance.

use System.out.println(proxy) in invocationHandler.invoke will cause infinite recursion and StackOverflowException.

so I think the only function about proxy parameter is use getClass() method to get the GeneratedProxy.class information, like which interfaces are implemented.

In InvocationHandler invoke method implemention, we can know the type of proxy object

So we can use an single InvocationHander class to deal with multiple proxy interfaces.

Yao Yuan
  • 350
  • 3
  • 11