Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Dynamic method invocation on Proxy object?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Sebastian  
View profile  
 More options Feb 8, 3:19 am
Newsgroups: comp.lang.java.programmer
From: Sebastian <sebast...@undisclosed.invalid>
Date: Wed, 08 Feb 2012 09:19:51 +0100
Local: Wed, Feb 8 2012 3:19 am
Subject: Dynamic method invocation on Proxy object?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Simpson  
View profile  
 More options Feb 8, 5:34 am
Newsgroups: comp.lang.java.programmer
From: Steven Simpson <s...@domain.invalid>
Date: Wed, 08 Feb 2012 10:34:25 +0000
Local: Wed, Feb 8 2012 5:34 am
Subject: Re: Dynamic method invocation on Proxy object?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sebastian  
View profile  
 More options Feb 8, 9:16 am
Newsgroups: comp.lang.java.programmer
From: Sebastian <sebast...@undisclosed.invalid>
Date: Wed, 08 Feb 2012 15:16:45 +0100
Local: Wed, Feb 8 2012 9:16 am
Subject: Re: Dynamic method invocation on Proxy object?
Am 08.02.2012 11:34, schrieb Steven Simpson:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »