com.googlecode.gwt.test.uibinder.UiBinderInvocationHandler Maven / Gradle / Ivy
package com.googlecode.gwt.test.uibinder;
import com.google.gwt.dev.Link;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.gwt.test.exceptions.GwtTestUiBinderException;
import com.googlecode.gwt.test.exceptions.ReflectionException;
import com.googlecode.gwt.test.utils.GwtReflectionUtils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
class UiBinderInvocationHandler implements InvocationHandler {
private static final Map, GwtEvent> EVENT_PROTOTYPES = new HashMap, GwtEvent>();
private final Class> proxiedClass;
UiBinderInvocationHandler(Class> proxiedClass) {
this.proxiedClass = proxiedClass;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("createAndBindUi")) {
return createAndBindUi(args[0]);
} else {
throw new GwtTestUiBinderException("Not managed method for UiBinder : " + method.getName());
}
}
@SuppressWarnings("unchecked")
private void addHandlers(Object owner) {
Map uiHandlerMethods = GwtReflectionUtils.getAnnotatedMethod(
owner.getClass(), UiHandler.class);
for (Map.Entry entry : uiHandlerMethods.entrySet()) {
for (String uiFieldName : entry.getValue().value()) {
Widget uiField = GwtReflectionUtils.getPrivateFieldValue(owner, uiFieldName);
GwtEvent.Type eventType = (GwtEvent.Type) getEventType(entry.getKey());
H handler = (H) createHandler(uiField, entry.getKey(), owner);
if (eventType instanceof DomEvent.Type) {
uiField.addDomHandler(handler, (DomEvent.Type) eventType);
} else {
// special case for ValueChangeEvent and HasValueChangeHandlers
if (uiField instanceof HasValueChangeHandlers
&& handler instanceof ValueChangeHandler) {
((HasValueChangeHandlers