Package org.apache.bcel.classfile

Examples of org.apache.bcel.classfile.Code


                result.append(n);
            }

            // Append a code.
            if (printCode) {
                Code code = m.getCode();
                // If the method is not abstract.
                if (code != null) {
                    result.append(indentString(indent * 2, "Code: "));
                    result.append(n);

                    if (verbose) {
                        result.append(indentString(indent * 3, "Max stack="));
                        result.append(code.getMaxStack());
                        result.append(", Max locals=");
                        result.append(code.getMaxLocals());
                        result.append(n);
                    }

                    // Append the code string.
                    result.append(indentString(
                            indent * 3,
                            Utility.codeToString(
                                    code.getCode(), constPool, 0, -1, verbose
                            ))
                    );
                }
            }

View Full Code Here


                        : null, cp);
        Attribute[] attributes = m.getAttributes();
        for (int i = 0; i < attributes.length; i++) {
            Attribute a = attributes[i];
            if (a instanceof Code) {
                Code c = (Code) a;
                setMaxStack(c.getMaxStack());
                setMaxLocals(c.getMaxLocals());
                CodeException[] ces = c.getExceptionTable();
                if (ces != null) {
                    for (int j = 0; j < ces.length; j++) {
                        CodeException ce = ces[j];
                        int type = ce.getCatchType();
                        ObjectType c_type = null;
                        if (type > 0) {
                            String cen = m.getConstantPool().getConstantString(type,
                                    Constants.CONSTANT_Class);
                            c_type = new ObjectType(cen);
                        }
                        int end_pc = ce.getEndPC();
                        int length = m.getCode().getCode().length;
                        InstructionHandle end;
                        if (length == end_pc) { // May happen, because end_pc is exclusive
                            end = il.getEnd();
                        } else {
                            end = il.findHandle(end_pc);
                            end = end.getPrev(); // Make it inclusive
                        }
                        addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce
                                .getHandlerPC()), c_type);
                    }
                }
                Attribute[] c_attributes = c.getAttributes();
                for (int j = 0; j < c_attributes.length; j++) {
                    a = c_attributes[j];
                    if (a instanceof LineNumberTable) {
                        LineNumber[] ln = ((LineNumberTable) a).getLineNumberTable();
                        for (int k = 0; k < ln.length; k++) {
View Full Code Here

        for (int i = 0; i < code_attrs.length; i++) {
            attrs_len += (code_attrs[i].getLength() + 6);
        }
        CodeException[] c_exc = getCodeExceptions();
        int exc_len = c_exc.length * 8; // Every entry takes 8 bytes
        Code code = null;
        if ((il != null) && !isAbstract() && !isNative()) {
            // Remove any stale code attribute
            Attribute[] attributes = getAttributes();
            for (int i = 0; i < attributes.length; i++) {
                Attribute a = attributes[i];
                if (a instanceof Code) {
                    removeAttribute(a);
                }
            }
            code = new Code(cp.addUtf8("Code"), 8 + byte_code.length + // prologue byte code
                    2 + exc_len + // exceptions
                    2 + attrs_len, // attributes
                    max_stack, max_locals, byte_code, c_exc, code_attrs, cp.getConstantPool());
            addAttribute(code);
        }
View Full Code Here

   * @param method
   * @return
   */
  private static List<Instruction> getMethodCode(Method method) {
    final List<Instruction> ret = new ArrayList<Instruction>();
    Code code = method.getCode();
    if (code == null) {
      return ret;
    }

    byte[] bytes = code.getCode();
    String str = Utility.codeToString(bytes, code.getConstantPool(), 0,
        bytes.length);
    String[] lines = StrUtils.parse(str, "\n");
    for (int i = 0; i < lines.length; i++) {
      if (lines[i].trim().length() == 0) {
        continue;
      }
      try {
        ret.add(Instruction.parse(lines[i], code.getConstantPool(),
            code.getLocalVariableTable()));
      } catch (IllegalArgumentException exc) {
        // Catch this and continue.
      }
    }
    return ret;
View Full Code Here

    }
    return ret;
  }

  private LineNumberTable getMethodLineNumberTable(Method method) {
    Code code = method.getCode();
    if (code == null) {
      return null;
    }

    return code.getLineNumberTable();
  }
View Full Code Here

   * @param method
   * @return
   */
  private static List<Instruction> getMethodCode(Method method) {
    final List<Instruction> ret = new ArrayList<Instruction>();
    Code code = method.getCode();
    if (code == null) {
      return ret;
    }

    byte[] bytes = code.getCode();
    String str = Utility.codeToString(bytes, code.getConstantPool(), 0,
        bytes.length);
    String[] lines = StrUtils.parse(str, "\n");
    for (int i = 0; i < lines.length; i++) {
      if (lines[i].trim().length() == 0) {
        continue;
      }
      try {
        ret.add(Instruction.parse(lines[i], code.getConstantPool(),
            code.getLocalVariableTable()));
      } catch (IllegalArgumentException exc) {
        // Catch this and continue.
      }
    }
    return ret;
View Full Code Here

    }
    return ret;
  }

  private LineNumberTable getMethodLineNumberTable(Method method) {
    Code code = method.getCode();
    if (code == null) {
      return null;
    }

    return code.getLineNumberTable();
  }
View Full Code Here

      ClassRecord aClassRec, Binary aBinary, RecordTable<RecordTable<ExceptionRecord>> aExceptionTables,
      HashVector<Signature> aSignatures) throws TinyVMException
   {
      iClassRecord = aClassRec;
      iMethod = aEntry;
      Code pCodeAttrib = iMethod.getCode();
      boolean pNoBody = iMethod.isAbstract() || iMethod.isNative();
      assert pCodeAttrib != null || pNoBody: "Check: body is present";
      assert pCodeAttrib == null || !pNoBody: "Check: no body is present";
      aSignatures.addElement(aSignature);
      iSignatureId = aSignatures.indexOf(aSignature);
      if (iSignatureId >= TinyVMConstants.MAX_SIGNATURES)
      {
         throw new TinyVMException(
            "The total number of unique signatures exceeds "
               + TinyVMConstants.MAX_SIGNATURES);
      }
      iNumLocals = pCodeAttrib == null? 0 : pCodeAttrib.getMaxLocals();
      if (iNumLocals > TinyVMConstants.MAX_LOCALS)
      {
         throw new TinyVMException("Method " + aClassRec.getName() + "."
            + iMethod.getName() + " has " + iNumLocals + " local words. Only "
            + TinyVMConstants.MAX_LOCALS + " are allowed.");
      }
      iNumOperands = pCodeAttrib == null? 0 : pCodeAttrib.getMaxStack();
      if (iNumOperands > TinyVMConstants.MAX_OPERANDS)
      {
         throw new TinyVMException("Method " + aClassRec.getName() + "."
            + iMethod.getName() + " has an operand stack "
            + " whose potential size is " + iNumOperands + ". " + "Only "
            + TinyVMConstants.MAX_OPERANDS + " are allowed.");
      }
      iNumParameters = getNumParamWords(iMethod);
      if (iNumParameters > TinyVMConstants.MAX_PARAMETER_WORDS)
      {
         throw new TinyVMException("Method " + aClassRec.getName() + "."
            + iMethod.getName() + " has " + iNumParameters
            + " parameter words. Only " + TinyVMConstants.MAX_PARAMETER_WORDS
            + " are allowed.");
      }
      if (iMethod.isNative() && !aBinary.isSpecialSignature(aSignature))
      {
         throw new TinyVMException("Method " + aClassRec.getName() + "."
           + iMethod.getName() + " is an unknown native method."
           + " You are probably using JDK APIs"
            + " or libraries that cannot be run under leJOS.");
      }

      if (pCodeAttrib != null)
      {
         iExceptionTable = new RecordTable<ExceptionRecord>("exceptions", true, false);
         CodeException[] pExcepTable = pCodeAttrib.getExceptionTable();
         iNumExceptionHandlers = pExcepTable.length;
         if (iNumExceptionHandlers > TinyVMConstants.MAX_EXCEPTION_HANDLERS)
         {
            throw new TinyVMException("Method " + aClassRec.getName() + "."
               + iMethod.getName() + " has " + iNumExceptionHandlers
View Full Code Here

   }

   public void copyCode (RecordTable<CodeSequence> aCodeSequences, JavaClass aClassFile,
      Binary aBinary)
   {
      Code pCodeAttrib = iMethod.getCode();
      if (pCodeAttrib != null)
      {
         iCodeSequence = new CodeSequence();
         copyCode(pCodeAttrib.getCode(), aClassFile, aBinary);
         aCodeSequences.add(iCodeSequence);
      }
   }
View Full Code Here

   }

   public void postProcessCode (RecordTable<CodeSequence> aCodeSequences,
      JavaClass aClassFile, Binary aBinary) throws TinyVMException
   {
      Code pCodeAttrib = iMethod.getCode();
      if (pCodeAttrib != null)
      {
         postProcessCode(pCodeAttrib.getCode(), aClassFile, aBinary);
         iCodeStart = (iCodeSequence == null? 0 : iCodeSequence.getOffset());

      }
   }
View Full Code Here

TOP

Related Classes of org.apache.bcel.classfile.Code

Copyright © 2018 www.massapicom. 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.