| Author |
generic casting with object.getClass() ?
|
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
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
Joined: Oct 25, 2008
Posts: 2700
|
|
Just change the reference from MyObject<?> o1 to MyObject<Hobby> 01 or MyObject<? super extends Hobby>
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
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
Joined: Oct 25, 2008
Posts: 2700
|
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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
Joined: Jun 28, 2008
Posts: 580
|
|
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
Joined: Jun 28, 2008
Posts: 580
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
And check if the object is an instance (or null) at the same time, throwing a class cast exception if it isn't.
|
 |
 |
|
|
subject: generic casting with object.getClass() ?
|
|
|