Search in sources :

Example 1 with ReflectionException

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

the class GwtReflectionUtils method callPrivateMethod.

public static <T> T callPrivateMethod(JavaScriptObject target, String methodName, String overlayOriginalType, Object... args) {
    if (overlayOriginalType == null) {
        throw new IllegalArgumentException("The specified overlay type should not be null");
    }
    overlayOriginalType = overlayOriginalType.trim();
    if (overlayOriginalType.equals("")) {
        throw new IllegalArgumentException("The specified overlay type should not be blank");
    }
    if (overlayOriginalType.endsWith("$")) {
        throw new IllegalArgumentException("The specified overlay type should not be a rewritten type : " + overlayOriginalType);
    }
    Class<?> rewrittenOverlayType = null;
    try {
        rewrittenOverlayType = Class.forName(overlayOriginalType + "$");
    } catch (ClassNotFoundException e) {
        throw new ReflectionException("Error while calling overlay rewritten method " + overlayOriginalType + "$." + methodName + "$(..) :", e);
    }
    Object[] newArgs = new Object[args.length + 1];
    newArgs[0] = target;
    System.arraycopy(args, 0, newArgs, 1, args.length);
    return callStaticMethod(rewrittenOverlayType, methodName + "$", newArgs);
}
Also used : ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject)

Example 2 with ReflectionException

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

the class GwtTestWithEasyMock method beforeGwtTestWithEasyMock.

@Before
public void beforeGwtTestWithEasyMock() {
    mockedClasses.forEach(clazz -> getMockManager().registerMock(clazz, createMock(clazz)));
    try {
        for (Field f : mockFields) {
            Object mock = getMockManager().getMock(f.getType());
            GwtReflectionUtils.makeAccessible(f);
            f.set(this, mock);
        }
    } catch (Exception e) {
        if (GwtTestException.class.isInstance(e)) {
            throw (GwtTestException) e;
        } else {
            throw new ReflectionException("Error during gwt-test-utils @Mock creation", e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) Before(org.junit.Before)

Example 3 with ReflectionException

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

the class UiBinderBeanUtils method populateObject.

/**
 * @param o
 * @param properties
 * @see BeanUtilsBean#populate(Object, Map)
 */
public static void populateObject(Object o, Map<String, Object> properties) {
    try {
        Map<String, Object> filteredProperties = new HashMap<>();
        for (String key : properties.keySet()) {
            if (PropertyUtils.isWriteable(o, key)) {
                filteredProperties.put(key, properties.get(key));
            }
        }
        UIBINDER_BEANUTILS.populate(o, filteredProperties);
    } catch (Exception e) {
        throw new ReflectionException("UiBinder error while setting properties for '" + o.getClass().getSimpleName() + "'", e);
    }
    // handle specifics
    String[] styles = (String[]) properties.get("addStyleNames");
    if (styles != null) {
        for (String style : styles) {
            if (o instanceof IsWidget) {
                ((IsWidget) o).asWidget().addStyleName(style);
            } else if (o instanceof UIObject) {
                ((UIObject) o).addStyleName(style);
            }
        }
    }
}
Also used : IsWidget(com.google.gwt.user.client.ui.IsWidget) ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) UIObject(com.google.gwt.user.client.ui.UIObject) HashMap(java.util.HashMap) UIObject(com.google.gwt.user.client.ui.UIObject) ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException)

Example 4 with ReflectionException

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

the class GwtReflectionUtils method callStaticMethod.

public static <T> T callStaticMethod(Class<?> clazz, String methodName, Object... args) {
    Method m = findMethod(clazz, methodName, args);
    if (m == null) {
        throw new ReflectionException("Cannot find method '" + clazz.getName() + "." + methodName + "(..)'");
    }
    try {
        m.setAccessible(true);
        Object res = m.invoke(null, args);
        return (T) res;
    } catch (Exception e) {
        throw new ReflectionException("Unable to call static method '" + m.toString() + "'", e);
    }
}
Also used : ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) UmbrellaException(com.google.web.bindery.event.shared.UmbrellaException)

Example 5 with ReflectionException

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

the class GwtReflectionUtils method callPrivateMethod.

public static <T> T callPrivateMethod(Object target, Method method, Object... args) {
    try {
        method.setAccessible(true);
        Object res = method.invoke(target, args);
        return (T) res;
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof GwtTestException) {
            throw (GwtTestException) e.getCause();
        } else if (e.getCause() instanceof AssertionError) {
            throw (AssertionError) e.getCause();
        } else if (e.getCause() instanceof UmbrellaException) {
            throw new ReflectionException("Error while calling method '" + method.toString() + "'", e.getCause().getCause());
        }
        throw new ReflectionException("Error while calling method '" + method.toString() + "'", e.getCause());
    } catch (Exception e) {
        throw new ReflectionException("Unable to call method '" + target.getClass().getSimpleName() + "." + method.getName() + "(..)'", e);
    }
}
Also used : ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) UmbrellaException(com.google.web.bindery.event.shared.UmbrellaException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) ReflectionException(com.googlecode.gwt.test.exceptions.ReflectionException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) UmbrellaException(com.google.web.bindery.event.shared.UmbrellaException)

Aggregations

ReflectionException (com.googlecode.gwt.test.exceptions.ReflectionException)5 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)3 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)3 UmbrellaException (com.google.web.bindery.event.shared.UmbrellaException)2 IsWidget (com.google.gwt.user.client.ui.IsWidget)1 UIObject (com.google.gwt.user.client.ui.UIObject)1 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1