我必须为GWT/GXT项目开发一个“通用”wigdet,为此我需要创建一个未知类型的对象实例。我发现了一种在开发模式下完美工作的方法,但只要我尝试编译我的项目并部署它,我会得到一个Only class literals may be used as arguments to GWT.create()
错误。GWT.create(clazz)“泛型”方法
这里是我做的一个样本:
public class GenericEditableGrid<M> extends Grid<M>{
private final ToolBar toolBar = new ToolBar();
private final TextButton newItemButton = new TextButton();
protected GridInlineEditing<M> editing;
private final Class<M> clazzM;
public GenericEditableGrid(Class<M> parametrizedClass, String gridTitle, ListStore<M> listStore, ColumnModel<M> cm) {
super(listStore, cm);
clazzM = parametrizedClass;
// ... then I create my widget
bind();
}
private void bind(){
newItemButton.addSelectHandler(new SelectEvent.SelectHandler() {
@Override
public void onSelect(SelectEvent selectEvent) {
editing.cancelEditing();
// it is the folliwing line which is the problem obviously
M element = GWT.create(clazzM);
getStore().add(0, element);
int index = 0;
editing.startEditing(new Grid.GridCell(getStore().indexOf(element), index));
}
});
}
}
这是我如何使用它在我的子类:
super(InternationalString.class, gridTitle, new ListStore<InternationalString>(isprops.key()), buildColumnModel());
基本上,我想知道是什么问题恰恰在于这种方法,最终我该如何做才能做好。
请注意,我关心的不仅仅是让它工作,更要以正确的方式去做。因为我可以避免使用处理子类中的GWT.create()方法的抽象方法的问题。但这不是我想要的设计,只是看起来不正确。
我不也得是什么这样的区别:
MyClass e = GWT.create(MyClass.class);
和:
Class<MyClass> clazz=MyClass.class;
MyClass e = GWT.create(clazz);
因为就我个人而言,我认为这是基本上我在做什么,它看起来像是一样的东西。不是吗?
为什么你叫'.getClass()'上getParameteriedClass()的'的结果。的getClass()' – StriplingWarrior
对不起它不应该有它只是我所做的只是在我张贴此线程一试。我更新了我的问题的代码。 – RadASM