Example usage for com.google.gwt.dev.shell.designtime JsValueGlue set

List of usage examples for com.google.gwt.dev.shell.designtime JsValueGlue set

Introduction

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

Prototype

public static void set(JsValue value, ClassLoader cl, DispatchIdOracle dispIdOracle, Class<?> type,
        Object obj) 

Source Link

Document

Set the underlying value.

Usage

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

License:Open Source License

/**
 * Converts a java object to its equivalent variant. A ClassLoader is passed here so that Handles
 * can be manipulated properly.//from  www  .  ja  v a  2 s  .  c  om
 */
public static Variant convertObjectToVariant(ClassLoader cl, DispatchIdOracle ora, Class<?> type, Object o) {
    if (type.equals(Variant.class)) {
        return (Variant) o;
    }
    JsValueIE6 jsValue = new JsValueIE6();
    JsValueGlue.set(jsValue, cl, ora, type, o);
    return jsValue.getVariant();
}

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

License:Open Source License

/**
 * Retrieve a field and store in the passed JsValue. This function is called
 * exclusively from native code./*from  w  w w  .  j a  va 2s . c o m*/
 * 
 * @param name name of the field to retrieve
 * @param jsValue a reference to the JsValue object to receive the value of
 *          the field
 */
public void getField(String member, int /*long*/ jsRootedValue) {
    JsValueMoz32/*64*/ jsValue = new JsValueMoz32/*64*/(jsRootedValue);
    ClassLoader classLoader = classLoaderRef.get();
    if (classLoader == null) {
        jsValue.setUndefined();
        return;
    }
    DispatchIdOracle dispIdOracle = dispIdOracleRef.get();
    if (dispIdOracle == null) {
        jsValue.setUndefined();
        return;
    }
    int dispId = getDispId(member);
    if (dispId < 0) {
        // no field by that name, return undefined
        jsValue.setUndefined();
        return;
    }
    if (javaDispatch.isField(dispId)) {
        Field field = javaDispatch.getField(dispId);
        JsValueGlue.set(jsValue, classLoader, dispIdOracle, field.getType(),
                javaDispatch.getFieldValue(dispId));
        return;
    } else {
        MethodAdaptor method = javaDispatch.getMethod(dispId);
        AccessibleObject obj = method.getUnderlyingObject();
        DispatchMethod32/*64*/ dispMethod = (DispatchMethod32/*64*/) WrappersCache
                .getWrapperForObject(classLoader, obj);
        if (dispMethod == null) {
            dispMethod = new MethodDispatch32/*64*/(classLoader, dispIdOracle, method);
            WrappersCache.putWrapperForObject(classLoader, obj, dispMethod);
        }
        jsValue.setWrappedFunction(method.toString(), dispMethod);
    }
}

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

License:Open Source License

/**
 * Retrieve a field and store in the passed JsValue. This function is called
 * exclusively from native code.//from w  w w. jav  a 2  s.c  o m
 * 
 * @param name name of the field to retrieve
 * @param jsValue a reference to the JsValue object to receive the value of
 *          the field
 */
public void getField(String member, long jsRootedValue) {
    JsValueMoz64 jsValue = new JsValueMoz64(jsRootedValue);
    ClassLoader classLoader = classLoaderRef.get();
    if (classLoader == null) {
        jsValue.setUndefined();
        return;
    }
    DispatchIdOracle dispIdOracle = dispIdOracleRef.get();
    if (dispIdOracle == null) {
        jsValue.setUndefined();
        return;
    }
    int dispId = getDispId(member);
    if (dispId < 0) {
        // no field by that name, return undefined
        jsValue.setUndefined();
        return;
    }
    if (javaDispatch.isField(dispId)) {
        Field field = javaDispatch.getField(dispId);
        JsValueGlue.set(jsValue, classLoader, dispIdOracle, field.getType(),
                javaDispatch.getFieldValue(dispId));
        return;
    } else {
        MethodAdaptor method = javaDispatch.getMethod(dispId);
        AccessibleObject obj = method.getUnderlyingObject();
        DispatchMethod64 dispMethod = (DispatchMethod64) WrappersCache.getWrapperForObject(classLoader, obj);
        if (dispMethod == null) {
            dispMethod = new MethodDispatch64(classLoader, dispIdOracle, method);
            WrappersCache.putWrapperForObject(classLoader, obj, dispMethod);
        }
        jsValue.setWrappedFunction(method.toString(), dispMethod);
    }
}

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  w w  w .  jav  a  2 s.  c  o  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.moz.jsni.ModuleSpaceMoz32.java

License:Open Source License

/**
 * Invokes a native JavaScript function.
 * //  w  w  w .ja v  a2 s  . co  m
 * @param name the name of the function to invoke
 * @param jthis the function's 'this' context
 * @param types the type of each argument
 * @param args the arguments to be passed
 * @return the return value as a Object.
 */
@Override
protected JsValue doInvoke(String name, Object jthis, Class<?>[] types, Object[] args) throws Exception {
    ClassLoader isolatedClassLoader = getIsolatedClassLoader();
    DispatchIdOracle dispatchIdOracle = getDispatchIdOracle();

    JsValueMoz32/*64*/ jsthis = new JsValueMoz32/*64*/();
    Class<?> jthisType = (jthis == null) ? Object.class : jthis.getClass();
    JsValueGlue.set(jsthis, isolatedClassLoader, dispatchIdOracle, jthisType, jthis);

    int argc = args.length;
    JsValueMoz32/*64*/ argv[] = new JsValueMoz32/*64*/[argc];
    int /*long*/[] jsArgsInt = new int /*long*/[argc];
    for (int i = 0; i < argc; ++i) {
        argv[i] = new JsValueMoz32/*64*/();
        JsValueGlue.set(argv[i], isolatedClassLoader, dispatchIdOracle, types[i], args[i]);
        jsArgsInt[i] = argv[i].getJsRootedValue();
    }
    JsValueMoz32/*64*/ returnVal = new JsValueMoz32/*64*/();
    LowLevelMoz32/*64*/.invoke(window, name, jsthis.getJsRootedValue(), jsArgsInt,
            returnVal.getJsRootedValue());
    return returnVal;
}

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

