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;
}
Aggregations