Example usage for com.google.gwt.dev.shell.designtime WrappersCache putWrapperForObject

List of usage examples for com.google.gwt.dev.shell.designtime WrappersCache putWrapperForObject

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public static void putWrapperForObject(ClassLoader cl, Object javaObject, Object wrapper) 

Source Link

Document

Weakly caches a wrapper for a given Java Object.

Usage

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

License:Open Source License

@Override
protected Variant invoke(int dispId, int flags, Variant[] params)
        throws HResultException, InstantiationException, InvocationTargetException {
    ClassLoader classLoader = classLoaderRef.get();
    if (classLoader == null) {
        throw new RuntimeException("Invalid class loader.");
    }//from   www  .  ja  v  a2s .  c o m
    DispatchIdOracle dispIdOracle = dispIdOracleRef.get();
    if (dispIdOracle == null) {
        throw new RuntimeException("Invalid dispatch oracle.");
    }
    try {
        // GWT 2: called static scope with dispId as parameter
        if (dispId == 0 && (flags & COM.DISPATCH_METHOD) != 0 && params.length >= 2) {
            Variant dispIdVar = params[0]; // zero is dispId, next should be null (as 'this') for static context
            dispId = dispIdVar.getInt();
            if (javaDispatch.isMethod(dispId)) {
                MethodAdaptor method = javaDispatch.getMethod(dispId);
                Object target = getTarget();
                Object jthis = method.needsThis()
                        ? JsValueGlue.get(new JsValueIE6(params[1]), classLoader, method.getDeclaringClass(),
                                "this")
                        : null;
                Variant[] otherParams = new Variant[params.length - 2];
                System.arraycopy(params, 2, otherParams, 0, otherParams.length);
                return callMethod(classLoader, dispIdOracle, jthis, otherParams, method);
            }
        }
        // Whatever the caller asks for, try to find it via reflection.
        //
        if (dispId == DISPID_MAGIC_GETGLOBALREF && myGlobalRef != 0) {
            // Handle specially.
            //
            return new Variant(myGlobalRef);
        } else if (dispId == 0) {
            if ((flags & COM.DISPATCH_METHOD) != 0) {
                // implicit call -- "m()"
                // not supported -- fall through to unsupported failure
            } else if ((flags & COM.DISPATCH_PROPERTYGET) != 0) {
                // implicit toString -- "'foo' + m"
                return new Variant(getTarget().toString());
            }
        } else if (dispId > 0) {
            if (javaDispatch.isMethod(dispId)) {
                MethodAdaptor method = javaDispatch.getMethod(dispId);
                if ((flags & COM.DISPATCH_METHOD) != 0) {
                    // This is a method call.
                    return callMethod(classLoader, dispIdOracle, getTarget(), params, method);
                } else if (flags == COM.DISPATCH_PROPERTYGET) {
                    // The function is being accessed as a property.
                    AccessibleObject obj = method.getUnderlyingObject();
                    IDispatchImpl dispMethod = (IDispatchImpl) WrappersCache.getWrapperForObject(classLoader,
                            obj);
                    if (dispMethod == null || dispMethod.refCount < 1) {
                        dispMethod = new MethodDispatch(classLoader, dispIdOracle, method);
                        WrappersCache.putWrapperForObject(classLoader, obj, dispMethod);
                    }
                    IDispatch disp = new IDispatch(dispMethod.getAddress());
                    disp.AddRef();
                    return new Variant(disp);
                }
            } else if (javaDispatch.isField(dispId)) {
                Field field = javaDispatch.getField(dispId);
                if (flags == COM.DISPATCH_PROPERTYGET) {
                    return SwtOleGlue.convertObjectToVariant(classLoader, dispIdOracle, field.getType(),
                            javaDispatch.getFieldValue(dispId));
                } else if ((flags & (COM.DISPATCH_PROPERTYPUT | COM.DISPATCH_PROPERTYPUTREF)) != 0) {
                    javaDispatch.setFieldValue(dispId, JsValueGlue.get(new JsValueIE6(params[0]), classLoader,
                            field.getType(), "Setting field '" + field.getName() + "'"));
                    return new Variant();
                }
            }
        } else {
            // The specified member id is out of range.
            throw new HResultException(COM.DISP_E_MEMBERNOTFOUND);
        }
    } catch (IllegalArgumentException e) {
        // should never, ever happen
        e.printStackTrace();
        throw new HResultException(e);
    }
    System.err.println("IDispatchProxy cannot be invoked with flags: " + Integer.toHexString(flags));
    throw new HResultException(COM.E_NOTSUPPORTED);
}

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

License:Open Source License

@Override
public <T> void setWrappedJavaObject(ClassLoader cl, DispatchIdOracle dispIdOracle, T val) {
    IDispatchImpl dispObj;/*w ww .j  a  va  2  s .  co  m*/
    if (val == null) {
        setNull();
        return;
    } else if (val instanceof IDispatchImpl) {
        dispObj = (IDispatchImpl) val;
    } else {
        dispObj = (IDispatchImpl) WrappersCache.getWrapperForObject(cl, val);
        if (dispObj == null || dispObj.refCount < 1) {
            dispObj = new IDispatchProxy(cl, dispIdOracle, val);
            WrappersCache.putWrapperForObject(cl, val, dispObj);
        }
    }
    IDispatch disp = new IDispatch(dispObj.getAddress());
    disp.AddRef();
    setVariant(new Variant(disp));
}

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

License:Open Source License

