PDA

View Full Version : load Dynamic Classes



cabster
20 Nov 2009, 5:32 AM
Hello there,

Today i wanted to load modules dynamic but my dream was cancelled pretty quick when i realized that Class.forName was not supported in GWT.

I really hope somebody can help me load classes dynamecly with just the full class name. (com.test.MyClass)

This is the code that i'm using.


private void loadModule(String[] modules) {

for (int i = 0; i < modules.length; i++) {
try {

//initialize class
Class<?> currentClass = Class.forName(modules[i]);

Class<?> c = Class.forName(currentClass.getName(), true,
currentClass.getClassLoader());
Object instance = c.newInstance();

//add module menu items
if (instance instanceof IModule) {
this.addModuleMenuItems((IModule) instance);
this.addModuleShortcuts((IModule) instance);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}

}

}

sven
20 Nov 2009, 5:39 AM
This is not possible and cannot be by definition how the compiler works.

You will need to do something like this:


String name = class.getName();
if(name.equals("x")){
return new X();
} else if(name.equals("y")){
return new Y();
}



The code thing is that you could write a generator for this, so you dont need to do the programming byh and.

cabster
20 Nov 2009, 2:36 PM
oke thank you...shame that there is no other way.