This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic casting with object.getClass() ?

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have something a generic class:



In MyBean, I instantiate this generic class to a object of type 'Hobby':





How can I solve way 2 or way 3 ?
I cannot use way 1, as I have a generic method in which I need to put the cast explicitly by its given object-instance.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just change the reference from MyObject<?> o1 to MyObject<Hobby> 01 or MyObject<? super extends Hobby>
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just change the reference from MyObject<?> o1 to MyObject<Hobby> 01 or MyObject<? super Hobby>


Ineed, I can do that - But when doing this:




I can use this method only for objects of type Hobby and this is what I want to avoid !

I want to use this method for any type of MyObject.

I want to have something like an abstract generic Method which can be invoked from any type of MyObject.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my previous post I used super when I meant extends. I edited my post. When you use ? extends Hobby then you can use any class as long as it extends Hobby.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:I can use this method only for objects of type Hobby and this is what I want to avoid !

I want to use this method for any type of MyObject.

Apparently not, or you wouldn't have the cast to Hobby in there.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently not, or you wouldn't have the cast to Hobby in there.


Yes, I want to cast it to Hobby, but not always.

In other cases, I want cast it to, for example, Object of type 'Work'.

Imagine, I have this method and would cast the object 'o' to 'object_type':



So you see,
I want to avoid the if-clauses and cast the object directly as I have the 'object_type'-property, which indicates which type of class my object 'o' is.

Is this possible? Is there another better solution?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class has a method called cast but that will only make sense if you know the Class type. For instance, with a Class<Hobby> object "cls", cls.cast(x) will either return x cast to Hobby or throw an exception.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed,

I can do the cast via:




but that will only make sense if you know the Class type


Unfortunately, I cannot cast something like:



So the only solution is to use the if-clauses.

By the way, where lies the difference of

and


(faster?)
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I found the solution:


According to this:


http://www.velocityreviews.com/forums/t145694-p2-class-cast-object-what-benefit.html

I am able to do the cast this way:





So Bob, you are right, cls.cast will do the cast.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And check if the object is an instance (or null) at the same time, throwing a class cast exception if it isn't.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic