Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11845 > unrolled thread

Dynamic method invocation on Proxy object?

Started bySebastian <sebastian@undisclosed.invalid>
First post2012-02-08 09:19 +0100
Last post2012-02-08 15:16 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Dynamic method invocation on Proxy object? Sebastian <sebastian@undisclosed.invalid> - 2012-02-08 09:19 +0100
    Re: Dynamic method invocation on Proxy object? Steven Simpson <ss@domain.invalid> - 2012-02-08 10:34 +0000
      Re: Dynamic method invocation on Proxy object? Sebastian <sebastian@undisclosed.invalid> - 2012-02-08 15:16 +0100

#11845 — Dynamic method invocation on Proxy object?

FromSebastian <sebastian@undisclosed.invalid>
Date2012-02-08 09:19 +0100
SubjectDynamic method invocation on Proxy object?
Message-ID<jgtb77$hsu$1@news.albasani.net>
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

[toc] | [next] | [standalone]


#11850

FromSteven Simpson <ss@domain.invalid>
Date2012-02-08 10:34 +0000
Message-ID<hmv909-bic.ln1@news.simpsonst.f2s.com>
In reply to#11845
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

[toc] | [prev] | [next] | [standalone]


#11852

FromSebastian <sebastian@undisclosed.invalid>
Date2012-02-08 15:16 +0100
Message-ID<jgu04d$vdc$1@news.albasani.net>
In reply to#11850
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web