Examples of BranchInstruction


Examples of org.apache.bcel.generic.BranchInstruction

            int pos;
            while (sequence.available() > 0 && ((pos = sequence.getIndex()) < end)) {
                Instruction ins = Instruction.readInstruction(sequence);
                if ((ins instanceof BranchInstruction) && !(ins instanceof TABLESWITCH) && !(ins instanceof LOOKUPSWITCH)) {
                    BranchInstruction bi = (BranchInstruction) ins;
                    int offset = bi.getIndex();
                    int target = offset + pos;
                    if (target >= end) { // or target < start ??
                        byte hiByte = (byte) ((target >> 8) & 0x000000FF);
                        byte loByte = (byte) (target & 0x000000FF);
                        bytes[pos + bi.getLength() - 2 - start] = hiByte;
                        bytes[pos + bi.getLength() - 1 - start] = loByte;
                    }
                }
            }
        } catch (IOException ioe) {
        }
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

    }


    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++) {
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

     */
    map = new Hashtable();

    for(int i=0; i < ihs.length; i++) {
      if(ihs[i] instanceof BranchHandle) {
  BranchInstruction bi = (BranchInstruction)ihs[i].getInstruction();
 
  if(bi instanceof Select) { // Special cases LOOKUPSWITCH and TABLESWITCH
    InstructionHandle[] targets = ((Select)bi).getTargets();
   
    for(int j=0; j < targets.length; j++) {
        put(targets[j], "Label" + label_counter++ + ":");
    }
  }

  InstructionHandle ih = bi.getTarget();
  put(ih, "Label" + label_counter++ + ":");
      }
    }

    LocalVariableGen[] lvs = mg.getLocalVariables();
    for(int i=0; i < lvs.length; i++) {
      InstructionHandle ih = lvs[i].getStart();
      put(ih, "Label" + label_counter++ + ":");
      ih = lvs[i].getEnd();
      put(ih, "Label" + label_counter++ + ":")
    }
   
    CodeExceptionGen[] ehs = mg.getExceptionHandlers();
    for(int i=0; i < ehs.length; i++) {
      CodeExceptionGen  c  = ehs[i];
      InstructionHandle ih = c.getStartPC();

      put(ih, "Label" + label_counter++ + ":")
      ih = c.getEndPC();
      put(ih, "Label" + label_counter++ + ":")
      ih = c.getHandlerPC();
      put(ih, "Label" + label_counter++ + ":")
    }

    LineNumberGen[] lns = mg.getLineNumbers();
    for(int i=0; i < lns.length; i++) {
      InstructionHandle ih = lns[i].getInstruction();
      put(ih, ".line " + lns[i].getSourceLine());
    }
    /* Pass 2: Output code.
     */
    for(int i=0; i < lvs.length; i++) {
      LocalVariableGen l = lvs[i];
      out.println(".var " + l.getIndex() + " is " + l.getName() + " " +
      l.getType().getSignature() +
      " from " + get(l.getStart()) +
      " to " + get(l.getEnd()));
    }

    out.print("\n");
   
    for(int i=0; i < ihs.length; i++) {
      InstructionHandle ih   = ihs[i];
      Instruction       inst = ih.getInstruction();
      String            str  = (String)map.get(ih);
     
      if(str != null) {
        out.println(str);
    }

      if(inst instanceof BranchInstruction) {
  if(inst instanceof Select) { // Special cases LOOKUPSWITCH and TABLESWITCH
    Select              s       = (Select)inst;
    int[]               matchs  = s.getMatchs();
    InstructionHandle[] targets = s.getTargets();
   
    if(s instanceof TABLESWITCH) {
      out.println("\ttableswitch " + matchs[0] + " " +
      matchs[matchs.length - 1]);
     
      for(int j=0; j < targets.length; j++) {
            out.println("\t\t" + get(targets[j]));
        }

    } else { // LOOKUPSWITCH
      out.println("\tlookupswitch ");

      for(int j=0; j < targets.length; j++) {
            out.println("\t\t" + matchs[j] + " : " + get(targets[j]));
        }
    }

    out.println("\t\tdefault: " + get(s.getTarget())); // Applies for both
  } else {
    BranchInstruction bi  = (BranchInstruction)inst;
    ih  = bi.getTarget();
    str = get(ih);
    out.println("\t" + Constants.OPCODE_NAMES[bi.getOpcode()] + " " + str);
  }
      } else {
        out.println("\t" + inst.toString(cp.getConstantPool()));
    }
    }
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

    String      pat = "IF_ICMP ICONST_1 GOTO ICONST_0 IFEQ Instruction";

    for(Iterator it = f.search(pat, my_constraint); it.hasNext();) {
      InstructionHandle[] match = (InstructionHandle[])it.next();
      // Everything ok, update code
      BranchInstruction ifeq    = (BranchInstruction)(match[4].getInstruction());
      BranchHandle      if_icmp = (BranchHandle)match[0];

      if_icmp.setTarget(ifeq.getTarget());

      try {
  il.delete(match[1], match[4]);
      } catch(TargetLostException e) {
  InstructionHandle[] targets = e.getTargets();
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

  if (hasNodeSetArgs() || hasReferenceArgs()) {
      translate(classGen, methodGen);
      desynthesize(classGen, methodGen);
  }
  else {
      BranchInstruction bi = null;
      final InstructionList il = methodGen.getInstructionList();

      _left.translate(classGen, methodGen);
      _right.translate(classGen, methodGen);
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

      // To close the last branch, I must jump to super.invokeImpl(), so I need its first instruction here
      InstructionHandle invokeSuper = implementation.append(factory.createThis());
      for (int i = 0; i < tests.size(); ++i)
      {
         BranchInstruction branch = (BranchInstruction)tests.get(i);
         branch.setTarget(invokeSuper);
      }
      tests.clear();
      for (int i = 0; i < catches.size(); ++i)
      {
         BranchInstruction branch = (BranchInstruction)catches.get(i);
         branch.setTarget(invokeSuper);
      }
      catches.clear();

      //
      // return super.invokeImpl(metadata, method, params, args);
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

      InstructionHandle startTest = implementation.append(factory.createLoad(Type.STRING, 2));

      // The first time it will be empty, the second time will be the first time's uninitialized jump instructions
      for (int i = 0; i < tests.size(); ++i)
      {
         BranchInstruction branch = (BranchInstruction)tests.get(i);
         // Initialize previous jump instruction to jump to the next branch
         branch.setTarget(startTest);
      }
      tests.clear();

      implementation.append(new PUSH(classGen.getConstantPool(), method.getName()));
      implementation.append(factory.createInvoke(String.class.getName(), "equals", Type.BOOLEAN, new Type[]{Type.OBJECT}, Constants.INVOKEVIRTUAL));
      // IFEQ compares the stack with 0, which means "if the previous comparison is false, ..."
      BranchInstruction test1 = factory.createBranchInstruction(Constants.IFEQ, null);
      tests.add(test1);
      implementation.append(test1);

      implementation.append(factory.createLoad(new ArrayType(Type.OBJECT, 1), 4));
      implementation.append(new ARRAYLENGTH());
      implementation.append(new PUSH(classGen.getConstantPool(), method.getParameterTypes().length));
      // Here I should test if args.length == <num>, if not equal then go to the next branch
      // Create branch instructions with no offset, since it cannot be handled now, see above
      BranchInstruction test2 = factory.createBranchInstruction(Constants.IF_ICMPNE, null);
      tests.add(test2);
      implementation.append(test2);

      // Here I am on the right method, unless someone created 2 methods with same names and same number of
      // parameters but of different type. In this last case if we're lucky it's the right method and we go
      // via direct call, otherwise we will have a class cast exception and go via reflection

      // Cast and invoke
      // Put the metadata on the stack, to access its 'mbean' field, that will be put on the stack
      // It's also the start of the try block
      InstructionHandle tryStart = implementation.append(factory.createLoad(new ObjectType(MBeanMetaData.class.getName()), 1));
      implementation.append(factory.createInvoke(MBeanMetaData.class.getName(), "getMBean", Type.OBJECT, new Type[0], Constants.INVOKEVIRTUAL));
      // Cast the 'mbean' field to the proper type, the stack will contain the casted mbean
      implementation.append(factory.createCheckCast(new ObjectType(management)));

      // Now add all the arguments to the stack
      Class[] signature = method.getParameterTypes();
      Type[] invokeSignature = new Type[signature.length];
      for (int i = 0; i < signature.length; ++i)
      {
         Class param = signature[i];

         // Load all args on the stack
         implementation.append(factory.createLoad(new ArrayType(Type.OBJECT, 1), 4));
         // I want index 'i'
         implementation.append(new PUSH(classGen.getConstantPool(), i));
         // Now on the stack there is args[i]
         implementation.append(factory.createArrayLoad(Type.OBJECT));

         // Save the signature for the invocation
         invokeSignature[i] = convertClassToType(param);

         if (param.isPrimitive())
         {
            // On the stack I have the wrapper object, I have to convert them to primitive
            replaceObjectWithPrimitive(param, implementation, factory);
         }
         else if (param.isArray())
         {
            // Cast args[i] to the proper class
            implementation.append(factory.createCheckCast((ReferenceType)invokeSignature[i]));
         }
         else
         {
            // Cast args[i] to the proper class
            implementation.append(factory.createCheckCast((ReferenceType)invokeSignature[i]));
         }
      }

      Class returnClass = method.getReturnType();
      Type returnType = convertClassToType(returnClass);

      // On the stack we now have the casted mbean and all the casted arguments, invoke
      implementation.append(factory.createInvoke(management, method.getName(), returnType, invokeSignature, Constants.INVOKEINTERFACE));

      if (returnClass == Void.TYPE)
      {
         implementation.append(InstructionConstants.ACONST_NULL);
      }
      else if (returnClass.isArray())
      {
         // Thanks to the fact that we can assign any array to Object, we do nothing here
      }
      else if (returnClass.isPrimitive())
      {
         replacePrimitiveWithObject(returnClass, methodGen, implementation, factory);
      }

      InstructionHandle tryEnd = implementation.append(factory.createReturn(Type.OBJECT));

      // In case of class cast exception, eat the exception and call super (hence using reflection)
      // catch (ClassCastException x) {/* do nothing */}
      // On the stack there is the exception, we assign it to local variable 'x'
      ObjectType exceptionTypeCCE = new ObjectType("java.lang.ClassCastException");
      LocalVariableGen x = methodGen.addLocalVariable("x", exceptionTypeCCE, null, null);
      InstructionHandle handler = implementation.append(factory.createStore(exceptionTypeCCE, x.getIndex()));
      x.setStart(handler);
      x.setEnd(handler);
      methodGen.addExceptionHandler(tryStart, tryEnd, handler, exceptionTypeCCE);
      // This catch block is followed by another one, and I don't exit with a throw or a return
      BranchInstruction skip = factory.createBranchInstruction(Constants.GOTO, null);
      catches.add(skip);
      implementation.append(skip);

      // An IllegalAccessError is thrown if the MBean interface or a parameter class is not public
      // We eat it and fall back to call super (hence using reflection)
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

               fieldName,
               interfaceType,
               Constants.GETFIELD  ) );
      
       // test for non null of the member variable          
       BranchInstruction bi =
           InstructionFactory.createBranchInstruction(
                   Constants.IFNONNULL, null );
      
       iList.append( bi );
      
       // create the delegate
       iList.append( InstructionFactory.createLoad( Type.OBJECT, 0 ) );
      
       iList.append( factory.createNew( implClass.getName() ) );
       iList.append( InstructionConstants.DUP );
       iList.append(
           factory.createInvoke(
               implClass.getName(),
               Constants.CONSTRUCTOR_NAME,
               Type.VOID, Type.NO_ARGS,
               Constants.INVOKESPECIAL ) );
      
       // use a factory if the implementation class is a factory
       if( MixInImplementationFactory.class.isAssignableFrom( implClass ) ) {

           iList.append(
               factory.createInvoke(
                       implClass.getName(),
                       "getInstance",
                       Type.OBJECT,
                       Type.NO_ARGS, Constants.INVOKEVIRTUAL ) );
          
           iList.append(
               factory.createCheckCast
                   new ObjectType( interfaceClass.getName() ) ) );
          
       }
       // store the created delgate object
       iList.append(
           factory.createFieldAccess(
               cGen.getClassName(),
               fieldName,
               interfaceType,
               Constants.PUTFIELD) );
      
       // load the delegate and return it
       InstructionHandle loadInstruction =
           iList.append( InstructionFactory.createLoad( Type.OBJECT, 0 ) );
      
       bi.setTarget( loadInstruction );
      
       iList.append(
               factory.createFieldAccess(
                   cGen.getClassName(),
                   fieldName,
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

                oidType = BCELClassEnhancer.OT_ObjectIdentity;
                oidKeyType = new ObjectType(Object.class.getName());
            }

            il.append(InstructionConstants.ALOAD_1);
            BranchInstruction checkKeyIsNull = new IFNONNULL(null);
            il.append(checkKeyIsNull);
            createThrowException(ClassEnhancer.CN_IllegalArgumentException, "key is null");
            checkKeyIsNull.setTarget(il.append(InstructionConstants.ALOAD_1));
            il.append(factory.createInstanceOf(Type.STRING));
            il.append(InstructionConstants.ICONST_1);
            BranchInstruction isInstanceof = new IF_ICMPEQ(null);
            il.append(isInstanceof);

            // new oidType(getClass(), (oidKeyType)key);
            il.append(factory.createNew(oidType));
            il.append(InstructionConstants.DUP);
            il.append(InstructionConstants.ALOAD_0);
            il.append(factory.createInvoke("java.lang.Object", "getClass", BCELClassEnhancer.OT_CLASS, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
            il.append(InstructionConstants.ALOAD_1);
            il.append(factory.createCheckCast(oidKeyType));
            il.append(factory.createInvoke(oidType.getClassName(), Constants.CONSTRUCTOR_NAME, Type.VOID,
                new Type[] { BCELClassEnhancer.OT_CLASS, oidKeyType }, Constants.INVOKESPECIAL));

            // "return"
            il.append(InstructionFactory.createReturn(Type.OBJECT));

            // "new oidType(getClass(), (String)key);"
            isInstanceof.setTarget(il.append(factory.createNew(oidType)));
            il.append(InstructionConstants.DUP);
            il.append(InstructionConstants.ALOAD_0);
            il.append(factory.createInvoke("java.lang.Object", "getClass", BCELClassEnhancer.OT_CLASS, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
            il.append(InstructionConstants.ALOAD_1);
            il.append(factory.createCheckCast(Type.STRING));
View Full Code Here

Examples of org.apache.bcel.generic.BranchInstruction

        InstructionHandle defaultHandle;

        // (flag == 0)
        il.append(InstructionConstants.ALOAD_0);
        il.append(factory.createGetField(className, ClassEnhancer.FN_Flag, Type.BYTE));
        BranchInstruction flgIsZero = new IFEQ(null);
        il.append(flgIsZero);

        // (sm == null)
        il.append(InstructionConstants.ALOAD_0);
        il.append(factory.createGetField(className, ClassEnhancer.FN_StateManager, BCELClassEnhancer.OT_StateManager));
        BranchInstruction smIsNull = new IFNULL(null);
        il.append(smIsNull);

        // jdoStateManager
        il.append(InstructionConstants.ALOAD_0);
        il.append(factory.createGetField(className, ClassEnhancer.FN_StateManager, BCELClassEnhancer.OT_StateManager));

        il.append(InstructionConstants.ALOAD_0);
        // the field index: 0, 1, 2...
        il.append(BCELUtils.getBIPUSH(fieldConfig.getFieldId()));
        if (cmd.getPersistenceCapableSuperclass() != null)
        {
            //add to field index the parentFieldCount
            il.append(factory.createGetStatic(className, ClassEnhancer.FN_JdoInheritedFieldCount, Type.INT));
            il.append(InstructionConstants.IADD);
        }
        // objPC.field
        il.append(InstructionConstants.ALOAD_0);
        il.append(factory.createGetField(className, fieldName, nativeType));

        // the value in parameter to set
        il.append(InstructionFactory.createLoad(nativeType, 1));

        // objPC.setXXXField
        il.append(factory.createInvoke(ClassEnhancer.CN_StateManager, "set" + BCELUtils.getJDOMethodName(smType) + "Field",
            Type.VOID, new Type[]{BCELClassEnhancer.OT_PersistenceCapable, Type.INT, smType, smType}, Constants.INVOKEINTERFACE));
        il.append(InstructionConstants.RETURN);

        // he value in parameter to set
        defaultHandle = il.append(InstructionConstants.ALOAD_0);
        il.append(InstructionFactory.createLoad(nativeType, 1));
        // objPC.field =
        il.append(factory.createPutField(className, targetField.getName(), nativeType));

        //----detach------
        if (cmd.isDetachable())
        {
            // jdoIsDetached()
            il.append(InstructionConstants.ALOAD_0);
            il.append(factory.createInvoke(ClassEnhancer.CN_PersistenceCapable, ClassEnhancer.MN_JdoIsDetached,
                Type.BOOLEAN, Type.NO_ARGS, Constants.INVOKEINTERFACE));

            //1
            il.append(InstructionConstants.ICONST_1);
            // if (jdoIsDetached() == 1)
            IF_ICMPEQ ifIsDetached = new IF_ICMPEQ(null);
            il.append(ifIsDetached);
            //return;
            il.append(InstructionConstants.RETURN);

            // jdoDetachedState[3].set(?)
            //   jdoDetachedState[3]
            ifIsDetached.setTarget(il.append(InstructionConstants.ALOAD_0));
            il.append(factory.createGetField(className, ClassEnhancer.FN_JdoDetachedState, BCELClassEnhancer.OT_ObjectArray));
            il.append(InstructionConstants.ICONST_3);
            il.append(InstructionConstants.AALOAD);
            il.append(factory.createCheckCast(BCELClassEnhancer.OT_BitSet)); // Cast to BitSet

            //  the field index: 0, 1, 2...
            il.append(BCELUtils.getBIPUSH(fieldConfig.getFieldId()));
            if (cmd.getPersistenceCapableSuperclass() != null)
            {
                //add to field index the parentFieldCount
                il.append(factory.createGetStatic(className, ClassEnhancer.FN_JdoInheritedFieldCount, Type.INT));
                il.append(InstructionConstants.IADD);
            }
            //  set(?)
            il.append(factory.createInvoke(ClassEnhancer.CN_BitSet, "set", Type.VOID, new Type[]{Type.INT}, Constants.INVOKEVIRTUAL));
        }
        //----detach------

        il.append(InstructionConstants.RETURN);

        flgIsZero.setTarget(defaultHandle);
        smIsNull.setTarget(defaultHandle);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.