Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.InstructionList


           
        }
       
        // create the method generator with empty instruction list
        String[] names = (String[])s_argNameLists.get(args.length);
        m_instructionList = new InstructionList();
        m_stackState = new StringStack();
        m_instructionBuilder = cf.getInstructionBuilder();
        m_generator = new MethodGen(access | SYNTHETIC_ACCESS_FLAG, ret, args,
            names, name, cf.getName(), m_instructionList, cf.getConstPoolGen());
       
View Full Code Here


    else
      endLocations = new java.util.Vector();


    // obtain the bytecode for the method
    InstructionList iList = null;
    byte[] code = null;
    try {
      code = doGetByteCode(m.getDeclaringClass(),m);
      iList=new InstructionList(code);
    }
    catch (Throwable e) {
      code = doGetByteCodeWithoutJvmdi(m);
      iList= new InstructionList(code);
    }

    // search the bytecode for the method for 'return' bytecodes
    InstructionHandle[] instructions = iList.getInstructionHandles();
    for (int i=0; i < instructions.length; i++) {
      if (instructions[i].getInstruction() instanceof ReturnInstruction)
        endLocations.add(new Integer(instructions[i].getPosition()));
    }
View Full Code Here

                "This method "
                    + mg.getName()
                    + " is a special method and won't be modified.");
            return orig;
        }
        InstructionList il = null;
        InstructionHandle handle = null;
        Instruction inst = null;

        GETSTATIC getStatic = null;

        Method m = null;

        try {
            il = mg.getInstructionList();
            handle = il.getStart();
            if (level_enters == SIMPLEENTER) {

                insertEnter(mg, il, handle, cp);
                insertFork(il, il.getStart(), handle.getPrev(), cp);
            }

            for (; handle != null; handle = handle.getNext()) {
                inst = handle.getInstruction();
                logger.info("next instruction: " + inst);
                if (level_exits == SIMPLEEXIT
                    && inst instanceof ReturnInstruction) {
                    InstructionHandle lastLogHandle = handle;
                    logger.info(
                        "Found a ReturnInstruction of type: "
                            + inst.toString());
                    int stackSize =
                        inst.consumeStack(cp) - inst.produceStack(cp);
                    while (stackSize != 0) {
                        handle = handle.getPrev();
                        inst = handle.getInstruction();
                        logger.info("prev instruction: " + inst);
                        stackSize += inst.consumeStack(cp)
                            - inst.produceStack(cp);
                    }

                    InstructionHandle insertHandle =
                        insertExit(mg, il, handle, cp);
                    if (insertHandle != null) {
                      // We need to redirect branches etc.... twice
                      // so that the if fork doesn't get erroneous modified.
                        il.redirectBranches(handle, insertHandle);
                        il.redirectExceptionHandlers(
                            mg.getExceptionHandlers(),
                            handle,
                            insertHandle);
                        il.redirectLocalVariables(
                            mg.getLocalVariables(),
                            handle,
                            insertHandle);
                        handle =
                            insertFork(il, insertHandle, handle.getPrev(), cp);
                        il.redirectBranches(insertHandle, handle );
                        il.redirectExceptionHandlers(
                            mg.getExceptionHandlers(),
              insertHandle,
                            handle);
                        il.redirectLocalVariables(
                            mg.getLocalVariables(),
                            insertHandle,
              handle);
                    }

                    handle = lastLogHandle;
                }
                if (inst instanceof GETSTATIC) {
                    getStatic = (GETSTATIC) inst;
                    Type type = getStatic.getFieldType(cp);
                    logger.info(
                        "Found a GETSTATIC instruction of type: "
                            + type.toString());

                    if (getStatic
                        .getFieldName(cp)
                        .equals(loggerAttribute.getName())) {
                        logger.info("This is a logger access");
                        transform(mg, il, handle, cp);
                    }
                }
            }
            if (orig.getLineNumberTable() == null) {
                logger.info("Removing line numbers");
                mg.removeLineNumbers();
            }
            if (orig.getLocalVariableTable() == null) {
                logger.info("Removing Local variables table");
                mg.removeLocalVariables();
            }
            m = mg.getMethod();
        } finally {

            il.dispose();
        }
        return m;
    }
