Exemplo n.º 1
0
  public void forEachObject(Object[] args) {
    boolean subtypes = true;
    Klass kls = null;
    Callable func = null;
    switch (args.length) {
      case 3:
        {
          Object b = args[2];
          if (b != null && b instanceof Boolean) {
            subtypes = ((Boolean) b).booleanValue();
          }
        }
      case 2:
        {
          Object k = args[1];
          if (k == null) return;
          if (k instanceof JSJavaKlass) {
            kls = ((JSJavaKlass) k).getKlass();
          } else if (k instanceof String) {
            kls = SystemDictionaryHelper.findInstanceKlass((String) k);
            if (kls == null) return;
          }
        }
      case 1:
        {
          Object f = args[0];
          if (f != null && f instanceof Callable) {
            func = (Callable) f;
          } else {
            // unknown target - just return
            return;
          }
        }
        break;

      default:
        return;
    }

    final Callable finalFunc = func;
    HeapVisitor visitor =
        new DefaultHeapVisitor() {
          public boolean doObj(Oop oop) {
            JSJavaObject jo = factory.newJSJavaObject(oop);
            if (jo != null) {
              try {
                finalFunc.call(new Object[] {jo});
              } catch (ScriptException exp) {
                throw new RuntimeException(exp);
              }
            }
            return false;
          }
        };
    ObjectHeap heap = VM.getVM().getObjectHeap();
    if (kls == null) {
      kls = SystemDictionaryHelper.findInstanceKlass("java.lang.Object");
    }
    heap.iterateObjectsOfKlass(visitor, kls, subtypes);
  }
Exemplo n.º 2
0
  private void executeQuery(SOQLQuery q, ObjectVisitor visitor) throws SOQLException {
    InstanceKlass kls = null;
    if (q.className != null) {
      kls = SystemDictionaryHelper.findInstanceKlass(q.className);
      if (kls == null) {
        throw new SOQLException(q.className + " is not found!");
      }
    }

    StringBuffer buf = new StringBuffer();
    buf.append("function result(");
    if (q.identifier != null) {
      buf.append(q.identifier);
    }
    buf.append(") { return ");
    buf.append(q.selectExpr.replace('\n', ' '));
    buf.append("; }");

    String selectCode = buf.toString();
    debugPrint(selectCode);
    String whereCode = null;
    if (q.whereExpr != null) {
      buf = new StringBuffer();
      buf.append("function filter(");
      buf.append(q.identifier);
      buf.append(") { return ");
      buf.append(q.whereExpr.replace('\n', ' '));
      buf.append("; }");
      whereCode = buf.toString();
      debugPrint(whereCode);
    } else {
      whereCode = "filter = null;";
    }

    beginQuery();
    // compile select expression and where condition
    evalString(selectCode, "", 1);
    evalString(whereCode, "", 1);

    // iterate thru heap, if needed
    if (q.className != null) {
      try {
        iterateOops(kls, visitor, q.isInstanceOf);
      } finally {
        endQuery();
      }
    } else {
      // simple "select <expr>" query
      try {
        Object select = call("result", new Object[] {});
        visitor.visit(select);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }