Java 类org.apache.bcel.generic.BranchHandle 实例源码

项目:VestaClient    文件:BCELFactory.java   
private void updateBranchTargets() {
    for (Iterator i = branches.iterator(); i.hasNext();) {
        BranchInstruction bi = (BranchInstruction) i.next();
        BranchHandle bh = (BranchHandle) branch_map.get(bi);
        int pos = bh.getPosition();
        String name = bi.getName() + "_" + pos;
        int t_pos = bh.getTarget().getPosition();
        _out.println("    " + name + ".setTarget(ih_" + t_pos + ");");
        if (bi instanceof Select) {
            InstructionHandle[] ihs = ((Select) bi).getTargets();
            for (int j = 0; j < ihs.length; j++) {
                t_pos = ihs[j].getPosition();
                _out.println("    " + name + ".setTarget(" + j + ", ih_" + t_pos + ");");
            }
        }
    }
}
项目:cashmere    文件:Cashmerec.java   
InstructionHandle insertTypecheckCode(MethodGen m, InstructionList il,
    InstructionHandle pos, int spawnId, int exceptionPos) {
    ArrayList<CodeExceptionGen> catches = mtab.getCatchTypes(m, spawnId);

    InstructionHandle[] jumpTargets = new InstructionHandle[catches.size() + 1];

    BranchHandle[] jumps = new BranchHandle[catches.size()];

    for (int i = 0; i < catches.size(); i++) {
        CodeExceptionGen e = catches.get(i);
        ObjectType type = e.getCatchType();
        InstructionHandle catchTarget = e.getHandlerPC();

        jumpTargets[i] = il.insert(pos, new ALOAD(exceptionPos));
        il.insert(pos, new INSTANCEOF(cpg.addClass(type)));
        il.insert(pos, new BIPUSH((byte) 1));
        jumps[i] = il.insert(pos, new IF_ICMPNE(null));
        il.insert(pos, new ALOAD(exceptionPos));
        il.insert(pos, ins_f.createCheckCast(type));
        il.insert(pos, new GOTO(catchTarget));
    }

    InstructionHandle t = il.insert(pos, new ALOAD(exceptionPos));
    il.insert(pos, new ATHROW());

    jumpTargets[catches.size()] = t;

    for (int i = 0; i < catches.size(); i++) {
        jumps[i].setTarget(jumpTargets[i + 1]);
    }

    return pos;
}
项目:root4j    文件:Clones2Builder.java   
private void generateMethods(RootClass k, ConstantPoolGen cp, InstructionList il, InstructionFactory factory, ClassGen cg, String className)
{
   RootMember[] members = k.getMembers();
   for (int i = 0; i < members.length; i++)
   {
      BasicMember member = (BasicMember) members[i];
      TLeaf leaf = (TLeaf) lMap.get(member);
      if (leaf == null) continue;
      String leafClassName = leaf.getClass().getName();

      Type type = member.getJavaType();
      Type arrayType = new ArrayType(type, 1);
      MethodGen mg = new MethodGen(ACC_PUBLIC, type, new Type[] { Type.INT }, new String[]
         {
            "index"
         }, nameMangler.mangleMember(member.getName()), className, il, cp);
      il.append(InstructionConstants.ALOAD_0);
      il.append(factory.createGetField(className, member.getName(), arrayType));
      il.append(InstructionFactory.DUP);

      BranchHandle bh = il.append(new IFNONNULL(null));
      il.append(InstructionFactory.POP);
      il.append(factory.createGetStatic(className, member.getName() + "Leaf", new ObjectType(leafClassName)));
      il.append(InstructionConstants.ALOAD_0);
      il.append(factory.createGetField("org.dianahep.root4j.core.Clones2", "hollowIndex", Type.LONG));

      //    BasicRootClass varClass = (BasicRootClass) member.getType();
      il.append(factory.createInvoke(leafClassName, "setPosition", new ObjectType("org.dianahep.root4j.core.RootInput"), new Type[]
            {
               Type.LONG
            }, INVOKEVIRTUAL));
      il.append(InstructionConstants.ALOAD_0);
      il.append(factory.createGetField("org.dianahep.root4j.core.Clones2", "size", Type.INT));
      il.append((Instruction) factory.createNewArray(type, (short) 1));
      il.append(InstructionConstants.DUP_X1);
      il.append(factory.createInvoke("org.dianahep.root4j.core.RootInput", "readFixedArray", Type.VOID, new Type[]
            {
               arrayType
            }, INVOKEINTERFACE));

      il.append(InstructionConstants.DUP);
      il.append(InstructionConstants.ALOAD_0);
      il.append(InstructionConstants.SWAP);
      il.append(factory.createPutField(className, member.getName(), arrayType));

      bh.setTarget(il.append(InstructionConstants.ILOAD_1));
      il.append(InstructionFactory.createArrayLoad(type));
      il.append(InstructionFactory.createReturn(type));
      mg.setMaxStack();
      mg.setMaxLocals();
      cg.addMethod(mg.getMethod());
      il.dispose();
   }
}
项目:cashmere    文件:Cashmerec.java   
void generateMain(ClassGen clg, Method origMain) {

        InstructionList il = new InstructionList();

        MethodGen new_main = new MethodGen(Constants.ACC_STATIC
            | Constants.ACC_PUBLIC, Type.VOID, new Type[] { new ArrayType(
            Type.STRING, 1) }, new String[] { "argv" }, "main", clg
            .getClassName(), il, clg.getConstantPool());

        il.append(ins_f.createNew(cashmereType));
        il.append(new DUP());
        il.append(ins_f.createInvoke("ibis.cashmere.impl.Cashmere", "<init>",
            Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
        il.append(ins_f.createInvoke("ibis.cashmere.impl.Cashmere", "isMaster",
            Type.BOOLEAN, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
        BranchHandle ifcmp = il.append(new IFEQ(null));

        InstructionHandle origMain_handle = il.append(new ALOAD(0));

        InstructionHandle try_start = il.append(ins_f.createInvoke(clg
            .getClassName(), origMain.getName(), Type.VOID,
            new Type[] { new ArrayType(Type.STRING, 1) },
            Constants.INVOKESTATIC));

        BranchHandle try_end = il.append(new GOTO(null));

        InstructionHandle e_handler = il.append(getCashmere(ins_f));
        il.append(new SWAP());
        il.append(ins_f.createInvoke("ibis.cashmere.impl.Cashmere", "exit",
                Type.VOID, new Type[] { new ObjectType("java.lang.Throwable")},
                Constants.INVOKEVIRTUAL));

        BranchHandle gto2 = il.append(new GOTO(null));

        InstructionHandle ifeq_target = il.append(getCashmere(ins_f));
        ifcmp.setTarget(ifeq_target);
        il.append(ins_f.createInvoke("ibis.cashmere.impl.Cashmere", "client",
            Type.VOID, Type.NO_ARGS, Constants.INVOKEVIRTUAL));

        il.append(getCashmere(ins_f));
        il.append(ins_f.createInvoke("ibis.cashmere.impl.Cashmere", "isMaster",
            Type.BOOLEAN, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
        il.append(new IFNE(origMain_handle));

        InstructionHandle gto_target = il.append(getCashmere(ins_f));
        try_end.setTarget(gto_target);

        il.append(ins_f.createInvoke("ibis.cashmere.impl.Cashmere", "exit",
            Type.VOID, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
        InstructionHandle gto2_target = il.append(new RETURN());
        gto2.setTarget(gto2_target);

        new_main.addExceptionHandler(try_start, try_end, e_handler,
            new ObjectType("java.lang.Throwable"));
        new_main.setMaxStack();
        new_main.setMaxLocals();

        new_main.addLocalVariable("argv", new ArrayType(Type.STRING, 1), 0,
            origMain_handle, null);

        removeLocalTypeTables(new_main);

        Method main = new_main.getMethod();
        gen_c.addMethod(main);
    }
项目:VestaClient    文件:BCELFactory.java   
public void visitBranchInstruction( BranchInstruction bi ) {
    BranchHandle bh = (BranchHandle) branch_map.get(bi);
    int pos = bh.getPosition();
    String name = bi.getName() + "_" + pos;
    if (bi instanceof Select) {
        Select s = (Select) bi;
        branches.add(bi);
        StringBuffer args = new StringBuffer("new int[] { ");
        int[] matchs = s.getMatchs();
        for (int i = 0; i < matchs.length; i++) {
            args.append(matchs[i]);
            if (i < matchs.length - 1) {
                args.append(", ");
            }
        }
        args.append(" }");
        _out.print("Select " + name + " = new " + bi.getName().toUpperCase(Locale.ENGLISH)
                + "(" + args + ", new InstructionHandle[] { ");
        for (int i = 0; i < matchs.length; i++) {
            _out.print("null");
            if (i < matchs.length - 1) {
                _out.print(", ");
            }
        }
        _out.println(" }, null);");
    } else {
        int t_pos = bh.getTarget().getPosition();
        String target;
        if (pos > t_pos) {
            target = "ih_" + t_pos;
        } else {
            branches.add(bi);
            target = "null";
        }
        _out.println("    BranchInstruction " + name + " = _factory.createBranchInstruction("
                + "Constants." + bi.getName().toUpperCase(Locale.ENGLISH) + ", " + target
                + ");");
    }
    if (bh.hasTargeters()) {
        _out.println("    ih_" + pos + " = il.append(" + name + ");");
    } else {
        _out.println("    il.append(" + name + ");");
    }
}
项目:eclectic    文件:ApiModelStrategy.java   
@Override
public void genSet(Set s, GenScope scope) {
    String javaClassName = description.getJavaClass(className);
    SimpleSet    setter  = (SimpleSet) description.getSetter(className, s.getFeatureName());
    assert(setter != null); // constructor setter should be rewritten in a pre-processing step
    // ObjectType paramType = new ObjectType(setter.getSetter().getParameterTypes().get(0));
    Type      returnType = Type.VOID;

    if ( setter.getSetter().getReturnType() != null ) {
        returnType = computeParamType(setter.getSetter().getReturnType()); // new ObjectType(setter.getSetter().getReturnType());
    }

    InstructionList il       = scope.getInstructionList();
    InstructionFactory ifact = scope.getInstructionFactory();

    Variable realReceptor = scope.getRealVariable(s.getReceptor());
    Variable realValue    = scope.getRealVariable(s.getValue());

    BranchInstruction branchToEnd = InstructionFactory
            .createBranchInstruction(Constants.GOTO, null);

    BranchInstruction branchToMonovalued = InstructionFactory
            .createBranchInstruction(Constants.IFEQ, null);

    // push value
    scope.loadVariable(realValue, il);
    il.append(InstructionConstants.DUP);

    // check if it is multivalued result
    il.append(ifact.createInstanceOf(DefaultTypes.Iterable));

    // If it is not an iterable goto -> monovalued set
    il.append(branchToMonovalued);

    // It is an iterable -> multivalued set
       // the collection is in queue, get the iterator 
       il.append(ifact.createCheckCast(DefaultTypes.Iterable));
       il.append(ifact.createInvoke("java.lang.Iterable", "iterator", new ObjectType("java.util.Iterator"), Type.NO_ARGS, Constants.INVOKEINTERFACE));

       // got to check next
       BranchHandle gotoHasNext = il.append(InstructionFactory.createBranchInstruction(Constants.GOTO, null));

       // next object
       InstructionHandle nextElement = il.append(InstructionConstants.DUP);
       il.append(ifact.createInvoke("java.util.Iterator", "next", Type.OBJECT, Type.NO_ARGS, Constants.INVOKEINTERFACE));

       genMonovaluedSet(scope, javaClassName, setter.getSetter(), il, ifact, realReceptor, returnType);

       // check if hasNext, but before dup the iterator
       gotoHasNext.setTarget( il.append(InstructionConstants.DUP) ); 
       il.append(ifact.createInvoke("java.util.Iterator", "hasNext", Type.BOOLEAN, Type.NO_ARGS, Constants.INVOKEINTERFACE));
       il.append(InstructionFactory.createBranchInstruction(Constants.IFNE, nextElement));

       // Pop the iterator, that it is no longer needed
       il.append(InstructionConstants.POP);
    // end of iteration
    // Skip monovalued set

    il.append(branchToEnd);

    // target of monovalued
    branchToMonovalued.setTarget(il.append(InstructionConstants.NOP));      
    genMonovaluedSet(scope, javaClassName, setter.getSetter(), il,
            ifact, realReceptor, returnType);

    branchToEnd.setTarget(il.append(InstructionConstants.NOP));     
}