Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

reflection and casting

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am calling java reflection api's "invoke(instance, null)" method, which returns an Object type. In my case at runtime this Object specifically is an array of a reference type. This reference type will change since I am calling invoke within a loop. For example, the first iteration would produce Cat[], the second iteration would produce Spaceship[], the third iteration would produce Pizza[], etc. I need to iterate through this array that I get back. When I try to convert the object I get back from invoke I get a "ClassCastException: [LCat; cannot be cast to java.lang.Iterable". How do I fix this, get an iterator from an Object that will always be a reference array at runtime? Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the return type of the method that you're calling via reflection (or are you calling different methods)? Can you show us your code?
 
peter pham
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:What is the return type of the method that you're calling via reflection (or are you calling different methods)? Can you show us your code?



It can be Cat[], Pizza[], etc, an array of references. Here's some code:


java reflection's invoke method returns an Object. At runtime, invoke here produces an array of objects(reference types). Because oneMethod becomes a different method with each iteration, this array of objects changes too. So at runtime objectArray could be Cat[], then Pizza[], then Student[], etc. My problem is that I need to iterate through this array(to call that specific object's getters, like getTailLength() for a Cat, getCheeseType() for Pizza, etc). However, the javadoc says invoke can only return an object type. I tried casting objectArray:

but got the ClassCastException: [LCat; cannot be cast to java.lang.iterable
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

peter pham wrote:
but got the ClassCastException: [LCat; cannot be cast to java.lang.iterable


That is because an array is not an Iterable. Why do you want an Iterable? Why not just cast to Object[] and iterate the array?
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter. To add to what Steve said you can iterate the object array and cast each item to the type you want, then call its methods. Something like this:



Perhaps you will want to include some conditional statements which help determine what you need to cast the object to, if you don't know at the time you execute this code.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic