/**
* Adds <code>JField</code> with given name and @UiField annotation into "form" <code>JType</code>
* .
*/
private void addFormJField(Class<?> componentClass, String name) throws Exception {
IDevModeBridge bridge = m_context.getState().getDevModeBridge();
ClassLoader devClassLoader = bridge.getDevClassLoader();
// prepare "form" JType
Object formType = bridge.findJType(m_context.getFormType().getFullyQualifiedName());
// prepare @UiField annotation instance
Class<?> uiFieldAnnotationClass =
devClassLoader.loadClass("com.google.gwt.uibinder.client.UiField");
java.lang.annotation.Annotation annotation =
(java.lang.annotation.Annotation) Proxy.newProxyInstance(
uiFieldAnnotationClass.getClassLoader(),
new Class[]{uiFieldAnnotationClass},
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) {
return Boolean.TRUE;
}
});
// add new JField
Object newField;
{
Constructor<?> fieldConstructor;
Class<?> fieldClass;
if (m_context.getState().getVersion().isHigherOrSame(Utils.GWT_2_2)) {
fieldClass = devClassLoader.loadClass("com.google.gwt.dev.javac.typemodel.JField");
fieldConstructor =
ReflectionUtils.getConstructorBySignature(
fieldClass,
"<init>(com.google.gwt.dev.javac.typemodel.JClassType,java.lang.String,java.util.Map)");
} else {
fieldClass = devClassLoader.loadClass("com.google.gwt.core.ext.typeinfo.JField");
fieldConstructor =
ReflectionUtils.getConstructorBySignature(
fieldClass,
"<init>(com.google.gwt.core.ext.typeinfo.JClassType,java.lang.String,java.util.Map)");
}
newField =
fieldConstructor.newInstance(
formType,
name,
ImmutableMap.of(uiFieldAnnotationClass, annotation));
}
// set "widget" JType for JField
Object widgetType = bridge.findJType(ReflectionUtils.getCanonicalName(componentClass));
ReflectionUtils.invokeMethod(
newField,
"setType(com.google.gwt.core.ext.typeinfo.JType)",
widgetType);
}