File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get an Object using reflection, how to cast it back to a specific type?

 
Ziyang Zhang
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks a lot!
 
Paul Clapham
Sheriff
Pie
Posts: 20184
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how you cast objects:
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or is the question regarding how to cast back to an arbitrary type?
 
Ziyang Zhang
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Or is the question regarding how to cast back to an arbitrary type?

Actually, "com.project.Package1.Bark" is stored in a string. I can not get this string until runtime.
I want to cast it back to the type stored in the string.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64184
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this an actual use case, or an academic exercise? If you don't know the class (or an interface) in advance, you can't have a reference variable of that class and therefore no need for a cast.

What's the actual need for this?
 
Paul Clapham
Sheriff
Pie
Posts: 20184
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, you can't do that. You can cast to any type known at compile time by the compiler, that's all.

What are you trying to achieve by this dynamic loading? There are various ways to deal with the issue but what you should do depends on your requirement.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ziyang Zhang wrote:
Actually, "com.project.Package1.Bark" is stored in a string. I can not get this string until runtime.
I want to cast it back to the type stored in the string.


This actually doesn't make much sense. There is no reason to cast something and not use it. Meaning you cast it to something so that it can be used -- either as a parameter to a method or to be assigned to a reference variable. And in both these cases, you need the type available at compile time -- either for the method or for the variable.

Henry
 
Ziyang Zhang
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Ziyang Zhang wrote:
Actually, "com.project.Package1.Bark" is stored in a string. I can not get this string until runtime.
I want to cast it back to the type stored in the string.


This actually doesn't make much sense. There is no reason to cast something and not use it. Meaning you cast it to something so that it can be used -- either as a parameter to a method or to be assigned to a reference variable. And in both these cases, you need the type available at compile time -- either for the method or for the variable.

Henry


Thank you very much for your response, Henry!
Maybe I didn't express my question well, I modified the question, please take a peek.
Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ziyang Zhang wrote:
Thank you very much for your response, Henry!
Maybe I didn't express my question well, I modified the question, please take a peek.
Thanks.


Okay, I will take a look, but in the future, please don't do that. It kinda messes up all the answers that you received so far -- as it is now for a question that has been changed.

Henry
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64184
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, please, "ninja edits" are not appreciated.
 
Paul Clapham
Sheriff
Pie
Posts: 20184
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ziyang Zhang wrote:Maybe I didn't express my question well, I modified the question, please take a peek.


Unfortunately the modified question isn't any better than the original question. All of the questions which people asked in response are still open and valid questions. So it's your turn to answer those questions before we can proceed.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Again, it doesn't make sense. Not only are you trying to trying to cast, you are also declaring a Bark variable. How the heck can you declare such a variable if you don't have the class in scope at compile time. Answer: You can't.

In other words, you can't even declare the Bark reference, much less try to cast the object to it.


So... how do you call the method? You need to use reflection. From the Class instance, you can get a Method instance for the method that you are trying to call. Using that Method instance, you can invoke the method, using the instance that you got back -- and without the need to cast.

Henry

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you know the name of the method you want to call, and it's *not* a String, then you should probably use an interface, and be done with all this making-Java-something-it-isn't stuff.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic