Ejemplo n.º 1
0
 protected void addToScope(Scriptable scope) {
   Object value = exportingBundle.lookup(name);
   StringTokenizer tokenizer = new StringTokenizer(name, "."); // $NON-NLS-1$
   while (true) {
     String token = tokenizer.nextToken();
     Object current = scope.get(token, scope);
     if (!tokenizer.hasMoreTokens()) {
       if (current == Scriptable.NOT_FOUND) {
         if (value instanceof NativeObject) {
           Scriptable wrapped = Context.getCurrentContext().newObject(scope);
           wrapped.setPrototype((Scriptable) value);
           value = wrapped;
         }
         scope.put(token, scope, value);
         return;
       }
       throw new IllegalStateException(
           "Resolve error: "
               + name
               + " already exists for "
               + this.toString()); // $NON-NLS-1$//$NON-NLS-2$	
     }
     if (current == Scriptable.NOT_FOUND) {
       current = ScriptableObject.getProperty(scope, token);
       if (current == Scriptable.NOT_FOUND) current = Context.getCurrentContext().newObject(scope);
       else if (current instanceof NativeObject) {
         // we need to wrap this object from the prototype
         Scriptable wrapped = Context.getCurrentContext().newObject(scope);
         wrapped.setPrototype((Scriptable) current);
         current = wrapped;
       } else
         throw new IllegalStateException(
             "Resolve error: "
                 + name
                 + "-"
                 + token
                 + " already exists for "
                 + this.toString()); // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
       scope.put(token, scope, current);
     }
     scope = (Scriptable) current;
   }
 }
Ejemplo n.º 2
0
  /**
   * Return new {@link Scriptable} instance using the default constructor for the class of the
   * underlying Java method. Return null to indicate that the call method should be used to create
   * new objects.
   */
  public Scriptable createObject(Context cx, Scriptable scope) {
    if (member.isCtor() || (parmsLength == VARARGS_CTOR)) {
      return null;
    }
    Scriptable result;
    try {
      result = (Scriptable) member.getDeclaringClass().newInstance();
    } catch (Exception ex) {
      throw Context.throwAsScriptRuntimeEx(ex);
    }

    result.setPrototype(getClassPrototype());
    result.setParentScope(getParentScope());
    return result;
  }
Ejemplo n.º 3
0
  synchronized Object getPkgProperty(String name, Scriptable start, boolean createPkg) {
    Object cached = super.get(name, start);
    if (cached != NOT_FOUND) return cached;
    if (negativeCache != null && negativeCache.contains(name)) {
      // Performance optimization: see bug 421071
      return null;
    }

    String className = (packageName.length() == 0) ? name : packageName + '.' + name;
    Context cx = Context.getContext();
    ClassShutter shutter = cx.getClassShutter();
    Scriptable newValue = null;
    if (shutter == null || shutter.visibleToScripts(className)) {
      Class<?> cl = null;
      if (classLoader != null) {
        cl = Kit.classOrNull(classLoader, className);
      } else {
        cl = Kit.classOrNull(className);
      }
      if (cl != null) {
        WrapFactory wrapFactory = cx.getWrapFactory();
        newValue = wrapFactory.wrapJavaClass(cx, getTopLevelScope(this), cl);
        newValue.setPrototype(getPrototype());
      }
    }
    if (newValue == null) {
      if (createPkg) {
        NativeJavaPackage pkg;
        pkg = new NativeJavaPackage(true, className, classLoader);
        ScriptRuntime.setObjectProtoAndParent(pkg, getParentScope());
        newValue = pkg;
      } else {
        // add to negative cache
        if (negativeCache == null) negativeCache = new HashSet<String>();
        negativeCache.add(name);
      }
    }
    if (newValue != null) {
      // Make it available for fast lookup and sharing of
      // lazily-reflected constructors and static members.
      super.put(name, start, newValue);
    }
    return newValue;
  }