Search in sources :

Example 1 with UiConstructor

use of com.google.gwt.uibinder.client.UiConstructor in project gwt-test-utils by gwt-test-utils.

the class UiBinderInstanciator method getObjectFromUiConstructor.

private static <U> U getObjectFromUiConstructor(Class<U> clazz, Map<String, Object> attributes) {
    for (Constructor<?> cons : clazz.getDeclaredConstructors()) {
        if (cons.getAnnotation(UiConstructor.class) == null) {
            continue;
        }
        Constructor<U> uiConstructor = (Constructor<U>) cons;
        String[] argNames = GwtParanamer.get().lookupParameterNames(uiConstructor);
        if (allArgsAreDeclaredInUiFile(argNames, attributes)) {
            List<Object> constructorArgs = extractArgs(argNames, attributes);
            if (constructorArgs != null) {
                Class<?>[] paramTypes = uiConstructor.getParameterTypes();
                for (int i = 0; i < argNames.length; i++) {
                    String argName = argNames[i];
                    try {
                        Object convertedArg = convertArgs(paramTypes[i], constructorArgs.get(i));
                        constructorArgs.set(i, convertedArg);
                        // remove the attribute from the map to populate the widget with since it's
                        // used in the constructor
                        attributes.remove(argName);
                    } catch (Exception e) {
                        throw new GwtTestUiBinderException("Error while converting argument " + argNames[i] + " to " + paramTypes[i], e);
                    }
                }
            }
            return instanciate(uiConstructor, constructorArgs);
        }
    }
    return null;
}
Also used : Constructor(java.lang.reflect.Constructor) UiConstructor(com.google.gwt.uibinder.client.UiConstructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) UIObject(com.google.gwt.user.client.ui.UIObject) UiConstructor(com.google.gwt.uibinder.client.UiConstructor)

Aggregations

UiConstructor (com.google.gwt.uibinder.client.UiConstructor)1 UIObject (com.google.gwt.user.client.ui.UIObject)1 GwtTestUiBinderException (com.googlecode.gwt.test.exceptions.GwtTestUiBinderException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1