License:Open Source License

/**
 * Invokes a native JavaScript function.
 * /* w  ww.  j a va 2  s  . c  o  m*/
 * @param name the name of the function to invoke
 * @param jthis the function's 'this' context
 * @param types the type of each argument
 * @param args the arguments to be passed
 * @return the return value as a Object.
 */
@Override
protected JsValue doInvoke(String name, Object jthis, Class<?>[] types, Object[] args) throws Exception {
    ClassLoader isolatedClassLoader = getIsolatedClassLoader();
    DispatchIdOracle dispatchIdOracle = getDispatchIdOracle();

    JsValueMoz64 jsthis = new JsValueMoz64();
    Class<?> jthisType = (jthis == null) ? Object.class : jthis.getClass();
    JsValueGlue.set(jsthis, isolatedClassLoader, dispatchIdOracle, jthisType, jthis);

    int argc = args.length;
    JsValueMoz64 argv[] = new JsValueMoz64[argc];
    long[] jsArgsInt = new long[argc];
    for (int i = 0; i < argc; ++i) {
        argv[i] = new JsValueMoz64();
        JsValueGlue.set(argv[i], isolatedClassLoader, dispatchIdOracle, types[i], args[i]);
        jsArgsInt[i] = argv[i].getJsRootedValue();
    }
    JsValueMoz64 returnVal = new JsValueMoz64();
    LowLevelMoz64.invoke(window, name, jsthis.getJsRootedValue(), jsArgsInt, returnVal.getJsRootedValue());
    return returnVal;
}

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. j  a  va  2  s. c  o  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);
    }
}

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

License:Open Source License

/**
 * Invokes a native JavaScript function.
 * /*from ww w .  j av a2  s . co  m*/
 * @param name
 *          the name of the function to invoke
 * @param jthis
 *          the function's 'this' context
 * @param types
 *          the type of each argument
 * @param args
 *          the arguments to be passed
 * @return the return value as a Object.
 */
@Override
protected JsValue doInvoke(String name, Object jthis, Class<?>[] types, Object[] args) throws Exception {
    ClassLoader isolatedClassLoader = getIsolatedClassLoader();
    DispatchIdOracle dispatchIdOracle = getDispatchIdOracle();
    JsValueWebKit<H> jsValueThis = new JsValueWebKit<H>();
    Class<?> jthisType = jthis == null ? Object.class : jthis.getClass();
    JsValueGlue.set(jsValueThis, isolatedClassLoader, dispatchIdOracle, jthisType, jthis);
    H jsthis = jsValueThis.getJsValue();
    int argc = args.length;
    List<H> argv = new ArrayList<H>(argc);
    for (int i = 0; i < argc; ++i) {
        JsValueWebKit<H> jsValue = new JsValueWebKit<H>();
        JsValueGlue.set(jsValue, isolatedClassLoader, dispatchIdOracle, types[i], args[i]);
        argv.add(jsValue.getJsValue());
    }
    final H curJsContext = LowLevelWebKit.getCurrentJsContext();
    H result = LowLevelWebKit.invoke(curJsContext, globalObject, name, jsthis, argv);
    return new JsValueWebKit<H>(result);
}

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

License:Open Source License

public H getField(H jsContext, String name) {
    LowLevelWebKit.pushJsContext(jsContext);
    ClassLoader classLoader = classLoaderRef.get();
    if (classLoader == null) {
        return LowLevelWebKit.getJsUndefined(jsContext);
    }/*from ww w.  j a v  a  2  s  .c  om*/
    DispatchIdOracle dispIdOracle = dispIdOracleRef.get();
    if (dispIdOracle == null) {
        return LowLevelWebKit.getJsUndefined(jsContext);
    }
    try {
        int dispId = getDispId(name);
        if (dispId < 0) {
            return LowLevelWebKit.getJsUndefined(jsContext);
        }
        if (javaDispatch.isField(dispId)) {
            Field field = javaDispatch.getField(dispId);
            JsValueWebKit<H> jsValue = new JsValueWebKit<H>();
            JsValueGlue.set(jsValue, classLoader, dispIdOracle, field.getType(),
                    javaDispatch.getFieldValue(dispId));
            H jsval = jsValue.getJsValue();
            // Native code will eat an extra ref.
            LowLevelWebKit.gcProtect(jsContext, jsval);
            return jsval;
        } else {
            MethodAdaptor method = javaDispatch.getMethod(dispId);
            AccessibleObject obj = method.getUnderlyingObject();
            DispatchMethod<?> dispMethod = (DispatchMethod<?>) WrappersCache.getWrapperForObject(classLoader,
                    obj);
            if (dispMethod == null) {
                dispMethod = new MethodDispatch<H>(classLoader, dispIdOracle, method);
                WrappersCache.putWrapperForObject(classLoader, obj, dispMethod);
            }
            // Native code eats the same ref it gave us.
            return LowLevelWebKit.wrapDispatchMethod(jsContext, method.toString(), dispMethod);
        }
    } finally {
        LowLevelWebKit.popJsContext(jsContext);
    }
}