Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I dynamic_cast jobject or do quick "instanceof"?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method that returns a jobjectArray that might be filled with other another jobjectArray or j<primitive-type>Array or simply jobjects. I want to use dynamic_cast to quickly know which method to call to get the data out of it but it doesn't seem to work. What's the best way to do this? (Since going in, the code can't know what it will encounter inside the jobjectArray)
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I vaguely remember dynamic_cast<type> as being,... well I do not want to get hammered with nothing to gain but I have seen this design challenge so many times and thought about it so many times that I do a switch on instanceof and if it gets to be more than three or four decisions, I do some refactoring.

I have found it to be more effective to put up some code while I think about it, thus some instanceof switching to get the concepts on viewable surface. In general I keep in mind that trying to do different logic based on differing types leads to strong motivation to fix the code on only three or four instanceof.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dynamic_cast only works for classes that have at least one virtual function (since the runtime type info (RTTI) for a class is stored with the virtual table, usually, and if there's no virtual functions, there's no vtbl.) If you look in jni.h, jclass, jarray, and friends have no methods, no nothing, in fact. So this isn't going to work.

I don't think there's any way around using the full JNI APIs to discover the type of your subarrays.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!