aspose file tools*
The moose likes Java in General and the fly likes Get an Object using reflection, how to cast it back to a specific type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get an Object using reflection, how to cast it back to a specific type? " Watch "Get an Object using reflection, how to cast it back to a specific type? " New topic
Author

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

Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 47


Thanks a lot!


Enjoy life!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

This is how you cast objects:
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Or is the question regarding how to cast back to an arbitrary type?
Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 47
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

Joined: Jan 10, 2002
Posts: 61434
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 47
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Jan 10, 2002
Posts: 61434
    
  67

Yes, please, "ninja edits" are not appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



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

Joined: Sep 29, 2008
Posts: 12617

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
 
subject: Get an Object using reflection, how to cast it back to a specific type?