Search in sources :

Example 1 with ArgumentReader

use of com.scratchdisk.script.ArgumentReader in project scriptographer by scriptographer.

the class RhinoWrapFactory method coerceType.

public Object coerceType(Class<?> type, Object value, Object unwrapped) {
    // Coerce native objects to maps when needed
    if (value instanceof Function) {
        if (type == Callable.class)
            return new RhinoCallable(engine, (Function) value);
    } else if (value instanceof Scriptable || value instanceof String) {
        // passed value, or can convert to it.
        if (Map.class.isAssignableFrom(type))
            return toMap((Scriptable) value);
        // Try and see if unwrapping NativeObjects through JS unwrap
        // method brings us to the right type.
        boolean isNativeObject = value instanceof NativeObject;
        if (isNativeObject) {
            unwrapped = unwrap(value);
            if (unwrapped != value && type.isInstance(unwrapped))
                return unwrapped;
        }
        ArgumentReader reader = null;
        if (ArgumentReader.canConvert(type) && (reader = getArgumentReader(value)) != null) {
            return ArgumentReader.convert(reader, unwrapped, type, this);
        } else if (isNativeObject) {
            Constructor ctor = getZeroArgumentConstructor(type);
            if (ctor != null) {
                try {
                    Object result = ctor.newInstance();
                    // As a conversion, use setProperties through argument
                    // reader to set all values on the newly
                    // created object.
                    setProperties(result, getArgumentReader(value));
                    return result;
                } catch (Exception e) {
                    throw Context.throwAsScriptRuntimeEx(e);
                }
            }
        }
    } else if (value == Undefined.instance) {
        // Convert undefined to false if destination is boolean
        if (type == Boolean.TYPE)
            return Boolean.FALSE;
    } else if (value instanceof Boolean) {
        // classes.
        if (!((Boolean) value).booleanValue() && !type.isPrimitive())
            return Undefined.instance;
    }
    return null;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) Function(org.mozilla.javascript.Function) Constructor(java.lang.reflect.Constructor) NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ArgumentReader(com.scratchdisk.script.ArgumentReader) StringArgumentReader(com.scratchdisk.script.StringArgumentReader) Scriptable(org.mozilla.javascript.Scriptable) IdentityHashMap(java.util.IdentityHashMap) WeakIdentityHashMap(com.scratchdisk.util.WeakIdentityHashMap) Map(java.util.Map)

Aggregations

ArgumentReader (com.scratchdisk.script.ArgumentReader)1 StringArgumentReader (com.scratchdisk.script.StringArgumentReader)1 WeakIdentityHashMap (com.scratchdisk.util.WeakIdentityHashMap)1 Constructor (java.lang.reflect.Constructor)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 Function (org.mozilla.javascript.Function)1 NativeObject (org.mozilla.javascript.NativeObject)1 Scriptable (org.mozilla.javascript.Scriptable)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1