• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Class.getMethod with variable args - part 2

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What JDK are you using?
 
Stephen Upton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.5.0_13 on Mac OS X
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
brevity is the soul of wit - shakepeare. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic