is it possible to instantiate an object from its class type on demand ?

26 views
Skip to first unread message

zixzigma

unread,
Jan 7, 2011, 11:24:55 PM1/7/11
to Google Web Toolkit
with GWT.create(Foo.class),
it is possible to instantiate Foo object from Foo.class.

however GWT.create mandates the argument passed to create method to be
a class literal, known at compile time.

in cases where the argument is not a class literal, for example coming
from a map,
is there an alternative way to create object Foo from its Foo.class ?

for example, lets say we have a map populated like below:

// this map is populated
Map<ObjectA, Class<? extends Foo>> map;

Foo lookupFoo(ObjectA objectA) {

Class<? extends Foo> fooClazz = map.get(objectA);

return GWT.create(fooClazz);

}

this works in hosted mode, but doesnt compile.

is there any alternative way that this can be achieved without early
eagerly instantiation Foo objects ?

the good thing about GWT.create is that it creates the object on
demand. (i believe ?)

Jim Douglas

unread,
Jan 7, 2011, 11:39:24 PM1/7/11
to Google Web Toolkit
The documentation is pretty unambiguous on this point:

http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/core/client/GWT.html#create(java.lang.Class)

"The argument to create(Class) must be a class literal because the web
mode compiler must be able to statically determine the requested type
at compile-time. This can be tricky because using a Class variable may
appear to work correctly in hosted mode."

That restriction makes sense when you consider that GWT needs to
generate the appropriate JavaScript code for the specified Java class
at compile-time.

Gal Dolber

unread,
Jan 7, 2011, 11:40:09 PM1/7/11
to google-we...@googlegroups.com
Gwt doesn't support that.
GWT.create() only works with class literals.

If you know what classes you'll need you can write a method like this:

<T> T instantiate(Class<T> clazz) {
       if (clazz.equals(Foo.class)) {
             return new Foo();
       } else if (clazz.equals(Foo2.class)) {
             return Foo2();
       }
       throw new IllegalStateException("Class no supported: " + clazz.getName());
}

You also can put together a generator to create it for you: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=gwt+generator


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
Guit: Elegant, beautiful, modular and *production ready* gwt applications.

http://code.google.com/p/guit/




Thomas Broyer

unread,
Jan 8, 2011, 6:05:35 AM1/8/11
to google-we...@googlegroups.com
...or you can have a map from the Class<?> to an instance of a factory for that type (or GIN Provider<?>); or in this case, directly map from ObjectA to the factory/provider instead of to the Class (so that you don't have to do 2 lookups: ObjectA to Class, then Class to factory)
Reply all
Reply to author
Forward
0 new messages