Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Method invocation via proxy and reflection

Reply
Thread Tools

Method invocation via proxy and reflection

 
 
Stefan Ram
Guest
Posts: n/a
 
      10-01-2007
A proxy object can implement any interface and then delegate
calls to anywhere.

I was able to use this once, but now I got a strange behavior
with Java 1.6 and java.lang.CharSequence.

One can see below that within the method »invoke«, the method
is being »redirected« to the method »Base#length()«. Therefore,
the program should print »363«.
This new invocation target is even printed (see »### HERE ###«
in the source code below).

But actually »Base#dummy0()« is called. (The output
of the program is given at the very end.)
I might have made some mistake. But in this case, I would
expect an exception or an error. Instead silently the wrong
method »Base#dummy0()« is called. Why?

class Util
{
/** Return the field "clazz" of the class java.lang.reflect.Method.
@return the field "clazz" of the class java.lang.reflect.Method */
static java.lang.reflect.Field methodClassField()
{ final java.lang.reflect.Field fields[] =
java.lang.reflect.Method.class.getDeclaredFields() ;
for( int i = 0; i < fields.length; ++i )
{ if( fields[ i ].getName().equals( "clazz" ))
return fields[ i ]; }
throw new java.lang.RuntimeException
( "Can't find field \"clazz\" of class \"java.lang.reflect.Method\"." ); }}

/** Delegate class */
class Delegate
{ public int dummy0(){ return 147; }
public int dummy1(){ return 266; }
public int length(){ return 363; }
public int dummy3(){ return 473; }
public int dummy4(){ return 557; }}

class InvocationHandler0 implements java.lang.reflect.InvocationHandler
{ final private Delegate delegate;
java.lang.reflect.Field classField = Util.methodClassField();

public InvocationHandler0( final Delegate delegate )
{ this.delegate = delegate;
this.classField.setAccessible( true ); }

/** redirect call to delegate object */
public java.lang.Object invoke
( final java.lang.Object proxy,
final java.lang.reflect.Method method,
final java.lang.Object[] args )
throws java.lang.Throwable
{ java.lang.System.out.println( method ); // java.lang.CharSequence.length()
classField.set( method, this.delegate.getClass() );
java.lang.System.out.println( method ); // Base.length() ### HERE ###
return method.invoke( delegate, args ); }}

class Main
{ public static void main( final java.lang.String[] _ )
{
/* create and use a proxy »v« to handle CharSequence-calls */
final Delegate delegate = new Delegate();
java.lang.ClassLoader cl = java.lang.CharSequence.class.getClassLoader();
java.lang.CharSequence v =
( java.lang.CharSequence )java.lang.reflect.Proxy.newProxyInstance
( cl, new Class[]{ java.lang.CharSequence.class },
new InvocationHandler0( delegate ));

java.lang.System.out.println( v.length() ); }}

/* The output is:

public abstract int java.lang.CharSequence.length()
public abstract int Delegate.length()
147

*/

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      10-01-2007
On Oct 1, 3:00 pm, (E-Mail Removed)-berlin.de (Stefan Ram) wrote:
> A proxy object can implement any interface and then delegate
> calls to anywhere.
>
> I was able to use this once, but now I got a strange behavior
> with Java 1.6 and java.lang.CharSequence.
>
> One can see below that within the method »invoke«, the method
> is being »redirected« to the method »Base#length()«. Therefore,
> the program should print »363«.
> This new invocation target is even printed (see »### HERE ###«
> in the source code below).
>
> But actually »Base#dummy0()« is called. (The output
> of the program is given at the very end.)
> I might have made some mistake. But in this case, I would
> expect an exception or an error. Instead silently the wrong
> method »Base#dummy0()« is called. Why?
>
> class Util
> {
> /** Return the field "clazz" of the class java.lang.reflect.Method.
> @return the field "clazz" of the class java.lang.reflect.Method */
> static java.lang.reflect.Field methodClassField()
> { final java.lang.reflect.Field fields[] =
> java.lang.reflect.Method.class.getDeclaredFields() ;
> for( int i = 0; i < fields.length; ++i )
> { if( fields[ i ].getName().equals( "clazz" ))
> return fields[ i ]; }
> throw new java.lang.RuntimeException
> ( "Can't find field \"clazz\" of class \"java.lang.reflect.Method\"." ); }}
>
> /** Delegate class */
> class Delegate
> { public int dummy0(){ return 147; }
> public int dummy1(){ return 266; }
> public int length(){ return 363; }
> public int dummy3(){ return 473; }
> public int dummy4(){ return 557; }}
>
> class InvocationHandler0 implements java.lang.reflect.InvocationHandler
> { final private Delegate delegate;
> java.lang.reflect.Field classField = Util.methodClassField();
>
> public InvocationHandler0( final Delegate delegate )
> { this.delegate = delegate;
> this.classField.setAccessible( true ); }
>
> /** redirect call to delegate object */
> public java.lang.Object invoke
> ( final java.lang.Object proxy,
> final java.lang.reflect.Method method,
> final java.lang.Object[] args )
> throws java.lang.Throwable
> { java.lang.System.out.println( method ); // java.lang.CharSequence.length()
> classField.set( method, this.delegate.getClass() );
> java.lang.System.out.println( method ); // Base.length() ### HERE ###
> return method.invoke( delegate, args ); }}
>
> class Main
> { public static void main( final java.lang.String[] _ )
> {
> /* create and use a proxy »v« to handle CharSequence-calls */
> final Delegate delegate = new Delegate();
> java.lang.ClassLoader cl = java.lang.CharSequence.class.getClassLoader();
> java.lang.CharSequence v =
> ( java.lang.CharSequence )java.lang.reflect.Proxy.newProxyInstance
> ( cl, new Class[]{ java.lang.CharSequence.class },
> new InvocationHandler0( delegate ));
>
> java.lang.System.out.println( v.length() ); }}
>
> /* The output is:
>
> public abstract int java.lang.CharSequence.length()
> public abstract int Delegate.length()
> 147
>
> */



