So basically I have List with some container, that contain generic object:
List<MyObj<?>> myObjList;
For example there can be MyObj<A>
and MyObj<B>
. And there some overloaded method todo(A a)
and todo(B b).
If i wanna call this method in cycle, I need hardcode casting to A
or B
:
for(MyObj<?> myObj: myObjList) {
todo( (A) myObj.body() )
}
Is there a way to casting using class literal A.class
?
I'm tried add class literal field to myObj
:
class myObj<T> {
Class<T> classLiteral;
T body;
}
and use .cast()
method:
for(MyObj<?> myObj: myObjList) {
todo(
myObj.getClassLiteral().cast( myObj.body() )
)
}
But it didn't work