View Full Code Here

            }
            if (ln) {
                mg.removeLineNumbers();
            }
            mg.stripAttributes(skipDebug);
            InstructionList il = mg.getInstructionList();
            if (il != null) {
                InstructionHandle ih = il.getStart();
                while (ih != null) {
                    ih = ih.getNext();
                }
                if (compute) {
                    mg.setMaxStack();
View Full Code Here

    {
  Type type = Type.Boolean;
  if (_chosenMethodType != null)
      type = _chosenMethodType.resultType();

  final InstructionList il = methodGen.getInstructionList();
  translate(classGen, methodGen);

  if ((type instanceof BooleanType) || (type instanceof IntType)) {
      _falseList.add(il.append(new IFEQ(null)));
  }
    }
View Full Code Here

     * return value on the JVM's stack.
     */
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  final int n = argumentCount();
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();
  int index;

  // Translate calls to methods in the BasisLibrary
  if (isStandard() || isExtension()) {
      for (int i = 0; i < n; i++) {
    final Expression exp = argument(i);
    exp.translate(classGen, methodGen);
    exp.startResetIterator(classGen, methodGen);
      }

      // append "F" to the function's name
      final String name = _fname.toString().replace('-', '_') + "F";
      String args = Constants.EMPTYSTRING;

      // Special precautions for some method calls
      if (name.equals("sumF")) {
    args = DOM_INTF_SIG;
    il.append(methodGen.loadDOM());
      }
      else if (name.equals("normalize_spaceF")) {
    if (_chosenMethodType.toSignature(args).
        equals("()Ljava/lang/String;")) {
        args = "I"+DOM_INTF_SIG;
        il.append(methodGen.loadContextNode());
        il.append(methodGen.loadDOM());
    }
      }

      // Invoke the method in the basis library
      index = cpg.addMethodref(BASIS_LIBRARY_CLASS, name,
             _chosenMethodType.toSignature(args));
      il.append(new INVOKESTATIC(index));
  }
  // Add call to BasisLibrary.unresolved_externalF() to generate
  // run-time error message for unsupported external functions
  else if (unresolvedExternal) {
      index = cpg.addMethodref(BASIS_LIBRARY_CLASS,
             "unresolved_externalF",
             "(Ljava/lang/String;)V");
      il.append(new PUSH(cpg, _fname.toString()));
      il.append(new INVOKESTATIC(index));
  }
  else if (_isExtConstructor) {
      final String clazz =
    _chosenConstructor.getDeclaringClass().getName();
      Class[] paramTypes = _chosenConstructor.getParameterTypes();
     
      il.append(new NEW(cpg.addClass(_className)));
      il.append(InstructionConstants.DUP);

      for (int i = 0; i < n; i++) {
    final Expression exp = argument(i);
    exp.translate(classGen, methodGen);
    // Convert the argument to its Java type
    exp.startResetIterator(classGen, methodGen);
    exp.getType().translateTo(classGen, methodGen, paramTypes[i]);
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
    buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append("V");

      index = cpg.addMethodref(clazz,
             "<init>",
             buffer.toString());
      il.append(new INVOKESPECIAL(index));

      // Convert the return type back to our internal type
      (Type.Object).translateFrom(classGen, methodGen,
        _chosenConstructor.getDeclaringClass());
     
  }
  // Invoke function calls that are handled in separate classes
  else {
      final String clazz = _chosenMethod.getDeclaringClass().getName();
      Class[] paramTypes = _chosenMethod.getParameterTypes();

      // Push "this" if it is an instance method
      if (_thisArgument != null) {
    _thisArgument.translate(classGen, methodGen);
      }     

      for (int i = 0; i < n; i++) {
    final Expression exp = argument(i);
    exp.translate(classGen, methodGen);
    // Convert the argument to its Java type
    exp.startResetIterator(classGen, methodGen);
    exp.getType().translateTo(classGen, methodGen, paramTypes[i]);
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
    buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append(getSignature(_chosenMethod.getReturnType()));

      index = cpg.addMethodref(clazz,
             _fname.getLocalPart(),
             buffer.toString());
      il.append(_thisArgument != null ? (InvokeInstruction) new INVOKEVIRTUAL(index) :
            (InvokeInstruction) new INVOKESTATIC(index));
      // Convert the return type back to our internal type
      _type.translateFrom(classGen, methodGen,
        _chosenMethod.getReturnType());
View Full Code Here

    //
    static void emitInitializerConstructor(ClassGen stubClass,
            FieldGen handlerField, FieldGen initializerField) {
        String stubClassName = stubClass.getClassName();
        ConstantPoolGen cp = stubClass.getConstantPool();
        InstructionList il = new InstructionList();

        MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, Type.VOID,
                Type.NO_ARGS, null, "<init>", stubClassName, il, cp);

        InstructionFactory fac = new InstructionFactory(stubClass, cp);

        // call super-constructor
        il.append(InstructionFactory.createThis());
        il.append(fac.createInvoke(stubClass.getSuperclassName(), "<init>",
                Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));

        // push "this"
        il.append(InstructionFactory.createThis());

        // get static initializer
        il.append(fac.createGetStatic(stubClassName,
                initializerField.getName(), initializerField.getType()));

        emitInvoke(il, fac, getStubHandlerRef);

        // checkCast
        il.append(fac.createCast(Type.OBJECT, handlerField.getType()));

        // put handlerField
        il.append(new PUTFIELD(cp.addFieldref(stubClassName, handlerField
                .getName(), handlerField.getSignature())));

        // return
        il.append(InstructionConstants.RETURN);

        // compute stack and locals...
        mg.setMaxStack();
        mg.setMaxLocals();
View Full Code Here

    // Constructor for a stub with an initializer
    //
    static void emitOneArgConstructor(ClassGen stubClass, FieldGen handlerField) {
        String stubClassName = stubClass.getClassName();
        ConstantPoolGen cp = stubClass.getConstantPool();
        InstructionList il = new InstructionList();

        Type[] args = new Type[] { handlerField.getType() };

        MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, args,
                null, "<init>", stubClassName, il, cp);

        InstructionFactory fac = new InstructionFactory(stubClass, cp);

        // call super-constructor
        il.append(InstructionFactory.createThis());
        il.append(fac.createInvoke(stubClass.getSuperclassName(), "<init>",
                Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));

        // push this again...
        il.append(InstructionFactory.createThis());

        // push the handler
        il.append(InstructionFactory.createLoad(handlerField.getType(), 1));

        // put handlerField
        il.append(new PUTFIELD(cp.addFieldref(stubClassName, handlerField
                .getName(), handlerField.getSignature())));

        // return
        il.append(InstructionConstants.RETURN);

        // compute stack and locals...
        mg.setMaxStack();
        mg.setMaxLocals();
View Full Code Here

                    + " has wrong # methods");
        }
        String handlerGetName = stub_methods[0].getName();

        ConstantPoolGen cp = clazz.getConstantPool();
        InstructionList il = new InstructionList();
        InstructionFactory fac = new InstructionFactory(clazz, cp);

        Type methodReturnType = translate(Object.class);
        Type[] methodArgTypes = new Type[0];

        MethodGen mg = new MethodGen(
                Constants.ACC_FINAL | Constants.ACC_PUBLIC, methodReturnType,
                methodArgTypes, null, // arg names
                handlerGetName, clazz.getClassName(), il, cp);

        mg.addAttribute(new Synthetic(cp.addUtf8("Synthetic"), 0, null, cp
                .getConstantPool()));

        //
        // construct method body
        //

        il.append(InstructionFactory.createThis());

        il.append(fac.createGetField(clazz.getClassName(), handlerField
                .getName(), handlerField.getType()));

        emitReturn(il, methodReturnType);

        //
View Full Code Here

    }

    static void generate(ClassGen clazz, MethodRef method, FieldGen dataField,
            FieldGen handlerField, MethodRef handlerMethodRef) {
        ConstantPoolGen cp;
        InstructionList il;

        cp = clazz.getConstantPool();
        il = new InstructionList();

        InstructionFactory fac = new InstructionFactory(clazz, cp);

        Type methodReturnType = translate(method.getReturnType());
        Type[] methodArgTypes = translate(method.getParameterTypes());

        MethodGen mg = new MethodGen(
                Constants.ACC_FINAL | Constants.ACC_PUBLIC, methodReturnType,
                methodArgTypes, null, // arg names
                method.getName(), clazz.getClassName(), il, cp);

        mg.addAttribute(new Synthetic(cp.addUtf8("Synthetic"), 0, null, cp
                .getConstantPool()));

        Class[] throwsException = method.getExceptionTypes();
        for (int i = 0; i < throwsException.length; i++) {
            mg.addException(throwsException[i].getName());
        }

        //
        // BODY
        //

        il.append(InstructionFactory.createThis());

        il.append(fac.createGetField(clazz.getClassName(), handlerField
                .getName(), handlerField.getType()));

        // push "this" as invoke's first argument
        il.append(InstructionFactory.createThis());

        // load data value
        if (dataField.isStatic()) {
            il.append(fac.createGetStatic(clazz.getClassName(), dataField
                    .getName(), dataField.getType()));
        } else {
            il.append(InstructionFactory.createThis());
            il.append(fac.createGetField(clazz.getClassName(), dataField
                    .getName(), dataField.getType()));
        }

        il.append(new PUSH(cp, methodArgTypes.length));
        il.append((Instruction) fac.createNewArray(Type.OBJECT, (short) 1));

        int index = 1;
        for (int i = 0; i < methodArgTypes.length; i++) {
            // dup array ref
            il.append(InstructionConstants.DUP);

            // push index
            il.append(new PUSH(cp, i));

            // transform parameter
            il.append(InstructionFactory.createLoad(methodArgTypes[i], index));
            emitCoerceToObject(il, fac, methodArgTypes[i]);

            // and store into array
            il.append(InstructionFactory.createArrayStore(Type.OBJECT));

            index += methodArgTypes[i].getSize();
        }

        //
        // invoke handler's method
        //
        InstructionHandle tryStart = emitInvoke(il, fac, handlerMethodRef);

        // convert to primitive type
        emitCoerceFromObject(il, fac, methodReturnType);

        // and return

        InstructionHandle tryEnd = emitReturn(il, methodReturnType);

        //
        // catch...
        //
        InstructionHandle rethrowLocation = il.append(new ATHROW());

        Class[] exceptions = method.getExceptionTypes();
        boolean handle_throwable_exception = true;
        boolean handle_runtime_exception = true;
        if (exceptions != null) {
            for (int i = 0; i < exceptions.length; i++) {
                Class ex = exceptions[i];

                if (ex == java.lang.Throwable.class)
                    handle_throwable_exception = false;

                if (ex == java.lang.RuntimeException.class
                        || ex == java.lang.Exception.class)
                    handle_runtime_exception = false;

                mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
                        (ObjectType) translate(ex));
            }
        }

        // A RuntimeException should not cause an
        // UndeclaredThrowableException, so we catch and re-throw it
        // that before throwable.
        if (handle_throwable_exception && handle_runtime_exception) {
            mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
                    new ObjectType("java.lang.RuntimeException"));
        }

        // If anything else is thrown, it is wrapped in an
        // UndeclaredThrowable
        if (handle_throwable_exception) {
            InstructionHandle handlerStart = il.append(new ASTORE(1));

            il
                    .append(new NEW(
                            cp
                                    .addClass("java.lang.reflect.UndeclaredThrowableException")));
            il.append(InstructionConstants.DUP);
            il.append(new ALOAD(1));
            il.append(new INVOKESPECIAL(cp.addMethodref(
                    "java.lang.reflect.UndeclaredThrowableException", "<init>",
                    "(Ljava/lang/Throwable;)V")));

            il.append(new ATHROW());

            mg.addExceptionHandler(tryStart, tryEnd, handlerStart,
                    new ObjectType("java.lang.Throwable"));
        }
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.InstructionList

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.