how to get class intance from widget instance. in gwt, my widget is a private field of pojo class. i able to get the widget using instanceOfPojo.returnWidget(); method. May i know how to get instance class of the widget so that i can use for
if( widget instanceof CustomWidgetClass) ?
-- Let me rephrase
let say , AbcClass extends Composite, we can check whether match with
if(widget instanceof AbcClass) .
but let say we do not know the class AbcClass exist, but we can get instance of abcClass. with this instance of abcClass, how to use java to create similar class and we call it DefClass so that we can cast abcClass to it?
... so how to i use it compared with if( widget instanceof instanceOfPojo.returnWidget()) ?
I'm really having a hard time understanding what you are asking for. Are you trying to get a working equivalent of widget instanceof instanceOfPojo.returnWidget()
, which doesn't work because instanceof
only works with Class literals?
To see if the class of instanceOfPojo.returnWidget()
is a superclass of widget
's class, you could use isAssignableFrom:
instanceOfPojo.returnWidget().getClass().isAssignableFrom(widget.getClass());
To see if they are the exact same class:
instanceOfPojo.returnWidget().getClass() == widget.getClass()
If that's not what you mean, please try to clarify your question.
Edit: OP has since clarified the question.
... we can get instance of abcClass. with this instance of abcClass, how to use java to create similar class
To get an instance of a class and then create a similar class from it would require dynamically creating a class at run-time. Its possible in Java to dynamically create a class, but I very much doubt you could do it in GWT client code. This is because GWT supports only a subset of Java and gets translated to Javascript.
... we call it DefClass so that we can cast abcClass to it?
Since the class of abcClass
is the ("unknown") class AbcClass
and AbcClass
derives directly from Composite, you won't be able to cast abcClass
to this dynamic class DefClass
. DefClass
is not part of abcClass
inheritance hierarchy (AbcClass
-> Composite
-> Widget
-> UIObject
-> Object
) and you can't change that inheritance hierarchy to include DefClass
during runtime after the fact.