Examples of InstructionHandle


Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    InstructionList endHandler = new InstructionList(exceptionVar.createLoad(fact));
    handler.append(munger.getAdviceInstructions(this, exceptionVar, endHandler.getStart()));
    handler.append(endHandler);
    handler.append(InstructionConstants.ATHROW);
    InstructionHandle handlerStart = handler.getStart();

    if (isFallsThrough()) {
      InstructionHandle jumpTarget = handler.append(InstructionConstants.NOP);
      handler.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, jumpTarget));
    }
    InstructionHandle protectedEnd = handler.getStart();
    range.insert(handler, Range.InsideAfter);

    enclosingMethod.addExceptionHandler(range.getStart().getNext(), protectedEnd.getPrev(), handlerStart,
        (ObjectType) BcelWorld.makeBcelType(catchType), // ???Type.THROWABLE,
        // high priority if our args are on the stack
        getKind().hasHighPriorityExceptions());
  }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    // aload_1
    rtExHandler.append(exceptionVar.createLoad(fact));
    // athrow
    rtExHandler.append(InstructionFactory.ATHROW);

    InstructionHandle handlerStart = rtExHandler.getStart();

    if (isFallsThrough()) {
      InstructionHandle jumpTarget = range.getEnd();// handler.append(fact.NOP);
      rtExHandler.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, jumpTarget));
    }

    rtExHandler.append(handler);

    InstructionHandle protectedEnd = rtExHandler.getStart();
    range.insert(rtExHandler, Range.InsideAfter);

    enclosingMethod.addExceptionHandler(range.getStart().getNext(), protectedEnd.getPrev(), handlerStart,
        (ObjectType) BcelWorld.makeBcelType(catchType),
        // high priority if our args are on the stack
        getKind().hasHighPriorityExceptions());
  }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    // inlining support for code style aspects
    if (!munger.getDeclaringType().isAnnotationStyleAspect()) {
      String proceedName = NameMangler.proceedMethodName(munger.getSignature().getName());

      InstructionHandle curr = localAdviceMethod.getBody().getStart();
      InstructionHandle end = localAdviceMethod.getBody().getEnd();
      ConstantPool cpg = localAdviceMethod.getEnclosingClass().getConstantPool();
      while (curr != end) {
        InstructionHandle next = curr.getNext();
        Instruction inst = curr.getInstruction();
        if ((inst.opcode == Constants.INVOKESTATIC) && proceedName.equals(((InvokeInstruction) inst).getMethodName(cpg))) {

          localAdviceMethod.getBody().append(curr,
              getRedoneProceedCall(fact, extractedShadowMethod, munger, localAdviceMethod, proceedVarList));
          Utility.deleteInstruction(curr, localAdviceMethod);
        }
        curr = next;
      }
      // and that's it.
    } else {
      // ATAJ inlining support for @AJ aspects
      // [TODO document @AJ code rule: don't manipulate 2 jps proceed at the same time.. in an advice body]
      InstructionHandle curr = localAdviceMethod.getBody().getStart();
      InstructionHandle end = localAdviceMethod.getBody().getEnd();
      ConstantPool cpg = localAdviceMethod.getEnclosingClass().getConstantPool();
      while (curr != end) {
        InstructionHandle next = curr.getNext();
        Instruction inst = curr.getInstruction();
        if ((inst instanceof INVOKEINTERFACE) && "proceed".equals(((INVOKEINTERFACE) inst).getMethodName(cpg))) {
          final boolean isProceedWithArgs;
          if (((INVOKEINTERFACE) inst).getArgumentTypes(cpg).length == 1) {
            // proceed with args as a boxed Object[]
            isProceedWithArgs = true;
          } else {
            isProceedWithArgs = false;
          }
          InstructionList insteadProceedIl = getRedoneProceedCallForAnnotationStyle(fact, extractedShadowMethod, munger,
              localAdviceMethod, proceedVarList, isProceedWithArgs);
          localAdviceMethod.getBody().append(curr, insteadProceedIl);
          Utility.deleteInstruction(curr, localAdviceMethod);
        }
        curr = next;
      }
    }

    // if (parameterNames.size() == 0) {
    // On return we have inserted the advice body into the local advice method. We have remapped all the local variables
    // that were referenced in the advice as we did the copy, and so the local variable table for localAdviceMethod is
    // now lacking any information about all the initial variables.
    InstructionHandle start = localAdviceMethod.getBody().getStart();
    InstructionHandle end = localAdviceMethod.getBody().getEnd();

    // Find the real start and end
    while (start.getInstruction().opcode == Constants.IMPDEP1) {
      start = start.getNext();
    }
    while (end.getInstruction().opcode == Constants.IMPDEP1) {
      end = end.getPrev();
    }
    Type[] args = localAdviceMethod.getArgumentTypes();
    int argNumber = 0;
    for (int slot = 0; slot < extraParamOffset; argNumber++) { // slot will increase by the argument size each time
      String argumentName = null;
      if (argNumber >= args.length || parameterNames.size() == 0 || argNumber >= parameterNames.size()) {
        // this should be unnecessary as I think all known joinpoints and helper methods
        // propagate the parameter names around correctly - but just in case let us do this
        // rather than fail. If a bug is raised reporting unknown as a local variable name
        // then investigate the joinpoint giving rise to the ResolvedMember and why it has
        // no parameter names specified
        argumentName = new StringBuffer("unknown").append(argNumber).toString();
      } else {
        argumentName = parameterNames.get(argNumber);
      }
      String argumentSignature = args[argNumber].getSignature();
      LocalVariableTag lvt = new LocalVariableTag(argumentSignature, argumentName, slot, 0);
      start.addTargeter(lvt);
      end.addTargeter(lvt);
      slot += args[argNumber].getSize();
    }
  }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    if (munger.getConcreteAspect().isAnnotationStyleAspect()) {
      // if we can't find one proceed() we suspect that the call
      // is happening in an inner class so we don't inline it.
      // Note: for code style, this is done at Aspect compilation time.
      boolean canSeeProceedPassedToOther = false;
      InstructionHandle curr = adviceMethod.getBody().getStart();
      InstructionHandle end = adviceMethod.getBody().getEnd();
      ConstantPool cpg = adviceMethod.getEnclosingClass().getConstantPool();
      while (curr != end) {
        InstructionHandle next = curr.getNext();
        Instruction inst = curr.getInstruction();
        if ((inst instanceof InvokeInstruction)
            && ((InvokeInstruction) inst).getSignature(cpg).indexOf("Lorg/aspectj/lang/ProceedingJoinPoint;") > 0) {
          // we may want to refine to exclude stuff returning jp ?
          // does code style skip inline if i write dump(thisJoinPoint) ?
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    body.append(InstructionConstants.ARETURN);
  }

  private Type[] getSuperConstructorParameterTypes() {
    // assert getKind() == PreInitialization
    InstructionHandle superCallHandle = getRange().getEnd().getNext();
    InvokeInstruction superCallInstruction = (InvokeInstruction) superCallHandle.getInstruction();
    return superCallInstruction.getArgumentTypes(getEnclosingClass().getConstantPool());
  }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    if (!gen.getName().equals("<clinit>")) {
      return false;
    }
    // System.err.println("checking clinig: " + gen);
    InstructionHandle start = gen.getBody().getStart();
    while (start != null) {
      if (Range.isRangeHandle(start) || (start.getInstruction().opcode == Constants.RETURN)) {
        start = start.getNext();
      } else {
        return false;
      }
    }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    boolean inlinedSomething = false;
    for (LazyMethodGen methodGen : methodGens) {
      if (!methodGen.getName().equals("<init>")) {
        continue;
      }
      InstructionHandle ih = findSuperOrThisCall(methodGen);
      if (ih != null && isThisCall(ih)) {
        LazyMethodGen donor = getCalledMethod(ih);
        inlineMethod(donor, methodGen, ih);
        inlinedSomething = true;
      }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

    }
  }

  private void positionInitializationShadow(BcelShadow s) {
    LazyMethodGen mg = s.getEnclosingMethod();
    InstructionHandle call = findSuperOrThisCall(mg);
    InstructionList body = mg.getBody();
    ShadowRange r = new ShadowRange(body);
    r.associateWithShadow(s);
    if (s.getKind() == Shadow.PreInitialization) {
      // XXX assert first instruction is an ALOAD_0.
      // a pre shadow goes from AFTER the first instruction (which we
      // believe to
      // be an ALOAD_0) to just before the call to super
      r.associateWithTargets(Range.genStart(body, body.getStart().getNext()), Range.genEnd(body, call.getPrev()));
    } else {
      // assert s.getKind() == Shadow.Initialization
      r.associateWithTargets(Range.genStart(body, call.getNext()), Range.genEnd(body));
    }
  }
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

        // ATHROW
        // L0: RETURN (line 23)

        // search for 'returns' and make them jump to the
        // aload_<n>,monitorexit
        InstructionHandle walker = body.getStart();
        List<InstructionHandle> rets = new ArrayList<InstructionHandle>();
        while (walker != null) {
          if (walker.getInstruction().isReturnInstruction()) {
            rets.add(walker);
          }
          walker = walker.getNext();
        }
        if (!rets.isEmpty()) {
          // need to ensure targeters for 'return' now instead target
          // the load instruction
          // (so we never jump over the monitorexit logic)

          for (Iterator<InstructionHandle> iter = rets.iterator(); iter.hasNext();) {
            InstructionHandle element = iter.next();
            InstructionList monitorExitBlock = new InstructionList();
            monitorExitBlock.append(InstructionFactory.createLoad(enclosingClassType, slotForLockObject));
            monitorExitBlock.append(InstructionConstants.MONITOREXIT);
            // monitorExitBlock.append(Utility.copyInstruction(element
            // .getInstruction()));
            // element.setInstruction(InstructionFactory.createLoad(
            // classType,slotForThis));
            InstructionHandle monitorExitBlockStart = body.insert(element, monitorExitBlock);

            // now move the targeters from the RET to the start of
            // the monitorexit block
            for (InstructionTargeter targeter : element.getTargetersCopy()) {
              // what kinds are there?
              if (targeter instanceof LocalVariableTag) {
                // ignore
              } else if (targeter instanceof LineNumberTag) {
                // ignore
                // } else if (targeter instanceof
                // InstructionBranch &&
                // ((InstructionBranch)targeter).isGoto()) {
                // // move it...
                // targeter.updateTarget(element,
                // monitorExitBlockStart);
              } else if (targeter instanceof InstructionBranch) {
                // move it
                targeter.updateTarget(element, monitorExitBlockStart);
              } else {
                throw new BCException("Unexpected targeter encountered during transform: " + targeter);
              }
            }
          }
        }

        // now the magic, putting the finally block around the code
        InstructionHandle finallyStart = finallyBlock.getStart();

        InstructionHandle tryPosition = body.getStart();
        InstructionHandle catchPosition = body.getEnd();
        body.insert(body.getStart(), prepend); // now we can put the
        // monitorenter stuff on
        synchronizedMethod.getBody().append(finallyBlock);
        synchronizedMethod.addExceptionHandler(tryPosition, catchPosition, finallyStart, null/* ==finally */, false);
        synchronizedMethod.addExceptionHandler(finallyStart, finallyStart.getNext(), finallyStart, null, false);
      } else {

        // TRANSFORMING STATIC METHOD ON PRE JAVA5

        // Hideous nightmare, class literal references prior to Java5

        // YIKES! this is just the code for MONITORENTER !
        // 0: getstatic #59; //Field class$1:Ljava/lang/Class;
        // 3: dup
        // 4: ifnonnull 32
        // 7: pop
        // try
        // 8: ldc #61; //String java.lang.String
        // 10: invokestatic #44; //Method
        // java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
        // 13: dup
        // catch
        // 14: putstatic #59; //Field class$1:Ljava/lang/Class;
        // 17: goto 32
        // 20: new #46; //class java/lang/NoClassDefFoundError
        // 23: dup_x1
        // 24: swap
        // 25: invokevirtual #52; //Method
        // java/lang/Throwable.getMessage:()Ljava/lang/String;
        // 28: invokespecial #54; //Method
        // java/lang/NoClassDefFoundError."<init>":(Ljava/lang/String;)V
        // 31: athrow
        // 32: dup <-- partTwo (branch target)
        // 33: astore_0
        // 34: monitorenter
        //
        // plus exceptiontable entry!
        // 8 13 20 Class java/lang/ClassNotFoundException
        Type classType = BcelWorld.makeBcelType(synchronizedMethod.getEnclosingClass().getType());
        Type clazzType = Type.getType(Class.class);

        InstructionList parttwo = new InstructionList();
        parttwo.append(InstructionFactory.createDup(1));
        int slotForThis = synchronizedMethod.allocateLocal(classType);
        parttwo.append(InstructionFactory.createStore(clazzType, slotForThis)); // ? should be the real type ? String or
        // something?
        parttwo.append(InstructionFactory.MONITORENTER);

        String fieldname = synchronizedMethod.getEnclosingClass().allocateField("class$");
        FieldGen f = new FieldGen(Modifier.STATIC | Modifier.PRIVATE, Type.getType(Class.class), fieldname,
            synchronizedMethod.getEnclosingClass().getConstantPool());
        synchronizedMethod.getEnclosingClass().addField(f, null);

        // 10: invokestatic #44; //Method
        // java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class;
        // 13: dup
        // 14: putstatic #59; //Field class$1:Ljava/lang/Class;
        // 17: goto 32
        // 20: new #46; //class java/lang/NoClassDefFoundError
        // 23: dup_x1
        // 24: swap
        // 25: invokevirtual #52; //Method
        // java/lang/Throwable.getMessage:()Ljava/lang/String;
        // 28: invokespecial #54; //Method
        // java/lang/NoClassDefFoundError."<init>":(Ljava/lang/String;)V
        // 31: athrow
        String name = synchronizedMethod.getEnclosingClass().getName();

        prepend.append(fact.createGetStatic(name, fieldname, Type.getType(Class.class)));
        prepend.append(InstructionFactory.createDup(1));
        prepend.append(InstructionFactory.createBranchInstruction(Constants.IFNONNULL, parttwo.getStart()));
        prepend.append(InstructionFactory.POP);

        prepend.append(fact.createConstant(name));
        InstructionHandle tryInstruction = prepend.getEnd();
        prepend.append(fact.createInvoke("java.lang.Class", "forName", clazzType,
            new Type[] { Type.getType(String.class) }, Constants.INVOKESTATIC));
        InstructionHandle catchInstruction = prepend.getEnd();
        prepend.append(InstructionFactory.createDup(1));

        prepend.append(fact.createPutStatic(synchronizedMethod.getEnclosingClass().getType().getName(), fieldname,
            Type.getType(Class.class)));
        prepend.append(InstructionFactory.createBranchInstruction(Constants.GOTO, parttwo.getStart()));

        // start of catch block
        InstructionList catchBlockForLiteralLoadingFail = new InstructionList();
        catchBlockForLiteralLoadingFail.append(fact.createNew((ObjectType) Type.getType(NoClassDefFoundError.class)));
        catchBlockForLiteralLoadingFail.append(InstructionFactory.createDup_1(1));
        catchBlockForLiteralLoadingFail.append(InstructionFactory.SWAP);
        catchBlockForLiteralLoadingFail.append(fact.createInvoke("java.lang.Throwable", "getMessage",
            Type.getType(String.class), new Type[] {}, Constants.INVOKEVIRTUAL));
        catchBlockForLiteralLoadingFail.append(fact.createInvoke("java.lang.NoClassDefFoundError", "<init>", Type.VOID,
            new Type[] { Type.getType(String.class) }, Constants.INVOKESPECIAL));
        catchBlockForLiteralLoadingFail.append(InstructionFactory.ATHROW);
        InstructionHandle catchBlockStart = catchBlockForLiteralLoadingFail.getStart();
        prepend.append(catchBlockForLiteralLoadingFail);
        prepend.append(parttwo);
        // MONITORENTER
        // pseudocode: load up 'this' (var0), dup it, store it in a new
        // local var (for use with monitorexit) and call
        // monitorenter:
        // ALOAD_0, DUP, ASTORE_<n>, MONITORENTER
        // prepend.append(InstructionFactory.createLoad(classType,0));
        // prepend.append(InstructionFactory.createDup(1));
        // int slotForThis =
        // synchronizedMethod.allocateLocal(classType);
        // prepend.append(InstructionFactory.createStore(classType,
        // slotForThis));
        // prepend.append(InstructionFactory.MONITORENTER);

        // MONITOREXIT
        // here be dragons

        // We basically need to wrap the code from the method in a
        // finally block that
        // will ensure monitorexit is called. Content on the finally
        // block seems to
        // be always:
        //
        // E1: ALOAD_1
        // MONITOREXIT
        // ATHROW
        //
        // so lets build that:
        InstructionList finallyBlock = new InstructionList();
        finallyBlock.append(InstructionFactory.createLoad(Type.getType(java.lang.Class.class), slotForThis));
        finallyBlock.append(InstructionConstants.MONITOREXIT);
        finallyBlock.append(InstructionConstants.ATHROW);

        // finally -> E1
        // | GETSTATIC java.lang.System.out Ljava/io/PrintStream; (line
        // 21)
        // | LDC "hello"
        // | INVOKEVIRTUAL java.io.PrintStream.println
        // (Ljava/lang/String;)V
        // | ALOAD_1 (line 20)
        // | MONITOREXIT
        // finally -> E1
        // GOTO L0
        // finally -> E1
        // | E1: ALOAD_1
        // | MONITOREXIT
        // finally -> E1
        // ATHROW
        // L0: RETURN (line 23)
        // frameEnv.put(donorFramePos, thisSlot);

        // search for 'returns' and make them to the
        // aload_<n>,monitorexit
        InstructionHandle walker = body.getStart();
        List<InstructionHandle> rets = new ArrayList<InstructionHandle>();
        while (walker != null) { // !walker.equals(body.getEnd())) {
          if (walker.getInstruction().isReturnInstruction()) {
            rets.add(walker);
          }
          walker = walker.getNext();
        }
        if (rets.size() > 0) {
          // need to ensure targeters for 'return' now instead target
          // the load instruction
          // (so we never jump over the monitorexit logic)

          for (InstructionHandle ret : rets) {
            // System.err.println("Adding monitor exit block at "+
            // element);
            InstructionList monitorExitBlock = new InstructionList();
            monitorExitBlock.append(InstructionFactory.createLoad(classType, slotForThis));
            monitorExitBlock.append(InstructionConstants.MONITOREXIT);
            // monitorExitBlock.append(Utility.copyInstruction(element
            // .getInstruction()));
            // element.setInstruction(InstructionFactory.createLoad(
            // classType,slotForThis));
            InstructionHandle monitorExitBlockStart = body.insert(ret, monitorExitBlock);

            // now move the targeters from the RET to the start of
            // the monitorexit block
            for (InstructionTargeter targeter : ret.getTargetersCopy()) {
              // what kinds are there?
              if (targeter instanceof LocalVariableTag) {
                // ignore
              } else if (targeter instanceof LineNumberTag) {
                // ignore
                // } else if (targeter instanceof GOTO ||
                // targeter instanceof GOTO_W) {
                // // move it...
                // targeter.updateTarget(element,
                // monitorExitBlockStart);
              } else if (targeter instanceof InstructionBranch) {
                // move it
                targeter.updateTarget(ret, monitorExitBlockStart);
              } else {
                throw new BCException("Unexpected targeter encountered during transform: " + targeter);
              }
            }
          }
        }
        // body =
        // rewriteWithMonitorExitCalls(body,fact,true,slotForThis,
        // classType);
        // synchronizedMethod.setBody(body);

        // now the magic, putting the finally block around the code
        InstructionHandle finallyStart = finallyBlock.getStart();

        InstructionHandle tryPosition = body.getStart();
        InstructionHandle catchPosition = body.getEnd();
        body.insert(body.getStart(), prepend); // now we can put the
        // monitorenter stuff on

        synchronizedMethod.getBody().append(finallyBlock);
        synchronizedMethod.addExceptionHandler(tryPosition, catchPosition, finallyStart, null/* ==finally */, false);
        synchronizedMethod.addExceptionHandler(tryInstruction, catchInstruction, catchBlockStart,
            (ObjectType) Type.getType(ClassNotFoundException.class), true);
        synchronizedMethod.addExceptionHandler(finallyStart, finallyStart.getNext(), finallyStart, null, false);
      }
    } else {

      // TRANSFORMING NON STATIC METHOD
      Type classType = BcelWorld.makeBcelType(synchronizedMethod.getEnclosingClass().getType());
      // MONITORENTER
      // pseudocode: load up 'this' (var0), dup it, store it in a new
      // local var (for use with monitorexit) and call
      // monitorenter:
      // ALOAD_0, DUP, ASTORE_<n>, MONITORENTER
      prepend.append(InstructionFactory.createLoad(classType, 0));
      prepend.append(InstructionFactory.createDup(1));
      int slotForThis = synchronizedMethod.allocateLocal(classType);
      prepend.append(InstructionFactory.createStore(classType, slotForThis));
      prepend.append(InstructionFactory.MONITORENTER);
      // body.insert(body.getStart(),prepend);

      // MONITOREXIT

      // We basically need to wrap the code from the method in a finally
      // block that
      // will ensure monitorexit is called. Content on the finally block
      // seems to
      // be always:
      //
      // E1: ALOAD_1
      // MONITOREXIT
      // ATHROW
      //
      // so lets build that:
      InstructionList finallyBlock = new InstructionList();
      finallyBlock.append(InstructionFactory.createLoad(classType, slotForThis));
      finallyBlock.append(InstructionConstants.MONITOREXIT);
      finallyBlock.append(InstructionConstants.ATHROW);

      // finally -> E1
      // | GETSTATIC java.lang.System.out Ljava/io/PrintStream; (line 21)
      // | LDC "hello"
      // | INVOKEVIRTUAL java.io.PrintStream.println (Ljava/lang/String;)V
      // | ALOAD_1 (line 20)
      // | MONITOREXIT
      // finally -> E1
      // GOTO L0
      // finally -> E1
      // | E1: ALOAD_1
      // | MONITOREXIT
      // finally -> E1
      // ATHROW
      // L0: RETURN (line 23)
      // frameEnv.put(donorFramePos, thisSlot);

      // search for 'returns' and make them to the aload_<n>,monitorexit
      InstructionHandle walker = body.getStart();
      List<InstructionHandle> rets = new ArrayList<InstructionHandle>();
      while (walker != null) { // !walker.equals(body.getEnd())) {
        if (walker.getInstruction().isReturnInstruction()) {
          rets.add(walker);
        }
        walker = walker.getNext();
      }
      if (!rets.isEmpty()) {
        // need to ensure targeters for 'return' now instead target the
        // load instruction
        // (so we never jump over the monitorexit logic)

        for (Iterator<InstructionHandle> iter = rets.iterator(); iter.hasNext();) {
          InstructionHandle element = iter.next();
          // System.err.println("Adding monitor exit block at "+element
          // );
          InstructionList monitorExitBlock = new InstructionList();
          monitorExitBlock.append(InstructionFactory.createLoad(classType, slotForThis));
          monitorExitBlock.append(InstructionConstants.MONITOREXIT);
          // monitorExitBlock.append(Utility.copyInstruction(element.
          // getInstruction()));
          // element.setInstruction(InstructionFactory.createLoad(
          // classType,slotForThis));
          InstructionHandle monitorExitBlockStart = body.insert(element, monitorExitBlock);

          // now move the targeters from the RET to the start of the
          // monitorexit block
          for (InstructionTargeter targeter : element.getTargetersCopy()) {
            // what kinds are there?
            if (targeter instanceof LocalVariableTag) {
              // ignore
            } else if (targeter instanceof LineNumberTag) {
              // ignore
              // } else if (targeter instanceof GOTO ||
              // targeter instanceof GOTO_W) {
              // // move it...
              // targeter.updateTarget(element,
              // monitorExitBlockStart);
            } else if (targeter instanceof InstructionBranch) {
              // move it
              targeter.updateTarget(element, monitorExitBlockStart);
            } else {
              throw new BCException("Unexpected targeter encountered during transform: " + targeter);
            }
          }
        }
      }

      // now the magic, putting the finally block around the code
      InstructionHandle finallyStart = finallyBlock.getStart();

      InstructionHandle tryPosition = body.getStart();
      InstructionHandle catchPosition = body.getEnd();
      body.insert(body.getStart(), prepend); // now we can put the
      // monitorenter stuff on
      synchronizedMethod.getBody().append(finallyBlock);
      synchronizedMethod.addExceptionHandler(tryPosition, catchPosition, finallyStart, null/* ==finally */, false);
      synchronizedMethod.addExceptionHandler(finallyStart, finallyStart.getNext(), finallyStart, null, false);
View Full Code Here

Examples of org.aspectj.apache.bcel.generic.InstructionHandle

   * @param fact an instruction factory for recipient
   */
  static InstructionList genInlineInstructions(LazyMethodGen donor, LazyMethodGen recipient, IntMap frameEnv,
      InstructionFactory fact, boolean keepReturns) {
    InstructionList footer = new InstructionList();
    InstructionHandle end = footer.append(InstructionConstants.NOP);

    InstructionList ret = new InstructionList();
    InstructionList sourceList = donor.getBody();

    Map<InstructionHandle, InstructionHandle> srcToDest = new HashMap<InstructionHandle, InstructionHandle>();
    ConstantPool donorCpg = donor.getEnclosingClass().getConstantPool();
    ConstantPool recipientCpg = recipient.getEnclosingClass().getConstantPool();

    boolean isAcrossClass = donorCpg != recipientCpg;

    // first pass: copy the instructions directly, populate the srcToDest
    // map,
    // fix frame instructions
    for (InstructionHandle src = sourceList.getStart(); src != null; src = src.getNext()) {
      Instruction fresh = Utility.copyInstruction(src.getInstruction());
      InstructionHandle dest;

      // OPTIMIZE optimize this stuff?
      if (fresh.isConstantPoolInstruction()) {
        // need to reset index to go to new constant pool. This is
        // totally
        // a computation leak... we're testing this LOTS of times. Sigh.
        if (isAcrossClass) {
          InstructionCP cpi = (InstructionCP) fresh;
          cpi.setIndex(recipientCpg.addConstant(donorCpg.getConstant(cpi.getIndex()), donorCpg));
        }
      }
      if (src.getInstruction() == Range.RANGEINSTRUCTION) {
        dest = ret.append(Range.RANGEINSTRUCTION);
      } else if (fresh.isReturnInstruction()) {
        if (keepReturns) {
          dest = ret.append(fresh);
        } else {
          dest = ret.append(InstructionFactory.createBranchInstruction(Constants.GOTO, end));
        }
      } else if (fresh instanceof InstructionBranch) {
        dest = ret.append((InstructionBranch) fresh);
      } else if (fresh.isLocalVariableInstruction() || fresh instanceof RET) {

        // IndexedInstruction indexed = (IndexedInstruction) fresh;
        int oldIndex = fresh.getIndex();
        int freshIndex;
        if (!frameEnv.hasKey(oldIndex)) {
          freshIndex = recipient.allocateLocal(2);
          frameEnv.put(oldIndex, freshIndex);
        } else {
          freshIndex = frameEnv.get(oldIndex);
        }
        if (fresh instanceof RET) {
          fresh.setIndex(freshIndex);
        } else {
          fresh = ((InstructionLV) fresh).setIndexAndCopyIfNecessary(freshIndex);
        }
        dest = ret.append(fresh);
      } else {
        dest = ret.append(fresh);
      }
      srcToDest.put(src, dest);
    }

    // second pass: retarget branch instructions, copy ranges and tags
    Map<Tag, Tag> tagMap = new HashMap<Tag, Tag>();
    Map<BcelShadow, BcelShadow> shadowMap = new HashMap<BcelShadow, BcelShadow>();
    for (InstructionHandle dest = ret.getStart(), src = sourceList.getStart(); dest != null; dest = dest.getNext(), src = src
        .getNext()) {
      Instruction inst = dest.getInstruction();

      // retarget branches
      if (inst instanceof InstructionBranch) {
        InstructionBranch branch = (InstructionBranch) inst;
        InstructionHandle oldTarget = branch.getTarget();
        InstructionHandle newTarget = srcToDest.get(oldTarget);
        if (newTarget == null) {
          // assert this is a GOTO
          // this was a return instruction we previously replaced
        } else {
          branch.setTarget(newTarget);
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.