예제 #1
0
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    // Don't generate code for unreferenced variables
    if (_refs.isEmpty()) {
      _ignore = true;
    }

    // Make sure that a variable instance is only compiled once
    if (_ignore) return;
    _ignore = true;

    final String name = getEscapedName();

    if (isLocal()) {
      // Compile variable value computation
      translateValue(classGen, methodGen);

      // Add a new local variable and store value
      boolean createLocal = _local == null;
      if (createLocal) {
        mapRegister(methodGen);
      }
      InstructionHandle storeInst = il.append(_type.STORE(_local.getIndex()));

      // If the local is just being created, mark the store as the start
      // of its live range.  Note that it might have been created by
      // initializeVariables already, which would have set the start of
      // the live range already.
      if (createLocal) {
        _local.setStart(storeInst);
      }
    } else {
      String signature = _type.toSignature();

      // Global variables are store in class fields
      if (classGen.containsField(name) == null) {
        classGen.addField(
            new Field(
                ACC_PUBLIC,
                cpg.addUtf8(name),
                cpg.addUtf8(signature),
                null,
                cpg.getConstantPool()));

        // Push a reference to "this" for putfield
        il.append(classGen.loadTranslet());
        // Compile variable value computation
        translateValue(classGen, methodGen);
        // Store the variable in the allocated field
        il.append(new PUTFIELD(cpg.addFieldref(classGen.getClassName(), name, signature)));
      }
    }
  }
예제 #2
0
파일: Predicate.java 프로젝트: srnsw/xena
  /**
   * Create a new "Filter" class implementing <code>CurrentNodeListFilter</code>. Allocate registers
   * for local variables and local parameters passed in the closure to test(). Notice that local
   * variables need to be "unboxed".
   */
  private void compileFilter(ClassGenerator classGen, MethodGenerator methodGen) {
    TestGenerator testGen;
    LocalVariableGen local;
    FilterGenerator filterGen;

    _className = getXSLTC().getHelperClassName();
    filterGen =
        new FilterGenerator(
            _className,
            "java.lang.Object",
            toString(),
            ACC_PUBLIC | ACC_SUPER,
            new String[] {CURRENT_NODE_LIST_FILTER},
            classGen.getStylesheet());

    final ConstantPoolGen cpg = filterGen.getConstantPool();
    final int length = (_closureVars == null) ? 0 : _closureVars.size();

    // Add a new instance variable for each var in closure
    for (int i = 0; i < length; i++) {
      VariableBase var = ((VariableRefBase) _closureVars.get(i)).getVariable();

      filterGen.addField(
          new Field(
              ACC_PUBLIC,
              cpg.addUtf8(var.getEscapedName()),
              cpg.addUtf8(var.getType().toSignature()),
              null,
              cpg.getConstantPool()));
    }

    final InstructionList il = new InstructionList();
    testGen =
        new TestGenerator(
            ACC_PUBLIC | ACC_FINAL,
            org.apache.bcel.generic.Type.BOOLEAN,
            new org.apache.bcel.generic.Type[] {
              org.apache.bcel.generic.Type.INT,
              org.apache.bcel.generic.Type.INT,
              org.apache.bcel.generic.Type.INT,
              org.apache.bcel.generic.Type.INT,
              Util.getJCRefType(TRANSLET_SIG),
              Util.getJCRefType(NODE_ITERATOR_SIG)
            },
            new String[] {"node", "position", "last", "current", "translet", "iterator"},
            "test",
            _className,
            il,
            cpg);

    // Store the dom in a local variable
    local = testGen.addLocalVariable("document", Util.getJCRefType(DOM_INTF_SIG), null, null);
    final String className = classGen.getClassName();
    il.append(filterGen.loadTranslet());
    il.append(new CHECKCAST(cpg.addClass(className)));
    il.append(new GETFIELD(cpg.addFieldref(className, DOM_FIELD, DOM_INTF_SIG)));
    local.setStart(il.append(new ASTORE(local.getIndex())));

    // Store the dom index in the test generator
    testGen.setDomIndex(local.getIndex());

    _exp.translate(filterGen, testGen);
    il.append(IRETURN);

    filterGen.addEmptyConstructor(ACC_PUBLIC);
    filterGen.addMethod(testGen);

    getXSLTC().dumpClass(filterGen.getJavaClass());
  }