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;
}
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 + "'");
}
}
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();
}
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);
}
}
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() + "'");
}
}
Aggregations