Monday, September 6, 2010

Re: How can I get a class by it's name and then construct it?

Hi Shedokan,

you can do this on the server, not on the client. A few weeks ago,
I ran into the same problem.

Here is my solution:

-> send the string with the classname to the srever.
-> then do the following:

myClass = Class.forName(className);
Constructor myConstr = myClass.getDeclaredConstructor();
myModule = myConstr.newInstance();

Note that myModule is in my case an interface, and every class,
which you
construct with Reflection, implements this inteerface.

return myModule;

Return myModule/your generated class to the client/GWT, and you can
use
it there.

I think it´s a really nice solution. Just try it.

Greets Alex

On 3 Sep., 15:43, Shedokan <shedok...@gmail.com> wrote:
> Hello,
> I am trying to split my code and load classes by a name I get from the
> server.
> I tried using Class.forName("com.app.classes."+className) but GWT
> doesn't support it.
>
> How can I get a class and then construct it like this:
> new (getClass("com.app.classes."+className))("Param1", "param2");
>
> Thanks.

--
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-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment