Monday, June 15, 2015

Re: How to search object instances by their type ?

Why don't you use instanceof? 
Good point, sorry I forgot some info: I want to use a general method that searches for the widget types.

Details:
I have my own Widget tree (its root is called Elemento ;), It's a kind of builder pattern, that constructs GWT Element instances (I hope I can still use it in gwt 3.0 ;). 
Anyway it's contains the visitor pattern, to visit all contained Elemento instances. So a Elemento panel will visit all contained Elemento instances.
The visitor will then use the following general static Util method  (work in progress) to discover if it concerns the correct type:

public static boolean isInstanceOf(final Class<?> type, final Object obj) {
               
if (isNotNull(type, obj)) {
                       
if (type.equals(obj.getClass())) {
                               
return true;
                       
}
                       
else {
                               
Class<? extends Object> cls = obj.getClass();
                               
while (isNotNull(cls) && !Object.class.equals(cls)) {
                                       
if (type.equals(cls)) {
                                               
return true;
                                       
}
                                        cls
= cls.getSuperclass();
                               
}
                       
}
               
}
               
return false;
       
}


The alternative would be to use a Visitor for every know type and use the instanceof operator (instanceof requires the Object name). 

I understand I can't use reflection ;), but was still hoping to still use some general approach.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment