private static double toDouble(Object value) {
   if (value instanceof Number) {
     return ((Number) value).doubleValue();
   } else if (value instanceof String) {
     return ScriptRuntime.toNumber((String) value);
   } else if (value instanceof Scriptable) {
     if (value instanceof Wrapper) {
       // XXX: optimize tail-recursion?
       return toDouble(((Wrapper) value).unwrap());
     } else {
       return ScriptRuntime.toNumber(value);
     }
   } else {
     Method meth;
     try {
       meth = value.getClass().getMethod("doubleValue", (Class[]) null);
     } catch (NoSuchMethodException e) {
       meth = null;
     } catch (SecurityException e) {
       meth = null;
     }
     if (meth != null) {
       try {
         return ((Number) meth.invoke(value, (Object[]) null)).doubleValue();
       } catch (IllegalAccessException e) {
         // XXX: ignore, or error message?
         reportConversionError(value, Double.TYPE);
       } catch (InvocationTargetException e) {
         // XXX: ignore, or error message?
         reportConversionError(value, Double.TYPE);
       }
     }
     return ScriptRuntime.toNumber(value.toString());
   }
 }
Ejemplo n.º 2
0
 public static Object convertArg(Context cx, Scriptable scope, Object arg, int typeTag) {
   switch (typeTag) {
     case JAVA_STRING_TYPE:
       if (arg instanceof String) {
         return arg;
       }
       return ScriptRuntime.toString(arg);
     case JAVA_INT_TYPE:
       if (arg instanceof Integer) {
         return arg;
       }
       return new Integer(ScriptRuntime.toInt32(arg));
     case JAVA_BOOLEAN_TYPE:
       if (arg instanceof Boolean) {
         return arg;
       }
       return ScriptRuntime.toBoolean(arg) ? Boolean.TRUE : Boolean.FALSE;
     case JAVA_DOUBLE_TYPE:
       if (arg instanceof Double) {
         return arg;
       }
       return new Double(ScriptRuntime.toNumber(arg));
     case JAVA_SCRIPTABLE_TYPE:
       if (arg instanceof Scriptable) {
         return arg;
       }
       return ScriptRuntime.toObject(cx, scope, arg);
     case JAVA_OBJECT_TYPE:
       return arg;
     default:
       throw new IllegalArgumentException();
   }
 }
Ejemplo n.º 3
0
 /**
  * Take an object from the scripting layer and convert it to an int.
  *
  * @param obj
  * @return int
  */
 private int toInt(Object obj) {
   int result = 0;
   if (obj instanceof String) {
     result = Integer.parseInt((String) obj);
   } else if (obj instanceof Number) {
     result = ((Number) obj).intValue();
   } else if (obj instanceof Scriptable) {
     Scriptable sobj = (Scriptable) obj;
     if (sobj.getClassName().equals("Number")) {
       result = (int) ScriptRuntime.toNumber(sobj);
     }
   }
   return result;
 }