Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.getMethod with variable args - part 2

 
Stephen Upton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stephen Upton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What JDK are you using?
 
Stephen Upton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.5.0_13 on Mac OS X
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephen Upton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic