Esempio n. 1
0
  void put(Scriptable scope, String name, Object javaObject, Object value, boolean isStatic) {
    Map<String, Object> ht = isStatic ? staticMembers : members;
    Object member = ht.get(name);
    if (!isStatic && member == null) {
      // Try to get static member from instance (LC3)
      member = staticMembers.get(name);
    }
    if (member == null) throw reportMemberNotFound(name);
    if (member instanceof FieldAndMethods) {
      FieldAndMethods fam = (FieldAndMethods) ht.get(name);
      member = fam.field;
    }

    // Is this a bean property "set"?
    if (member instanceof BeanProperty) {
      BeanProperty bp = (BeanProperty) member;
      if (bp.setter == null) {
        throw reportMemberNotFound(name);
      }
      // If there's only one setter or if the value is null, use the
      // main setter. Otherwise, let the NativeJavaMethod decide which
      // setter to use:
      if (bp.setters == null || value == null) {
        Class<?> setType = bp.setter.argTypes[0];
        Object[] args = {Context.jsToJava(value, setType)};
        try {
          bp.setter.invoke(javaObject, args);
        } catch (Exception ex) {
          throw Context.throwAsScriptRuntimeEx(ex);
        }
      } else {
        Object[] args = {value};
        bp.setters.call(
            Context.getContext(), ScriptableObject.getTopLevelScope(scope), scope, args);
      }
    } else {
      if (!(member instanceof Field)) {
        String str = (member == null) ? "msg.java.internal.private" : "msg.java.method.assign";
        throw Context.reportRuntimeError1(str, name);
      }
      Field field = (Field) member;
      Object javaValue = Context.jsToJava(value, field.getType());
      try {
        field.set(javaObject, javaValue);
      } catch (IllegalAccessException accessEx) {
        if ((field.getModifiers() & Modifier.FINAL) != 0) {
          // treat Java final the same as JavaScript [[READONLY]]
          return;
        }
        throw Context.throwAsScriptRuntimeEx(accessEx);
      } catch (IllegalArgumentException argEx) {
        throw Context.reportRuntimeError3(
            "msg.java.internal.field.type",
            value.getClass().getName(),
            field,
            javaObject.getClass().getName());
      }
    }
  }
Esempio n. 2
0
  static JavaMembers lookupClass(
      Scriptable scope, Class<?> dynamicType, Class<?> staticType, boolean includeProtected) {
    JavaMembers members;
    scope = ScriptableObject.getTopLevelScope(scope);
    ClassCache cache = ClassCache.get(scope);
    Map<Class<?>, JavaMembers> ct = cache.getClassCacheMap();

    Class<?> cl = dynamicType;
    for (; ; ) {
      members = ct.get(cl);
      if (members != null) {
        return members;
      }
      try {
        members = new JavaMembers(scope, cl, includeProtected);
        break;
      } catch (SecurityException e) {
        // Reflection may fail for objects that are in a restricted
        // access package (e.g. sun.*).  If we get a security
        // exception, try again with the static type if it is interface.
        // Otherwise, try superclass
        if (staticType != null && staticType.isInterface()) {
          cl = staticType;
          staticType = null; // try staticType only once
        } else {
          Class<?> parent = cl.getSuperclass();
          if (parent == null) {
            if (cl.isInterface()) {
              // last resort after failed staticType interface
              parent = ScriptRuntime.ObjectClass;
            } else {
              throw e;
            }
          }
          cl = parent;
        }
      }
    }

    if (cache.isCachingEnabled()) ct.put(cl, members);
    return members;
  }
Esempio n. 3
0
 Object get(Scriptable scope, String name, Object javaObject, boolean isStatic) {
   Map<String, Object> ht = isStatic ? staticMembers : members;
   Object member = ht.get(name);
   if (!isStatic && member == null) {
     // Try to get static member from instance (LC3)
     member = staticMembers.get(name);
   }
   if (member == null) {
     member =
         this.getExplicitFunction(
             scope, name,
             javaObject, isStatic);
     if (member == null) return Scriptable.NOT_FOUND;
   }
   if (member instanceof Scriptable) {
     return member;
   }
   Context cx = Context.getContext();
   Object rval;
   Class<?> type;
   try {
     if (member instanceof BeanProperty) {
       BeanProperty bp = (BeanProperty) member;
       if (bp.getter == null) return Scriptable.NOT_FOUND;
       rval = bp.getter.invoke(javaObject, Context.emptyArgs);
       type = bp.getter.method().getReturnType();
     } else {
       Field field = (Field) member;
       rval = field.get(isStatic ? null : javaObject);
       type = field.getType();
     }
   } catch (Exception ex) {
     throw Context.throwAsScriptRuntimeEx(ex);
   }
   // Need to wrap the object before we return it.
   scope = ScriptableObject.getTopLevelScope(scope);
   return cx.getWrapFactory().wrap(cx, scope, rval, type);
 }
Esempio n. 4
0
 public static Scriptable createAdapterWrapper(Scriptable obj, Object adapter) {
   Scriptable scope = ScriptableObject.getTopLevelScope(obj);
   NativeJavaObject res = new NativeJavaObject(scope, adapter, null, true);
   res.setPrototype(obj);
   return res;
 }