InvocationHandler - Who get a handle on the Object?

Ladies and Gentlemen,

Once again, here am I, confused. I need explanation on the code outline below.

/****************************************************************************/

package typeinfo.proxy;

import java.lang.reflect.*;

import java.util.*;

public class DynamicProxy implements InvocationHandler

{

private Object server;

public DynamicProxy(Object server)

{

this.server = server;

}

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable

{

/* print the proxy instance class name, print the interface

method signature and print the method's arguments. */

System.out.print("\n Proxy: " + proxy.getClass() + "\n Method: " +

method + "\n");

if(args != null)

{

System.out.println(" Method arguments:");

for(Object arg : args)

{

System.out.println(" " + arg);

}

}

return method.invoke(server, args);

}

}

/****************************************************************************/

Notes:

1. This is only the proxy class. The interface, implemetation (server) and client programmes are not included here.

2. My interpretation of the programme is that the cleint programme makes an instance of the proxy, upcast this instance to the interface and calls a method on it. This automatically leads to invocation of the proxy instance's invoke( ) method which ultimately calls the appropriate method on the implementation object (server).

The invoke( ) method above is required to return an Object. My question is where is the object returned to?

method.invoke(server, args); I want this method invocation on the server to return an integer value that I would like to use in the client code. How do I get the proxy to fetch and subsequently release this value to the client?

Thank you.

ue_Joe.

[1963 byte] By [ue_Joea] at [2007-11-27 11:52:09]
# 1

> The invoke( ) method above is required to return an

> Object. My question is where is the object returned

> to?

Um, to whoever calls the method.

I don't understand the question.

jverda at 2007-7-29 18:42:42 > top of Java-index,Java Essentials,New To Java...
# 2

also, you've been posting for a bit now. You're a big boy now. Please learn to use code tags so that

1) we can read your code and

2) your code doesn't get garbled (which can happen if you have any array indices looking like text formating strings).

petes1234a at 2007-7-29 18:42:42 > top of Java-index,Java Essentials,New To Java...
# 3

Bear with me. English is not my native language. If the proxy instance automatically triggers the invoke( ) method, where does this method send its returned value to? If the returned value is sent to somewhere, then it should be possible to get it and obtain information from it.

ue-Joe.

ue_Joea at 2007-7-29 18:42:42 > top of Java-index,Java Essentials,New To Java...
# 4

Your question still doesn't make any sense. Whoever calls the method gets the return value.

import java.lang.reflect.*;

public class ProxyStuff {

public static void main(String[] args) throws Exception {

InvocationHandler handler = new MyProxyHandler();

MyProxyXfc xfc = (MyProxyXfc) Proxy.newProxyInstance(MyProxyXfc.class.getClassLoader(),

new Class[] { MyProxyXfc.class },

handler);

String foo = xfc.foo();

System.out.println(foo);

String bar = xfc.bar();

System.out.println(bar);

}

}

interface MyProxyXfc {

String foo();

String bar();

}

class MyProxyBase implements MyProxyXfc {

public String foo() {

return "MyProxyBase foo";

}

public String bar() {

return "MyProxyBase bar";

}

}

class MyProxyHandler implements InvocationHandler {

private final MyProxyXfc proxXfc = new MyProxyBase();

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

if (method.getName().equals("foo")) return foo();

else return method.invoke(proxXfc, args);

}

private String foo() {

return "invocation handler foo";

}

}

When I call foo and bar on xfc (my InvocationHandler), they end up calling its invocationHandler method.

jverda at 2007-7-29 18:42:42 > top of Java-index,Java Essentials,New To Java...
# 5

Note that you never call the InvocationHandler's invoke method directly.

jverda at 2007-7-29 18:42:42 > top of Java-index,Java Essentials,New To Java...
# 6

Thank you Sir! You have answered my questions. The point on the issue of code tags is also noted.

ue_Joe.

ue_Joea at 2007-7-29 18:42:42 > top of Java-index,Java Essentials,New To Java...