I have to run, but I think the problem is that Delegate doesn't
implement CharSequence, so calling the CharSequence.length() on the
delegate is undefined behavior. You need to intercept the method
name, and act accordingly.

 
Reply With Quote
 
 
 
 
Piotr Kobzda
Guest
Posts: n/a
 
      10-01-2007
Stefan Ram wrote:

> I might have made some mistake. But in this case, I would
> expect an exception or an error. Instead silently the wrong
> method »Base#dummy0()« is called. Why?


Because you are trying to cheat Java doing something which is not
normally allowed -- i.e. change private data of a Method. It succeeds
because there is no security manager active, but run it with even
default SM (-Djava.security.manager) and you'll see an exception.

The result "147" is because Java usually optimizes method invocations.
It just happened that length() is declared as first method in
CharSequence, so instead of expected invocation of length() in Delegate,
Java just invokes a first method from it.

One solution to what you are trying to achieve is already mentioned by
Daniel -- implement CharSequence by Delegate and call it directly with a
Method passed to invoke().

Another solution is to use a Map<Method, Method> (most likely HashMap)
for mappings of source CharSequence methods into Delegate methods.

Alternatively you may generate "bridge" methods using e.g. ASM, or cglib.


piotr
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      10-01-2007
On Oct 1, 3:48 pm, Piotr Kobzda <(E-Mail Removed)> wrote:
> Stefan Ram wrote:
> > I might have made some mistake. But in this case, I would
> > expect an exception or an error. Instead silently the wrong
> > method »Base#dummy0()« is called. Why?

>
> Because you are trying to cheat Java doing something which is not
> normally allowed -- i.e. change private data of a Method. It succeeds
> because there is no security manager active, but run it with even
> default SM (-Djava.security.manager) and you'll see an exception.
>
> The result "147" is because Java usually optimizes method invocations.
> It just happened that length() is declared as first method in
> CharSequence, so instead of expected invocation of length() in Delegate,
> Java just invokes a first method from it.
>
> One solution to what you are trying to achieve is already mentioned by
> Daniel -- implement CharSequence by Delegate and call it directly with a
> Method passed to invoke().
>
> Another solution is to use a Map<Method, Method> (most likely HashMap)
> for mappings of source CharSequence methods into Delegate methods.
>
> Alternatively you may generate "bridge" methods using e.g. ASM, or cglib.
>
> piotr


Yet another, probably most appropriate, solution is to implement
CharSequence and delegate manually.

Proxy is use most useful for doing things between the calling code and
the called code, or deciding on targets (that already implement the
interfaces) after the call. I've used it to dispatch calls to a list
of targets, such as Listeners. In general, Proxy is a sophisticated
reflection tool, and should be avoided unless you have an excellent
reason to use it

 
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 on Proxy object? Sebastian Java 2 02-08-2012 02:16 PM
method_missing behaves differently when a method is called via reflection? Hartin, Brian Ruby 1 11-10-2006 10:16 PM
cleaning up some reflection / dynamic method invocation code Marc E Java 2 07-19-2006 02:00 AM
Invoking variable method with var args via reflection Faiser Java 3 08-18-2004 03:51 PM
Web Service invocation from behind proxy behind firewall Kumarforg ASP .Net Web Services 0 08-03-2004 07:15 AM



Advertisments