Search in sources :

Example 1 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException 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)

Example 2 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException in project gwt-test-utils by gwt-test-utils.

the class UiBinderInvocationHandler method findHandlerClass.

@SuppressWarnings("unchecked")
private Class<EventHandler> findHandlerClass(Method uiHandlerMethod) {
    Class<?> eventTypeClass = uiHandlerMethod.getParameterTypes()[0];
    String eventHandlerClassName = eventTypeClass.getName().substring(0, eventTypeClass.getName().lastIndexOf("Event")) + "Handler";
    try {
        return (Class<EventHandler>) GwtReflectionUtils.getClass(eventHandlerClassName);
    } catch (ClassNotFoundException e) {
    }
    String anotherEventHandlerClassName = eventTypeClass.getName() + ".Handler";
    try {
        return (Class<EventHandler>) GwtReflectionUtils.getClass(anotherEventHandlerClassName);
    } catch (ClassNotFoundException e) {
        // should never happen
        throw new GwtTestUiBinderException("Cannot find handler class for event type '" + eventTypeClass.getName() + "'. By convention, it should be '" + eventHandlerClassName + "' or '" + anotherEventHandlerClassName + "'");
    }
}
Also used : GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException)

Example 3 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException in project gwt-test-utils by gwt-test-utils.

the class UiBinderParser method createUiComponent.

/**
     * Parse the .ui.xml file to fill the corresponding objects.
     *
     * @param rootComponentClass the root component's class that UiBinder has to instanciated.
     * @param uiBinderClass      the UiBinder subinterface which is used
     * @param owner              The owner of the UiBinder template, with {@link UiField} fields.
     */
<T> T createUiComponent(Class<UiBinder<?, ?>> uiBinderClass, Object owner) {
    @SuppressWarnings("unchecked") Class<T> rootComponentClass = (Class<T>) getRootElementClass(uiBinderClass);
    InputStream uiXmlStream = getUiXmlFile(owner.getClass(), uiBinderClass);
    if (uiXmlStream == null) {
        throw new GwtTestUiBinderException("Cannot find the .ui.xml file corresponding to '" + owner.getClass().getName() + "'");
    }
    UiXmlContentHandler<T> contentHandler = new UiXmlContentHandler<T>(rootComponentClass, owner);
    XMLReader saxReader = XmlUtils.newXMLReader();
    try {
        saxReader.setContentHandler(contentHandler);
        saxReader.parse(new InputSource(uiXmlStream));
    } catch (Exception e) {
        if (GwtTestException.class.isInstance(e)) {
            throw (GwtTestException) e;
        } else {
            throw new GwtTestUiBinderException("Error while parsing '" + owner.getClass().getSimpleName() + ".ui.xml'", e);
        }
    } finally {
        try {
            uiXmlStream.close();
        } catch (IOException e) {
        // do nothing
        }
    }
    return contentHandler.getRootComponent();
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) IOException(java.io.IOException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) IOException(java.io.IOException) GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) XMLReader(org.xml.sax.XMLReader)

Example 4 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException in project gwt-test-utils by gwt-test-utils.

the class UiTagBuilder method createUiTag.

private UiTag<?> createUiTag(String nameSpaceURI, String localName, Attributes attributesXML, UiTag<?> parentTag) {
    Map<String, Object> attributes = parseAttributesMap(nameSpaceURI, attributesXML);
    localName = localName.replaceAll("\\.", "\\$");
    int i = nameSpaceURI.lastIndexOf(':');
    if (i > 0 && Character.isUpperCase(localName.charAt(0))) {
        // the element should represent a Widget Class
        String className = nameSpaceURI.substring(i + 1) + "." + localName;
        Class<?> clazz = null;
        try {
            clazz = GwtReflectionUtils.getClass(className);
        } catch (ClassNotFoundException e) {
            throw new GwtTestUiBinderException("Cannot find class '" + className + "' declared in file '" + owner.getClass().getSimpleName() + ".ui.xml");
        }
        if (UIObject.class.isAssignableFrom(clazz) || IsWidget.class.isAssignableFrom(clazz)) {
            UiObjectTag<Object> uibinderTag = DefaultUiWidgetTagFactory.get().createUiObjectTag(clazz, attributes);
            uibinderTag.startTag(clazz, attributes, parentTag, owner);
            return uibinderTag;
        } else {
            throw new GwtTestUiBinderException("Not managed UiBinder type '" + clazz + "' declared in file '" + owner.getClass().getSimpleName() + ".ui.xml" + "', only implementation of '" + IsWidget.class.getName() + "' or subclass of '" + UIObject.class.getName() + "' are allowed");
        }
    } else if (UiBinderXmlUtils.isResourceTag(nameSpaceURI, localName)) {
        return resourceManager.registerResource(localName, attributes, parentTag, owner);
    } else if (UiBinderXmlUtils.isImportTag(nameSpaceURI, localName)) {
        return resourceManager.registerImport(attributes, parentTag, owner);
    } else if (UiBinderXmlUtils.isMsgTag(nameSpaceURI, localName)) {
        return resourceManager.registerMsg(attributes, parentTag, attributes);
    } else if (UiBinderXmlUtils.isTextTag(nameSpaceURI, localName)) {
        return resourceManager.registerText(attributes, parentTag, attributes);
    } else {
        return new UiElementTag(nameSpaceURI, localName, attributes, parentTag, owner);
    }
}
Also used : IsWidget(com.google.gwt.user.client.ui.IsWidget) GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) UIObject(com.google.gwt.user.client.ui.UIObject) UIObject(com.google.gwt.user.client.ui.UIObject)

Example 5 with GwtTestUiBinderException

use of com.googlecode.gwt.test.exceptions.GwtTestUiBinderException in project gwt-test-utils by gwt-test-utils.

the class UiBinderInstanciator method getObjectFromUiFactory.

private static <U> U getObjectFromUiFactory(Class<U> clazz, Object owner) {
    Map<Method, UiFactory> map = GwtReflectionUtils.getAnnotatedMethod(owner.getClass(), UiFactory.class);
    List<Method> compatibleFactories = new ArrayList<Method>();
    for (Method factoryMethod : map.keySet()) {
        if (clazz.isAssignableFrom(factoryMethod.getReturnType())) {
            compatibleFactories.add(factoryMethod);
        }
    }
    switch(compatibleFactories.size()) {
        case 0:
            return null;
        case 1:
            return (U) GwtReflectionUtils.callPrivateMethod(owner, compatibleFactories.get(0));
        default:
            throw new GwtTestUiBinderException("Duplicate factory in class '" + owner.getClass().getName() + " for type '" + clazz.getName() + "'");
    }
}
Also used : GwtTestUiBinderException(com.googlecode.gwt.test.exceptions.GwtTestUiBinderException) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) UiFactory(com.google.gwt.uibinder.client.UiFactory)

Aggregations

GwtTestUiBinderException (com.googlecode.gwt.test.exceptions.GwtTestUiBinderException)8 UIObject (com.google.gwt.user.client.ui.UIObject)5 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)2 Element (com.google.gwt.dom.client.Element)1 UiConstructor (com.google.gwt.uibinder.client.UiConstructor)1 UiFactory (com.google.gwt.uibinder.client.UiFactory)1 IsWidget (com.google.gwt.user.client.ui.IsWidget)1 ResourcePrototypeProxyBuilder (com.googlecode.gwt.test.internal.resources.ResourcePrototypeProxyBuilder)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 InputSource (org.xml.sax.InputSource)1 XMLReader (org.xml.sax.XMLReader)1