isInstance() supported?

1,052 views
Skip to first unread message

Mike Wannamaker

unread,
Feb 15, 2007, 5:14:43 PM2/15/07
to Google-We...@googlegroups.com
I would like to do this

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

Chad Bourque

unread,
Feb 15, 2007, 5:30:14 PM2/15/07
to Google-We...@googlegroups.com
Mike,
 
Call me naive (I've not been doing Java that long), but what exactly is the difference between:
 
if (p_Class.isInstance(widget))
 
and
 
if (widget instanceOf p_Class)
 
or perhaps
 
if (widget instanceOf p_Class.getClass())
 
I'm using instanceOf in my RPC callback handlers without issue. So, I'm curious, what is the difference here?
 
Chad

 

Ian Petersen

unread,
Feb 15, 2007, 5:33:15 PM2/15/07
to Google-We...@googlegroups.com
> Any work arounds?

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

Jason Essington

unread,
Feb 15, 2007, 5:39:34 PM2/15/07
to Google-We...@googlegroups.com
The problem is that Class.isInstance() is getting a little too close
to reflection which is decidedly not supported by design in GWT.

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

Mike Wannamaker

unread,
Feb 16, 2007, 9:52:58 AM2/16/07
to Google-We...@googlegroups.com

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

Reinier Zwitserloot

unread,
Feb 16, 2007, 3:36:20 PM2/16/07
to Google Web Toolkit
Mike, the GWT way to dynamically determine type is as follows:

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.

Ian Petersen

unread,
Feb 16, 2007, 3:55:33 PM2/16/07
to Google-We...@googlegroups.com
> Mike, the GWT way to dynamically determine type is as follows:
>
> boolean b = SomeClassOrOther.class.isInstance(widget);
>
> becomes in GWT:
>
> boolean b =
> "com.yourcompany.SomeClassOrOther".equals(GWT.getTypeName(widget));

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

Reinier Zwitserloot

unread,
Feb 17, 2007, 9:55:00 AM2/17/07
to Google Web Toolkit
Ian:

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.

Ian Petersen

unread,
Feb 17, 2007, 1:22:36 PM2/17/07
to Google-We...@googlegroups.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 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.

Michel David

unread,
Apr 12, 2012, 5:19:45 AM4/12/12
to google-we...@googlegroups.com, Google-We...@googlegroups.com
You can easily replace myClass.isInstance(myInstance) by myClass.equals(myInstance.getClass())

Michel.
Reply all
Reply to author
Forward
0 new messages