Example usage for com.google.gwt.dev.shell.designtime ModuleSpace setThrownJavaException

List of usage examples for com.google.gwt.dev.shell.designtime ModuleSpace setThrownJavaException

Introduction

In this page you can find the example usage for com.google.gwt.dev.shell.designtime ModuleSpace setThrownJavaException.

Prototype

public static void setThrownJavaException(Throwable t) 

Source Link

Usage

From source file:com.google.gdt.eclipse.designer.ie.jsni.IDispatchImpl.java

License:Open Source License

@SuppressWarnings("unused")
private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags, int pDispParams, int pVarResult,
        int pExcepInfo, int pArgErr) {
    HResultException ex = null;/*from   w ww .jav  a 2s . c om*/
    Variant[] vArgs = null;
    Variant result = null;
    try {
        vArgs = extractVariantArrayFromDispParamsPtr(pDispParams);
        result = invoke(dispIdMember, dwFlags, vArgs);
        if (pVarResult != 0) {
            Utils.win32_copy(pVarResult, result);
        }
    } catch (HResultException e) {
        // Log to the console for detailed examination.
        //
        e.printStackTrace();
        ex = e;
    } catch (InvocationTargetException e) {
        // If we get here, it means an exception is being thrown from
        // Java back into JavaScript
        Throwable t = e.getTargetException();
        ex = new HResultException(t);
        ModuleSpace.setThrownJavaException(t);
    } catch (Exception e) {
        // Log to the console for detailed examination.
        //
        e.printStackTrace();
        ex = new HResultException(e);
    } finally {
        // We allocated variants for all arguments, so we must dispose them all.
        //
        for (int i = 0; i < vArgs.length; ++i) {
            if (vArgs[i] != null) {
                vArgs[i].dispose();
            }
        }
        if (result != null) {
            result.dispose();
        }
    }
    if (ex != null) {
        // Set up an exception for IE to throw.
        //
        ex.fillExcepInfo(pExcepInfo);
        return ex.getHResult();
    }
    return COM.S_OK;
}

From source file:com.google.gdt.eclipse.designer.moz.jsni.MethodDispatch.java

License:Open Source License

/**
 * Invoke a Java method from JavaScript. This is called solely from native
 * code.//from  ww  w.j a v  a 2  s  . co m
 * 
 * @param jsthis JavaScript reference to Java object
 * @param jsargs array of JavaScript values for parameters
 * @param returnValue JavaScript value to return result in
 * @throws RuntimeException if improper arguments are supplied
 * 
 * TODO(jat): lift most of this interface to platform-independent code (only
 * exceptions still need to be made platform-independent)
 */
