Package org.aspectj.apache.bcel.generic

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


    InstOperandConstraintVisitor v = new InstOperandConstraintVisitor(cpg);
 
    // Checks for the things BCEL does _not_ handle itself.
    InstructionHandle ih = instructionList.getStart();
    while (ih != null){
      Instruction i = ih.getInstruction();
     
      // An "own" constraint, due to JustIce's new definition of what "subroutine" means.
      if (i instanceof JsrInstruction){
        InstructionHandle target = ((JsrInstruction) i).getTarget();
        if (target == instructionList.getStart()){
View Full Code Here


      freshMethod.assertGoodBody();
        InstructionList freshBody = freshMethod.getBody();

        for (InstructionHandle oldIh = start.getNext(); oldIh != end; oldIh = oldIh.getNext()) {
        // first we copy the instruction itself. 
            Instruction oldI = oldIh.getInstruction();
            Instruction freshI = (oldI == RANGEINSTRUCTION) ? oldI : Utility.copyInstruction(oldI);

      // Now we add it to the new instruction list.
            InstructionHandle freshIh;
            if (freshI instanceof BranchInstruction) {
        //If it's a targeting instruction,
View Full Code Here

    final ConstantPoolGen cpg = getEnclosingClass().getConstantPoolGen();
    int depth = 1;
    InstructionHandle ih = range.getStart();

    while (true) {
      Instruction inst = ih.getInstruction();
      if (inst instanceof INVOKESPECIAL
        && ((INVOKESPECIAL) inst).getName(cpg).equals("<init>")) {
        depth++;
      } else if (inst instanceof NEW) {
        depth--;
View Full Code Here

   * (Unless we have a void return type in which case there's nothing)
   */
    public void weaveAfterReturning(BcelAdvice munger) {
        // InstructionFactory fact = getFactory();
        List returns = new ArrayList();
        Instruction ret = null;
        for (InstructionHandle ih = range.getStart(); ih != range.getEnd(); ih = ih.getNext()) {
            if (ih.getInstruction() instanceof ReturnInstruction) {
                returns.add(ih);
                ret = Utility.copyInstruction(ih.getInstruction());
            }
View Full Code Here

            InstructionHandle curr = adviceMethod.getBody().getStart();
            InstructionHandle end = adviceMethod.getBody().getEnd();
            ConstantPoolGen cpg = adviceMethod.getEnclosingClass().getConstantPoolGen();
            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) ?
                    canSeeProceedPassedToOther = true;// we see one pjp passed around - dangerous
                    break;
                }
                curr = next;
            }
            if (canSeeProceedPassedToOther) {
                // remember this decision to avoid re-analysis
                adviceMethod.setCanInline(false);
                weaveAroundClosure(munger, hasDynamicTest);
                return;
            }
        }



    // We can't inline around methods if they have around advice on them, this
    // is because the weaving will extract the body and hence the proceed call.
    //??? should consider optimizations to recognize simple cases that don't require body extraction
    enclosingMethod.setCanInline(false);
   
    // start by exposing various useful things into the frame
    final InstructionFactory fact = getFactory();
   
    // now generate the aroundBody method
        LazyMethodGen extractedMethod =
          extractMethod(
            NameMangler.aroundCallbackMethodName(
              getSignature(),
              getEnclosingClass()),
        Modifier.PRIVATE,
        munger
            );
             
             
        // now extract the advice into its own method
        String adviceMethodName =
      NameMangler.aroundCallbackMethodName(
              getSignature(),
              getEnclosingClass()) + "$advice";
       
    List argVarList = new ArrayList();
    List proceedVarList = new ArrayList();
    int extraParamOffset = 0;
   
    // Create the extra parameters that are needed for passing to proceed
    // This code is very similar to that found in makeCallToCallback and should
    // be rationalized in the future
    if (thisVar != null) {
      argVarList.add(thisVar);
      proceedVarList.add(new BcelVar(thisVar.getType(), extraParamOffset));
      extraParamOffset += thisVar.getType().getSize();
    }
   
    if (targetVar != null && targetVar != thisVar) {
      argVarList.add(targetVar);
      proceedVarList.add(new BcelVar(targetVar.getType(), extraParamOffset));
      extraParamOffset += targetVar.getType().getSize();
    }
    for (int i = 0, len = getArgCount(); i < len; i++) {
      argVarList.add(argVars[i]);
      proceedVarList.add(new BcelVar(argVars[i].getType(), extraParamOffset));
      extraParamOffset += argVars[i].getType().getSize();
    }
    if (thisJoinPointVar != null) {
      argVarList.add(thisJoinPointVar);
      proceedVarList.add(new BcelVar(thisJoinPointVar.getType(), extraParamOffset));
      extraParamOffset += thisJoinPointVar.getType().getSize();
    }
       
        Type[] adviceParameterTypes = adviceMethod.getArgumentTypes();
        Type[] extractedMethodParameterTypes = extractedMethod.getArgumentTypes();
    Type[] parameterTypes =
      new Type[extractedMethodParameterTypes.length
        + adviceParameterTypes.length
        + 1];
    int parameterIndex = 0;
    System.arraycopy(
      extractedMethodParameterTypes,
      0,
      parameterTypes,
      parameterIndex,
      extractedMethodParameterTypes.length);
    parameterIndex += extractedMethodParameterTypes.length;

    parameterTypes[parameterIndex++] =
      BcelWorld.makeBcelType(adviceMethod.getEnclosingClass().getType());
    System.arraycopy(
      adviceParameterTypes,
      0,
      parameterTypes,
      parameterIndex,
      adviceParameterTypes.length);

        LazyMethodGen localAdviceMethod =
          new LazyMethodGen(
            Modifier.PRIVATE | Modifier.FINAL | Modifier.STATIC,
            BcelWorld.makeBcelType(mungerSig.getReturnType()),
            adviceMethodName,
            parameterTypes,
            new String[0],
            getEnclosingClass());
    String donorFileName = adviceMethod.getEnclosingClass().getInternalFileName();
    String recipientFileName = getEnclosingClass().getInternalFileName();
//    System.err.println("donor " + donorFileName);
//    System.err.println("recip " + recipientFileName);
    if (! donorFileName.equals(recipientFileName)) {
      localAdviceMethod.fromFilename = donorFileName;
      getEnclosingClass().addInlinedSourceFileInfo(
        donorFileName,
        adviceMethod.highestLineNumber);
    }
   
    getEnclosingClass().addMethodGen(localAdviceMethod);
   
    // create a map that will move all slots in advice method forward by extraParamOffset
    // in order to make room for the new proceed-required arguments that are added at
    // the beginning of the parameter list
    int nVars = adviceMethod.getMaxLocals() + extraParamOffset;
    IntMap varMap = IntMap.idMap(nVars);
    for (int i=extraParamOffset; i < nVars; i++) {
      varMap.put(i-extraParamOffset, i);
    }

    localAdviceMethod.getBody().insert(
      BcelClassWeaver.genInlineInstructions(adviceMethod,
          localAdviceMethod, varMap, fact, true));


         
    localAdviceMethod.setMaxLocals(nVars);
         
    //System.err.println(localAdviceMethod);
   
   
      // the shadow is now empty.  First, create a correct call
      // to the around advice.  This includes both the call (which may involve
      // value conversion of the advice arguments) and the return
      // (which may involve value conversion of the return value).  Right now
      // we push a null for the unused closure.  It's sad, but there it is.
           
      InstructionList advice = new InstructionList();
        // InstructionHandle adviceMethodInvocation;
        {
      for (Iterator i = argVarList.iterator(); i.hasNext(); ) {
        BcelVar var = (BcelVar)i.next();
        var.appendLoad(advice, fact);
      }        
          // ??? we don't actually need to push NULL for the closure if we take care
      advice.append(
        munger.getAdviceArgSetup(
          this,
          null,
                    (munger.getConcreteAspect().isAnnotationStyleAspect())?
                        this.loadThisJoinPoint():
              new InstructionList(InstructionConstants.ACONST_NULL)));
        // adviceMethodInvocation =
            advice.append(
              Utility.createInvoke(fact, localAdviceMethod)); //(fact, getWorld(), munger.getSignature()));
      advice.append(
            Utility.createConversion(
                getFactory(),
                BcelWorld.makeBcelType(mungerSig.getReturnType()),
                extractedMethod.getReturnType(),world.isInJava5Mode()));
        if (! isFallsThrough()) {
            advice.append(InstructionFactory.createReturn(extractedMethod.getReturnType()));
        }
        }
       
    // now, situate the call inside the possible dynamic tests,
    // and actually add the whole mess to the shadow
        if (! hasDynamicTest) {
            range.append(advice);
        } else {
          InstructionList afterThingie = new InstructionList(InstructionConstants.NOP);
            InstructionList callback = makeCallToCallback(extractedMethod);
      if (terminatesWithReturn()) {
        callback.append(
          InstructionFactory.createReturn(extractedMethod.getReturnType()));
      } else {
        //InstructionHandle endNop = range.insert(fact.NOP, Range.InsideAfter);
        advice.append(
          InstructionFactory.createBranchInstruction(
            Constants.GOTO,
            afterThingie.getStart()));
      }
      range.append(
        munger.getTestInstructions(
          this,
          advice.getStart(),
          callback.getStart(),
          advice.getStart()));
            range.append(advice);
            range.append(callback);
            range.append(afterThingie);         
        }       


        // now search through the advice, looking for a call to PROCEED. 
        // Then we replace the call to proceed with some argument setup, and a
        // call to the extracted method.

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

            InstructionHandle curr = localAdviceMethod.getBody().getStart();
            InstructionHandle end = localAdviceMethod.getBody().getEnd();
            ConstantPoolGen cpg = localAdviceMethod.getEnclosingClass().getConstantPoolGen();
            while (curr != end) {
                InstructionHandle next = curr.getNext();
                Instruction inst = curr.getInstruction();
                if ((inst instanceof INVOKESTATIC)
                    && proceedName.equals(((INVOKESTATIC) inst).getMethodName(cpg))) {

                    localAdviceMethod.getBody().append(
                        curr,
                        getRedoneProceedCall(
                            fact,
                            extractedMethod,
                            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();
            ConstantPoolGen cpg = localAdviceMethod.getEnclosingClass().getConstantPoolGen();
            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[]
View Full Code Here

    }

  public static Instruction createConstant(
        InstructionFactory fact,
        int i) {
    Instruction inst;
    switch(i) {
      case -1: inst =  InstructionConstants.ICONST_M1; break;
      case 0: inst =  InstructionConstants.ICONST_0;  break;     
      case 1: inst =  InstructionConstants.ICONST_1; break;
      case 2: inst =  InstructionConstants.ICONST_2; break;       
View Full Code Here

      int size = executionPredecessors.size();
      int retcount = 0;
     
      for (int i=size-1; i>=0; i--){
        InstructionContextImpl current = (InstructionContextImpl) (executionPredecessors.get(i));
        Instruction currentlast = current.getInstruction().getInstruction();
        if (currentlast instanceof RET) retcount++;
        if (currentlast instanceof JsrInstruction){
          retcount--;
          if (retcount == -1) return current;
        }
View Full Code Here

    private InstructionHandle[] _getSuccessors(){
      final InstructionHandle[] empty = new InstructionHandle[0];
      final InstructionHandle[] single = new InstructionHandle[1];
      final InstructionHandle[] pair = new InstructionHandle[2];
   
      Instruction inst = getInstruction().getInstruction();
   
      if (inst instanceof RET){
        Subroutine s = subroutines.subroutineOf(getInstruction());
        if (s==null){ //return empty; // RET in dead code. "empty" would be the correct answer, but we know something about the surrounding project...
          throw new AssertionViolatedException("Asking for successors of a RET in dead code?!");
View Full Code Here

            if (realizedCannotInline) {
                // we know we cannot inline this advice so no need for futher handling
                break;
            }
            InstructionHandle next = curr.getNext();
            Instruction inst = curr.getInstruction();

            // open-up method call
            if ((inst instanceof InvokeInstruction)) {
                InvokeInstruction invoke = (InvokeInstruction) inst;
                ResolvedType callee = m_aspectGen.getWorld().resolve(UnresolvedType.forName(invoke.getClassName(cpg)));
View Full Code Here

        void printInstruction(InstructionHandle h, int depth) {
            printDepth(depth);
            printLabel((String) labelMap.get(h), depth);  
          
            Instruction inst = h.getInstruction();
            if (inst instanceof CPInstruction) {
                CPInstruction cpinst = (CPInstruction) inst;
                out.print(Constants.OPCODE_NAMES[cpinst.getOpcode()].toUpperCase());
                out.print(" ");
                out.print(pool.constantToString(pool.getConstant(cpinst.getIndex())));
            } else if (inst instanceof Select) {
                Select sinst = (Select) inst;
                out.println(Constants.OPCODE_NAMES[sinst.getOpcode()].toUpperCase());
                int[] matches = sinst.getMatchs();
                InstructionHandle[] targets = sinst.getTargets();
                InstructionHandle defaultTarget = sinst.getTarget();
                for (int i = 0, len = matches.length; i < len; i++) {
                    printDepth(depth);
                    printLabel(null, depth);
                    out.print("  ");
                    out.print(matches[i]);
                    out.print(": \t");
                    out.println(labelMap.get(targets[i]));
                }
                printDepth(depth);
                printLabel(null, depth);
                out.print("  ");
                out.print("default: \t");
                out.print(labelMap.get(defaultTarget));
            } else if (inst instanceof BranchInstruction) {
                BranchInstruction brinst = (BranchInstruction) inst;
                out.print(Constants.OPCODE_NAMES[brinst.getOpcode()].toUpperCase());
                out.print(" ");
                out.print(labelMap.get(brinst.getTarget()));
            } else if (inst instanceof LocalVariableInstruction) {
                LocalVariableInstruction lvinst = (LocalVariableInstruction) inst;
                out.print(inst.toString(false).toUpperCase());
                int index = lvinst.getIndex();
                LocalVariableTag tag = getLocalVariableTag(h, index);
                if (tag != null) {
                    out.print("     // ");
                    out.print(tag.getType());
                    out.print(" ");
                    out.print(tag.getName());
                }
            } else {
                out.print(inst.toString(false).toUpperCase());
            }
        }
View Full Code Here

TOP

Related Classes of org.aspectj.apache.bcel.generic.Instruction

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.