wood burning stoves 2.0*
The moose likes Java in General and the fly likes Class.getMethod with variable args - part 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class.getMethod with variable args - part 2" Watch "Class.getMethod with variable args - part 2" New topic
Author

Class.getMethod with variable args - part 2

Stephen Upton
Greenhorn

Joined: Dec 26, 2005
Posts: 9
Previously, I received an answer on this (see thread topic without the part 2), where I was struggling to find the correct syntax to invoke a method that had a variable argument list using reflection, e.g.,



and I wanted to use reflection to call this method (since I won't know the method name until runtime).

The previous answer was this:


However, I'm still struggling with how to invoke this. I've tried:


I get an warning saying I need to cast the ss to Object[], but then when I do that, I get a ClassCastException when the m.invoke is executed.

Hope that explains enough.
thanks!
steve upton
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The previous topic is here. Normally it's preferable to post a followup like this in the same thread as the original. It's easier for one person (you) to do the work of finding the original thread, rather than each reader having to find it themselves.

Can you show what is AasClass? How did you obtain it? If you want the class object associated with A, the easy way is using a class literal: A.class.

This code seems to invoke the method just fine. If it's not working for you, can you show the exact error message?


"I'm not back." - Bill Harding, Twister
Stephen Upton
Greenhorn

Joined: Dec 26, 2005
Posts: 9
Hi Jim,

Thanks! I'll work on keeping the topics in the same thread. ;-)

Actually, the problem is more complicated than I posted. I don't know what class A is. I was actually trying to call different methods with differing number of parameters (all Strings, though), so I think I found a way around that complication. However, here's the code I'm actually working with that throws a IllegalAccessArgument (error below) (actionParameters is a String[]):



and here's the error it throws on the invoke line:



Thank you all for your help.
steve
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
What is actionParameters? Is it a String[], or an Object[]? I suspect it's an Object[], and the method requires a String[] - thus you're getting a ClassCastException.
Stephen Upton
Greenhorn

Joined: Dec 26, 2005
Posts: 9
Hi Jim,

actionParameters is a String[]. If I don't cast to Object[], I get a warning from Eclipse, and if ignore the warning, I still get the same exception.

Oh, well on to change the code to something else that works or to try an experiment on something simple...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What JDK are you using?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stephen Upton
Greenhorn

Joined: Dec 26, 2005
Posts: 9
1.5.0_13 on Mac OS X
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
actionParameters is a String[]. If I don't cast to Object[], I get a warning from Eclipse, and if ignore the warning, I still get the same exception.

You cannot cast an array to another array as there is no hierarchy amongst arrays. You can only cast an array to Object.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Stephen Upton
Greenhorn

Joined: Dec 26, 2005
Posts: 9
Hi Roger,

You cannot cast an array to another array as there is no hierarchy amongst arrays. You can only cast an array to Object.


I think you hit the nail on the head! After some reflection (pun intended), I realized I was trying to shortcut calling a number of methods using the varargs, but now realize there should still be only one method match.

Thank y'all for your help!
steve
p.s. I LOVE JavaRanch!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Roger Chung-Wee:

You cannot cast an array to another array as there is no hierarchy amongst arrays. You can only cast an array to Object.


That's not true - Java arrays are covariant, meaning that the following is perfectly legal:

Object[] array = new String[1];

Now, if you try to put, say, a number into that array, you will get an ArrayStoreException, but that's a different kind of beast from a ClassCastException, obviously...
 
 
subject: Class.getMethod with variable args - part 2