protected void invoke0(JsValue jsthis, JsValue[] jsargs, JsValue returnValue) {
    ClassLoader classLoader = classLoaderRef.get();
    if (classLoader == null) {
        throw new RuntimeException("Invalid class loader.");
    }
    DispatchIdOracle dispatchIdOracle = dispatchIdOracleRef.get();
    if (dispatchIdOracle == null) {
        throw new RuntimeException("Invalid dispatch oracle.");
    }
    Class<?>[] paramTypes = method.getParameterTypes();
    int argc = paramTypes.length;
    Object args[] = new Object[argc];
    // too many arguments are ok: the extra will be silently ignored
    if (jsargs.length < argc) {
        throw new RuntimeException("Not enough arguments to " + method);
    }
    Object jthis = null;
    if (method.needsThis()) {
        jthis = JsValueGlue.get(jsthis, classLoader, method.getDeclaringClass(), "invoke this");
    }
    for (int i = 0; i < argc; ++i) {
        args[i] = JsValueGlue.get(jsargs[i], classLoader, paramTypes[i], "invoke arguments");
    }
    try {
        Object result;
        try {
            result = method.invoke(jthis, args);
        } catch (IllegalAccessException e) {
            // should never, ever happen
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        JsValueGlue.set(returnValue, classLoader, dispatchIdOracle, method.getReturnType(), result);
    } catch (InstantiationException e) {
        // If we get here, it means an exception is being thrown from
        // Java back into JavaScript
        Throwable t = e.getCause();
        // TODO(jat): if this was originally JavaScript exception, re-throw the
        // original exception rather than just a null.
        ModuleSpace.setThrownJavaException(t);
        LowLevelMoz.raiseJavaScriptException();
    } catch (InvocationTargetException e) {
        // If we get here, it means an exception is being thrown from
        // Java back into JavaScript
        Throwable t = e.getTargetException();
        // TODO(jat): if this was originally JavaScript exception, re-throw the
        // original exception rather than just a null.
        ModuleSpace.setThrownJavaException(t);
        LowLevelMoz.raiseJavaScriptException();
    } catch (IllegalArgumentException e) {
        // TODO(jat): log to treelogger instead? If so, how do I get to it?
        System.err.println("MethodDispatch.invoke, method=" + method.toString() + ": argument mismatch");
        for (int i = 0; i < argc; ++i) {
            System.err.println(" param " + i + " type is " + paramTypes[i].toString() + " value is type "
                    + jsargs[i].getTypeString() + " = " + args[i].toString());
        }
        throw e;
    }
}

From source file:com.google.gdt.eclipse.designer.webkit.jsni.MethodDispatch.java

License:Open Source License

public H invoke(H jsContext, H jsthisInt, List<H> jsargsInt, List<H> exception) {
    ClassLoader classLoader = classLoaderRef.get();
    if (classLoader == null) {
        throw new RuntimeException("Invalid class loader.");
    }/*from w  w  w .  ja v  a 2  s  .co  m*/
    DispatchIdOracle dispatchIdOracle = dispatchIdOracleRef.get();
    if (dispatchIdOracle == null) {
        throw new RuntimeException("Invalid dispatch oracle.");
    }
    LowLevelWebKit.pushJsContext(jsContext);
    JsValue jsthis = new JsValueWebKit<H>(jsthisInt);
    JsValue jsargs[] = new JsValue[jsargsInt.size()];
    for (int i = 0; i < jsargsInt.size(); ++i) {
        jsargs[i] = new JsValueWebKit<H>(jsargsInt.get(i));
    }
    JsValueWebKit<H> returnValue = new JsValueWebKit<H>();
    try {
        Class<?>[] paramTypes = method.getParameterTypes();
        int argc = paramTypes.length;
        Object args[] = new Object[argc];
        // too many arguments are ok: the extra will be silently ignored
        if (jsargs.length < argc) {
            throw new RuntimeException("Not enough arguments to " + method);
        }
        Object jthis = null;
        if (method.needsThis()) {
            jthis = JsValueGlue.get(jsthis, classLoader, method.getDeclaringClass(), "invoke this");
        }
        for (int i = 0; i < argc; ++i) {
            args[i] = JsValueGlue.get(jsargs[i], classLoader, paramTypes[i], "invoke args");
        }
        try {
            Object result;
            try {
                result = method.invoke(jthis, args);
            } catch (IllegalAccessException e) {
                // should never, ever happen
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            JsValueGlue.set(returnValue, classLoader, dispatchIdOracle, method.getReturnType(), result);
            H jsResult = returnValue.getJsValue();
            // Native code will eat an extra ref.
            LowLevelWebKit.gcProtect(jsContext, jsResult);
            return jsResult;
        } catch (InstantiationException e) {
            // If we get here, it means an exception is being thrown from
            // Java back into JavaScript
            ModuleSpace.setThrownJavaException(e.getCause());
            // Native code eats the same ref it gave us.
            exception.add(LowLevelWebKit.getJsNull(jsContext));
            // Native code eats the same ref it gave us.
            return LowLevelWebKit.getJsUndefined(jsContext);
        } catch (InvocationTargetException e) {
            // If we get here, it means an exception is being thrown from
            // Java back into JavaScript
            Throwable t = e.getTargetException();
            ModuleSpace.setThrownJavaException(t);
            // Native code eats the same ref it gave us.
            exception.add(LowLevelWebKit.getJsNull(jsContext));
            // Native code eats the same ref it gave us.
            return LowLevelWebKit.getJsUndefined(jsContext);
        }
    } finally {
        LowLevelWebKit.popJsContext(jsContext);
    }
}