@Override
protected Variant invoke(int id, int flags, Variant[] params)
        throws HResultException, InstantiationException, InvocationTargetException {
    ClassLoader classLoader = classLoaderRef.get();
    if (classLoader == null) {
        throw new RuntimeException("Invalid class loader.");
    }//from w w w. j  a  v a2s.co  m
    DispatchIdOracle dispIdOracle = dispIdOracleRef.get();
    if (dispIdOracle == null) {
        throw new RuntimeException("Invalid dispatch oracle.");
    }
    switch (id) {
    case 0:
        // An implicit access.
        if ((flags & COM.DISPATCH_METHOD) != 0) {
            // implicit call -- "m()"
            return callMethod(classLoader, dispIdOracle, null, params, method);
        } else if ((flags & COM.DISPATCH_PROPERTYGET) != 0) {
            // implicit toString -- "'foo' + m"
            return new Variant(toString());
        }
        break;
    case 1:
        // toString
        if ((flags & COM.DISPATCH_METHOD) != 0) {
            // "m.toString()"
            return new Variant(toString());
        } else if ((flags & COM.DISPATCH_PROPERTYGET) != 0) {
            // "m.toString"
            MethodAdaptor toStringMethod;
            try {
                toStringMethod = new MethodAdaptor(Object.class.getDeclaredMethod("toString"));
            } catch (Throwable e) {
                throw new RuntimeException("Failed to get Object.toString() method", e);
            }
            AccessibleObject obj = toStringMethod.getUnderlyingObject();
            IDispatchImpl dispMethod = (IDispatchImpl) WrappersCache.getWrapperForObject(classLoader, obj);
            if (dispMethod == null || dispMethod.refCount < 1) {
                dispMethod = new MethodDispatch(classLoader, dispIdOracle, toStringMethod);
                WrappersCache.putWrapperForObject(classLoader, obj, dispMethod);
            }
            IDispatch disp = new IDispatch(dispMethod.getAddress());
            disp.AddRef();
            return new Variant(disp);
        }
        break;
    case 2:
        // call
        if ((flags & COM.DISPATCH_METHOD) != 0) {
            // "m.call(thisObj, arg)"
            /*
             * First param must be a this object of the correct type (for instance
             * methods). If method is static, it can be null.
             */
            Object jthis = JsValueGlue.get(new JsValueIE6(params[0]), classLoader, method.getDeclaringClass(),
                    "this");
            Variant[] otherParams = new Variant[params.length - 1];
            System.arraycopy(params, 1, otherParams, 0, otherParams.length);
            return callMethod(classLoader, dispIdOracle, jthis, otherParams, method);
        } else if ((flags & COM.DISPATCH_PROPERTYGET) != 0) {
            // "m.call"
            // TODO: not supported
        }
        break;
    case 3:
        // apply
        // TODO: not supported
        break;
    case IDispatchProxy.DISPID_MAGIC_GETGLOBALREF:
        // We are NOT in fact a "wrapped Java Object", but we don't want to
        // throw an exception for being asked.
        return new Variant(0);
    default:
        // The specified member id is out of range.
        throw new HResultException(COM.DISP_E_MEMBERNOTFOUND);
    }
    throw new HResultException(COM.E_NOTSUPPORTED);
}

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  ww  . j av  a2s  . co  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 . j av a2  s  . co  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.JsValueMoz32.java

License:Open Source License

@Override
public <T> void setWrappedJavaObject(ClassLoader cl, DispatchIdOracle dispIdOracle, T val) {
    if (val == null) {
        setNull();//from ww w  .j  a va 2 s.c  o  m
        return;
    }
    DispatchObject32/*64*/ dispObj;
    if (val instanceof DispatchObject32/*64*/) {
        dispObj = (DispatchObject32/*64*/) val;
    } else {
        dispObj = (DispatchObject32/*64*/) WrappersCache.getWrapperForObject(cl, val);
        if (dispObj == null) {
            dispObj = new GeckoDispatchAdapter32/*64*/(cl, dispIdOracle, val);
            WrappersCache.putWrapperForObject(cl, val, dispObj);
        }
    }
    setWrappedJavaObject(dispObj);
}

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

License:Open Source License

@Override
public <T> void setWrappedJavaObject(ClassLoader cl, DispatchIdOracle dispIdOracle, T val) {
    if (val == null) {
        setNull();// w  w w .j ava2  s  .  c  o m
        return;
    }
    DispatchObject64 dispObj;
    if (val instanceof DispatchObject64) {
        dispObj = (DispatchObject64) val;
    } else {
        dispObj = (DispatchObject64) WrappersCache.getWrapperForObject(cl, val);
        if (dispObj == null) {
            dispObj = new GeckoDispatchAdapter64(cl, dispIdOracle, val);
            WrappersCache.putWrapperForObject(cl, val, dispObj);
        }
    }
    setWrappedJavaObject(dispObj);
}

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

License:Open Source License

@Override
public <T> void setWrappedJavaObject(ClassLoader cl, DispatchIdOracle dispIdOracle, T val) {
    DispatchObject<?> dispObj;//  w w  w.j ava2 s .  c om
    if (val == null) {
        setNull();
        return;
    } else if (val instanceof DispatchObject) {
        dispObj = (DispatchObject<?>) val;
    } else {
        dispObj = (DispatchObject<?>) WrappersCache.getWrapperForObject(cl, val);
        if (dispObj == null) {
            dispObj = new WebKitDispatchAdapter<H>(cl, dispIdOracle, val);
            WrappersCache.putWrapperForObject(cl, val, dispObj);
        }
    }
    setJsVal(LowLevelWebKit.<H>wrapDispatchObject(LowLevelWebKit.<H>getCurrentJsContext(), dispObj));
}

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 w w w . j  a v a  2s  . co m
    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);
    }
}