List of usage examples for com.google.gwt.dev.shell.designtime JsValueGlue get
@SuppressWarnings("unchecked") public static <T> T get(JsValue value, ClassLoader cl, Class<T> type, String msgPrefix)
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 w w w . j a va 2 s . c om*/ 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.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."); }/* ww w. j av a 2 s .c o 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.ie.jsni.SwtOleGlue.java
License:Open Source License
/** * Converts an array of variants to their equivalent java objects. *///www . j a v a 2 s . c o m public static Object[] convertVariantsToObjects(ClassLoader cl, Class<?>[] argTypes, Variant[] varArgs, String msgPrefix) { Object[] javaArgs = new Object[Math.min(varArgs.length, argTypes.length)]; for (int i = 0; i < javaArgs.length; i++) { try { Object javaArg = JsValueGlue.get(new JsValueIE6(varArgs[i]), cl, argTypes[i], msgPrefix); javaArgs[i] = javaArg; } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Error converting argument " + (i + 1) + ": " + e.getMessage()); } } return javaArgs; }
From source file:com.google.gdt.eclipse.designer.moz.jsni.GeckoDispatchAdapter32.java
License:Open Source License
public void setField(String member, int /*long*/ jsRootedValue) { ClassLoader classLoader = classLoaderRef.get(); if (classLoader == null) { throw new RuntimeException("Invalid class loader."); }/*from w w w. j ava 2 s . co m*/ JsValue jsValue = new JsValueMoz32/*64*/(jsRootedValue); int dispId = getDispId(member); if (dispId < 0) { // no field by that name // TODO: expandos? throw new RuntimeException("No such field " + member); } if (javaDispatch.isMethod(dispId)) { throw new RuntimeException("Cannot reassign method " + member); } Field field = javaDispatch.getField(dispId); Object val = JsValueGlue.get(jsValue, classLoader, field.getType(), "setField"); javaDispatch.setFieldValue(dispId, val); }
From source file:com.google.gdt.eclipse.designer.moz.jsni.GeckoDispatchAdapter64.java
License:Open Source License
public void setField(String member, long jsRootedValue) { ClassLoader classLoader = classLoaderRef.get(); if (classLoader == null) { throw new RuntimeException("Invalid class loader."); }//from ww w . j a v a2s . c om JsValue jsValue = new JsValueMoz64(jsRootedValue); int dispId = getDispId(member); if (dispId < 0) { // no field by that name // TODO: expandos? throw new RuntimeException("No such field " + member); } if (javaDispatch.isMethod(dispId)) { throw new RuntimeException("Cannot reassign method " + member); } Field field = javaDispatch.getField(dispId); Object val = JsValueGlue.get(jsValue, classLoader, field.getType(), "setField"); javaDispatch.setFieldValue(dispId, val); }
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 www .j av a 2 s. com * * @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 www. ja va2 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.WebKitDispatchAdapter.java
License:Open Source License
public void setField(H jsContext, String name, H value) { ClassLoader classLoader = classLoaderRef.get(); if (classLoader == null) { throw new RuntimeException("Invalid class loader."); }/*from w ww. j a v a 2 s .c o m*/ LowLevelWebKit.pushJsContext(jsContext); try { JsValue jsValue = new JsValueWebKit<H>(value); int dispId = getDispId(name); if (dispId < 0) { // TODO (knorton): We could allow expandos, but should we? throw new RuntimeException("No such field " + name); } if (javaDispatch.isMethod(dispId)) { throw new RuntimeException("Cannot reassign method " + name); } Field field = javaDispatch.getField(dispId); Object val = JsValueGlue.get(jsValue, classLoader, field.getType(), "setField"); javaDispatch.setFieldValue(dispId, val); } finally { LowLevelWebKit.popJsContext(jsContext); } }