if(p_Class.isInstance(widget))
{
p_List.add(widget);
}
It doesn't look like GWT supports this method, however I can do
if(widget instanceof TextBox)
Any work arounds?
Mike Wannamaker
Senior Software Developer
You'll probably have to encapsulate a map from Class to functor so
that you can do something like this:
if (DynamicInstanceOf.check(p_Class, widget)) {
p_List.add(widget);
}
where DynamicInstanceOf is defined like this:
public abstract class DynamicInstanceOf {
private static HashMap registry = new HashMap();
public static register(Class clazz, DynamicInstanceOf checker) {
registry.put(clazz, checker);
}
public static boolean check(Class clazz, Object o) {
return ((DynamicInstanceOf) registry.get(clazz)).check(o);
}
public abstract boolean check(Object o);
static {
register(MyClass.class, new DynamicInstanceOf() {
public boolean check(Object o) { return o instanceof MyClass; }
});
// etc.
}
}
Ian
--
Tired of pop-ups, security holes, and spyware?
Try Firefox: http://www.getfirefox.com
In fact none of the methods on Class are supported, the only reason
that Class is even in the JRE emulation library is so that deferred
binding (GWT.create()) can happen at compile time.
http://code.google.com/webtoolkit/documentation/java.lang.Class.html
-jason
Thanks to all for comments.
Jason,
In regards to Class.isInstance() method, from the javadoc "This method is the dynamic equivalent of the Java language instanceof operator." So I'm not sure why the code that is generated of (obj instanceof HasWidgets) cannot be used for HasWidgets.getClass().isInstance(obj);
Either way I had already implemented something similar to what Ian suggested.
Chad
The difference between the examples you gave below are your second two are incorrect. You can't do instanceof using the Class object. You have to use a Class by name IE: HasWidgets and not HasWidgets.getClass(). However you are correct that Class.isInstance() and instanceof are the same, however one acts on the Class object and one uses class types.
if (p_Class.isInstance(widget))
and
if (widget instanceOf p_Class)
or perhaps
if (widget instanceOf p_Class.getClass())
Mike Wannamaker
boolean b = SomeClassOrOther.class.isInstance(widget);
becomes in GWT:
boolean b =
"com.yourcompany.SomeClassOrOther".equals(GWT.getTypeName(widget));
WARNING: unlike isInstance, the GWT variant of this construct does NOT
return true on subclasses.
good luck.
I have to question this recommendation. How is using
if (<a class literal>.class.isInstance(o)) {
// do something
}
any more dynamic than
if (o instanceof <a class literal>) {
// do something
}
?
As far as I can see, you're checking against a compile-time constant
in either case. And, according to this:
> WARNING: unlike isInstance, the GWT variant of this construct does NOT
> return true on subclasses.
the method that uses .isInstance() is _less_ useful because GWT's
implementation of instanceof _does_ return true for subclasses.
Unless you meant it merely as an example when you used the
SomeClassOrOther class literal, rather than a variable like p_Class,
but, as Mike pointed out, the GWT emulation of java.lang.Class is
missing all of its instance methods.
Ian
--
Tired of pop-ups, security holes, and spyware?
Try Firefox: http://www.getfirefox.com
you're forgetting that you can use instanceof ONLY if you have a class
that you KNOW, at write-time. The Class.instanceOf idea (or the GWT
replacement for it) has no such restriction. You can e.g. make a
method that takes a widget and a list of class names and returns if
the widget is one of those. You can make this 'generic' (e.g: one
method that works for any list of class names and any widget). With
the instanceof keyword, you can't do this; you'd have to make one
method for each list of classes you do have.
I can't think of a good use case offhand for this, but someone asked,
and I answered. I'm tempted to say it's a sign of bad design, but I
don't know the details and I'm sure there's at least one good reason
for doing this. Maybe the one asking found it.
I don't want this to descend into a flamewar, so please understand I'm
not trying to criticize anyone or anything--I just want to better
understand what you've written.
I already know that instanceof requires write-time knowledge of the
class and that, as implemented in a full implementation of the Java
library, java.lang.Class.isInstance() does _not_ require write-time
knowledge of the class on which you're calling isInstance().
What I'm questioning is the benefit of using isInstance() in a GWT
client-side application. It appears that the JRE emulation library
for GWT does not include any instance methods for java.lang.Class,
which makes instances of Class nearly useless within GWT except as
tokens for use as keys in maps, or similar.
You suggested using a class literal as follows:
boolean b = SomeClassOrOther.class.isInstance(widget);
I figured, in response, that the GWT compiler must be able to figure
out that this is supposed to be equivalent to:
boolean b = widget instanceof SomeClassOrOther;
because we already know the compiler has special handling for class
literals like Foo.class. (This is how the compiler handles
GWT.create(Foo.class)--it knows that Foo.class is special.)
So, I'm assuming that you know that the GWT compiler can handle
Foo.class.isInstance(bar). It's already been established by Mike that
Class foo = Foo.class;
boolean b = foo.isInstance(bar);
won't work because foo doesn't have any instance methods (besides
those defined on Object).
So, since you can't store Foo.class in a variable (or pass it as a
parameter) and _then_ call isInstance() on it, I don't see the benefit
of (Foo.class.isInstance(bar)) over (bar instanceof Foo) especially
since you pointed out that, within GWT, isInstance() won't return true
for arguments that are instances of subclasses of the method's
subject.