all groups > vj# > january 2005 >
You're in the

vj#

group:

Using reflection to invoke a Method


Using reflection to invoke a Method Andy Wu
1/24/2005 6:29:02 AM
vj#:
Hi,

I'm having trouble running the following example with J#. The call to
Method.invoke() results in an IllegalArgumentException which as far as I can
is wrong. Does anyone know how I can get around this? Where can I report this
as a bug?

import java.lang.reflect.*;

public class StringTest{

public StringTest(){}

public static boolean test( java.io.Serializable s ){
return true;
}

public static void main( String [] args ){

try{

Class [] params = new Class[] { java.io.Serializable.class };
StringTest st = new StringTest();
Method m = st.getClass().getMethod( "test", params );

Object [] theArgs = new Object[]{ "Foo" };
Object result = m.invoke( null, theArgs );

System.out.println( result );

}catch( Exception e ){
e.printStackTrace();
}

}


TIA

--
Re: Using reflection to invoke a Method Andy Wu
1/24/2005 11:37:02 AM
Thanks for your reply.

I did try the code on Sun Java first (I work predominantly with it as
opposed to VJ#) and it does work. The first argument passed to
Method.invoke() is 'null' because the method is a static method and therefore
isn't required. No harm in adding it though.

The second argument is the array of arguments to be used in invoking the
method. It's interesting that passing the Class [] to invoke() works where
passing the Object[] containing the String doesn't. Both java.lang.Class and
java.lang.String implement java.io.Serializable so both should work.

Here's an additional test that further exemplifies the problem:

...

public static boolean test2( java.lang.Comparable c ) {
return true;
}

public static void main( String [] args ) {
try {
boolean b = java.io.Serializable.class.isInstance( String.class );
System.out.println( "b : " + b );

Class [] params = new Class[] { java.lang.Comparable.class };
StringTest st = new StringTest();
Method m = st.getClass().getMethod( "test2", params );

Object [] theArgs = new Object[]{ "Foo" };
Object result = m.invoke( st, theArgs );

System.out.println( result );
}
catch( Exception e ) {
e.printStackTrace();
}

...

Convinced it's a bug yet? :)

Andy Wu

[quoted text, click to view]
Re: Using reflection to invoke a Method Lars-Inge Tønnessen [VJ# MVP]
1/24/2005 7:42:41 PM
Hi Andy,

I don't think this is a bug. (What you do is not working in Sun Java. J# and
Sun Java gives the same result.)

You forget to tell invoke what object you want to do the invoke on. You also
forget to give the correct arguments to the method.

Please see "//PLEASE SEE HERE" in the example.


import java.lang.reflect.*;
public class StringTest
{
public StringTest(){}

public static boolean test( java.io.Serializable s )
{
return true;
}

public static void main( String [] args )
{
try
{
Class [] params = new Class[] { java.io.Serializable.class };
StringTest st = new StringTest();
Method m = st.getClass().getMethod( "test", params );

// Object [] theArgs = new Object[]{ "Foo" };
//Object result = m.invoke( null, theArgs );

// PLEASE SEE HERE
Object result = m.invoke( st, params );
System.out.println( result );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}



Regards,
Lars-Inge Tønnessen


Re: Using reflection to invoke a Method Lars-Inge Tønnessen [VJ# MVP]
1/24/2005 9:32:03 PM

Hi Andy!


Sorry, I compiled the wrong code on my computer. My mistake. =:o)

The team is monitoring this newsgroup, so they will know. If you want to
submit a bug report you can do so on this web site:

http://lab.msdn.microsoft.com/productfeedback/


Best Regards,
Lars-Inge Tønnessen

Re: Using reflection to invoke a Method Andy Wu
1/25/2005 2:23:06 AM
Thanks for the link. I went ahead and registered this one as a bug. Hopefully
I'll receive a response.

Thanks again for your input.

Andy Wu

[quoted text, click to view]
Re: Using reflection to invoke a Method Andy Wu
1/25/2005 5:41:04 AM
FYI, I got a very prompt response from the bug report:

Thank you for the feedback. This is known issue for only these two scenarios
and is currently unsupported in this release. We will address this in the
next release.

Re: Using reflection to invoke a Method Lars-Inge Tønnessen [VJ# MVP]
1/25/2005 6:59:34 PM

Thanks for the info Andy! =:o)

AddThis Social Bookmark Button