Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Dynamic method invocation on Proxy object?

Reply
Thread Tools

Dynamic method invocation on Proxy object?

 
 
Sebastian
Guest
Posts: n/a
 
      02-08-2012
How can I dynamically invoke a method on a Proxy, where the method
belongs to one of the proxied interfaces?

Normally (i. e. in the non-proxy case), one would do something like this:

protected Object invokeDelegated( Method m, Object[] args,
Object delegate ) throws Exception
{
// m is a method from an interface that is not implemented by delegate
// find the corresponding method in delegate interface and invoke
Class<?>[] parameterTypes = new Class[args.length];
for( int i = 0; i < args.length; i++ ) {
parameterTypes[i] = args[i].getClass();
}
Method meth = delegate.getClass().getMethod( m.getName(),
parameterTypes );
return meth.invoke( delegate, args );
}

However, when delegate is itself a proxy, then delegate.getClass() will
give me Proxy, which is not what I'm looking for. How can I dynamically
invoke the methods in the proxied interfaces?

-- Sebastian
 
Reply With Quote
 
 
 
 
Steven Simpson
Guest
Posts: n/a
 
      02-08-2012
On 08/02/12 08:19, Sebastian wrote:
> However, when delegate is itself a proxy, then delegate.getClass()
> will give me Proxy,


Are you sure? I get $Proxy0 from this:

import java.lang.reflect.*;

public class GetProxyClass {
public static void main(String[] args) throws Exception {
ClassLoader classLoader =
ClassLoader.getSystemClassLoader();
InvocationHandler behaviour = new InvocationHandler() {
public Object invoke(Object proxy, Method method,
Object[] args) {
System.out.println("Called " + method);
return null;
}
};
Object proxy =
Proxy.newProxyInstance(classLoader,
new Class<?>[] { Runnable.class },
behaviour);
Class<?> proxyClass = proxy.getClass();
System.out.println("Proxy class is: " + proxyClass);

Method m = proxyClass.getMethod("run", new Class<?>[0]);
((Runnable) proxy).run();
m.invoke(proxy);
}
}

Output:

Proxy class is: class $Proxy0
Called public abstract void java.lang.Runnable.run()
Called public abstract void java.lang.Runnable.run()

It also demonstrates a method on the implemented interface being looked
up and invoked.

--
ss at comp dot lancs dot ac dot uk

 
Reply With Quote
 
 
 
 
Sebastian
Guest
Posts: n/a
 
      02-08-2012
Am 08.02.2012 11:34, schrieb Steven Simpson:
> On 08/02/12 08:19, Sebastian wrote:
>> However, when delegate is itself a proxy, then delegate.getClass()
>> will give me Proxy,

>
> Are you sure? I get $Proxy0 from this:
>

[coding snipped]
>
> Output:
>
> Proxy class is: class $Proxy0
> Called public abstract void java.lang.Runnable.run()
> Called public abstract void java.lang.Runnable.run()
>
> It also demonstrates a method on the implemented interface being looked
> up and invoked.


Thanks for that. I found my mistake - the reason that I could not find
the target method turned out to be different classloaders for the
argument types, so the signatures did not match.

-- Sebastian

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic method invocation jythonuser Python 3 06-26-2009 10:16 PM
Dynamic Method Invocation (specific line) ash Java 8 03-19-2008 01:58 PM
Method invocation via proxy and reflection Stefan Ram Java 3 10-01-2007 11:43 PM
cleaning up some reflection / dynamic method invocation code Marc E Java 2 07-19-2006 02:00 AM
Web Service invocation from behind proxy behind firewall Kumarforg ASP .Net Web Services 0 08-03-2004 07:15 AM



Advertisments