Ejemplo n.º 1
0
  private Object handleDynamicProperty(
      String name, String pname, Scriptable start, boolean retrieveValue, Object value) {
    // dynamic property
    Object o = null;

    Method getMethod = (Method) loadMethod(target.getClass(), "getDynamicValue");
    Method setMethod = (Method) loadMethod(target.getClass(), "setDynamicValue");

    if (DBG) {
      Log.d(LCAT, "Treating as dynamic property: " + name);
    }
    // add getter
    KrollMethod getterKm =
        new KrollMethod(this, target, getMethod, KrollMethodType.KrollMethodDynamic, pname);
    setGetterOrSetter(pname, 0, getterKm, false);
    put(buildMethodName("get", pname), this, getterKm);

    if (name.equals(pname) && retrieveValue) {
      try {
        // get value from native
        Object[] args = new Object[1];
        args[0] = name;

        o = KrollObject.fromNative(getMethod.invoke(target, args), getKrollContext());
      } catch (InvocationTargetException e) {
        Log.e(LCAT, "Error getting property: " + e.getMessage(), e);
        Context.throwAsScriptRuntimeEx(e);
      } catch (IllegalAccessException e) {
        Log.e(LCAT, "Error getting property: " + e.getMessage(), e);
        Context.throwAsScriptRuntimeEx(e);
      }
    }

    // add setter
    KrollMethod setterKm =
        new KrollMethod(this, target, setMethod, KrollMethodType.KrollMethodDynamic, pname);
    setGetterOrSetter(pname, 0, setterKm, true);
    put(buildMethodName("set", pname), this, setterKm);

    // pass value through to native
    if (name.equals(pname) && !retrieveValue) {
      Object[] args = new Object[2];
      args[0] = name;
      args[1] = value;

      try {
        setMethod.invoke(target, argsForMethod(setMethod, args));
      } catch (InvocationTargetException e) {
        Log.e(LCAT, "Error setting property: " + e.getMessage(), e);
        Context.throwAsScriptRuntimeEx(e);
      } catch (IllegalAccessException e) {
        Log.e(LCAT, "Error setting property: " + e.getMessage(), e);
        Context.throwAsScriptRuntimeEx(e);
      }
    }

    if (o == null) {
      if (name.startsWith("get")) {
        o = getterKm;
      } else if (name.startsWith("set")) {
        o = setterKm;
      }
    }

    return o;
  }
Ejemplo n.º 2
0
 public KrollObject(KrollObject parent, Object target) {
   super(parent, null);
   weakKrollContext = new WeakReference<KrollContext>(parent.getKrollContext());
   this.target = target;
   initApplication(parent.getKrollContext());
 }
Ejemplo n.º 3
0
  private Object handleMethodOrProperty(
      String name, Scriptable start, boolean retrieveValue, Object value) {
    Object o = NOT_FOUND;

    String pname = name;

    //		if (name.equals("valueOf") || name.equals("toString")) {
    //			KrollMethod km = new KrollMethod(this, value, null, KrollMethodType.KrollMethodInvoke);
    //			put(name, this, km);
    //			o = value;
    //		}

    if (name.startsWith("get") || name.startsWith("set")) {
      pname = name.substring(3, 4);
      pname = pname.toLowerCase();
      if (name.length() > 4) {
        pname += name.substring(4);
      }
    }

    Method getMethod = (Method) loadMethod(target.getClass(), buildMethodName("get", pname));
    Method setMethod = (Method) loadMethod(target.getClass(), buildMethodName("set", pname));
    boolean isGetter = getMethod != null && getMethod.getParameterTypes().length == 0;
    boolean isSetter = setMethod != null && setMethod.getParameterTypes().length == 1;

    if (isGetter || isSetter) {
      if (DBG) {
        Log.d(LCAT, "Treating as property: " + pname);
      }
      boolean getRetrieved = false;
      if (getMethod != null) {
        // add getter
        KrollMethod km = null;

        Method propertyMethod = (Method) loadMethod(target.getClass(), pname);
        if (propertyMethod != null) {
          km = new KrollMethod(this, target, propertyMethod, KrollMethodType.KrollMethodGetter);
          put(pname, this, km);
          retrieveValue = false;
          o = km;
        } else {
          km = new KrollMethod(this, target, getMethod, KrollMethodType.KrollMethodPropertyGetter);
          setGetterOrSetter(pname, 0, km, false);
        }

        // add get method
        km = new KrollMethod(this, target, getMethod, KrollMethodType.KrollMethodGetter);
        put(buildMethodName("get", pname), this, km);

        if (retrieveValue && pname.equals(name)) {
          try {
            // get value from native
            o = KrollObject.fromNative(getMethod.invoke(target, new Object[0]), getKrollContext());
            getRetrieved = true;
          } catch (InvocationTargetException e) {
            Log.e(LCAT, "Error getting property: " + e.getMessage(), e);
            Context.throwAsScriptRuntimeEx(e);
          } catch (IllegalAccessException e) {
            Log.e(LCAT, "Error getting property: " + e.getMessage(), e);
            Context.throwAsScriptRuntimeEx(e);
          }
        } else {
          o = km;
        }
      }

      if (setMethod != null) {
        // add setter
        KrollMethod km =
            new KrollMethod(this, target, setMethod, KrollMethodType.KrollMethodPropertySetter);
        setGetterOrSetter(pname, 0, km, true);

        // add set method
        km = new KrollMethod(this, target, setMethod, KrollMethodType.KrollMethodSetter);
        put(buildMethodName("set", pname), this, km);
        if (!getRetrieved) {
          o = km;
        }
        // pass value through to native
        if (!retrieveValue) {
          Object[] args = new Object[1];
          args[0] = value;

          try {
            setMethod.invoke(target, argsForMethod(setMethod, args));
          } catch (InvocationTargetException e) {
            Log.e(LCAT, "Error setting property: " + e.getMessage(), e);
            Context.throwAsScriptRuntimeEx(e);
          } catch (IllegalAccessException e) {
            Log.e(LCAT, "Error setting property: " + e.getMessage(), e);
            Context.throwAsScriptRuntimeEx(e);
          }
        }
      }
    } else {
      // See if the method exists
      Method m = (Method) loadMethod(target.getClass(), name);
      if (m != null) {
        o = new KrollMethod(this, target, m, KrollMethodType.KrollMethodInvoke);
        put(name, this, o);
      } else if (name.startsWith("create")) {
        // Check for dynamic proxy.
        m = (Method) loadMethod(target.getClass(), "createProxy");
        if (m != null) {
          o = new KrollMethod(this, target, m, KrollMethodType.KrollMethodFactory, name);
          put(name, this, o);
        }
      } else {
        o = handleDynamicProperty(name, pname, start, retrieveValue, value);
      }
    }

    return o;
  }