Package org.aspectj.apache.bcel.generic

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


  }

  private void deleteNewAndDup() {
    final ConstantPool cpg = getEnclosingClass().getConstantPool();
    int depth = 1;
    InstructionHandle ih = range.getStart();

    // Go back from where we are looking for 'NEW' that takes us to a stack depth of 0. INVOKESPECIAL <init>
    while (true) {
      Instruction inst = ih.getInstruction();
      if (inst.opcode == Constants.INVOKESPECIAL && ((InvokeInstruction) inst).getName(cpg).equals("<init>")) {
        depth++;
      } else if (inst.opcode == Constants.NEW) {
        depth--;
        if (depth == 0) {
          break;
        }
      } else if (inst.opcode == Constants.DUP_X2) {
        // This code seen in the wild (by Brad):
        // 40: new #12; //class java/lang/StringBuffer
        // STACK: STRINGBUFFER
        // 43: dup
        // STACK: STRINGBUFFER/STRINGBUFFER
        // 44: aload_0
        // STACK: STRINGBUFFER/STRINGBUFFER/THIS
        // 45: dup_x2
        // STACK: THIS/STRINGBUFFER/STRINGBUFFER/THIS
        // 46: getfield #36; //Field value:Ljava/lang/String;
        // STACK: THIS/STRINGBUFFER/STRINGBUFFER/STRING<value>
        // 49: invokestatic #37; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
        // STACK: THIS/STRINGBUFFER/STRINGBUFFER/STRING
        // 52: invokespecial #19; //Method java/lang/StringBuffer."<init>":(Ljava/lang/String;)V
        // STACK: THIS/STRINGBUFFER
        // 55: aload_1
        // STACK: THIS/STRINGBUFFER/LOCAL1
        // 56: invokevirtual #22; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
        // STACK: THIS/STRINGBUFFER
        // 59: invokevirtual #34; //Method java/lang/StringBuffer.toString:()Ljava/lang/String;
        // STACK: THIS/STRING
        // 62: putfield #36; //Field value:Ljava/lang/String;
        // STACK: <empty>
        // 65: return

        // if we attempt to match on the ctor call to StringBuffer.<init> then we get into trouble.
        // if we simply delete the new/dup pair without fixing up the dup_x2 then the dup_x2 will fail due to there
        // not being 3 elements on the stack for it to work with. The fix *in this situation* is to change it to
        // a simple 'dup'

        // this fix is *not* very clean - but a general purpose decent solution will take much longer and this
        // bytecode sequence has only been seen once in the wild.
        ih.setInstruction(InstructionConstants.DUP);
      }
      ih = ih.getPrev();
    }
    // now IH points to the NEW. We're followed by the DUP, and that is followed
    // by the actual instruction we care about.
    InstructionHandle newHandle = ih;
    InstructionHandle endHandle = newHandle.getNext();
    InstructionHandle nextHandle;
    if (endHandle.getInstruction().opcode == Constants.DUP) {
      nextHandle = endHandle.getNext();
      retargetFrom(newHandle, nextHandle);
      retargetFrom(endHandle, nextHandle);
    } else if (endHandle.getInstruction().opcode == Constants.DUP_X1) {
      InstructionHandle dupHandle = endHandle;
      endHandle = endHandle.getNext();
      nextHandle = endHandle.getNext();
      boolean skipEndRepositioning = false;
      if (endHandle.getInstruction().opcode == Constants.SWAP) {
      } else if (endHandle.getInstruction().opcode == Constants.IMPDEP1) {
View Full Code Here


        il.append(fact.createInvoke("java/lang/reflect/Method", "getAnnotation", jlaAnnotation, new Type[] { jlClass },
            Constants.INVOKEVIRTUAL));
        il.append(InstructionConstants.DUP);
        il.append(fact
            .createPutStatic(shadow.getEnclosingClass().getName(), annotationCachingField.getName(), jlAnnotation));
        InstructionHandle ifNullElse = il.append(InstructionConstants.NOP);
        ifNonNull.setTarget(ifNullElse);

      } else { // init/preinit/ctor-call/ctor-exec
        il.append(fact.createConstant(BcelWorld.makeBcelType(containingType)));
        buildArray(il, fact, jlClass, paramTypes, 1);
View Full Code Here

   * @param enclosingMethod where to find ih's instruction list.
   */
  public static void replaceInstruction(InstructionHandle ih, InstructionList replacementInstructions,
      LazyMethodGen enclosingMethod) {
    InstructionList il = enclosingMethod.getBody();
    InstructionHandle fresh = il.append(ih, replacementInstructions);
    deleteInstruction(ih, fresh, enclosingMethod);
  }
View Full Code Here

   * Prepare the around advice, flag it as cannot be inlined if it can't be
   *
   * @param aroundAdvice
   */
  private void openAroundAdvice(LazyMethodGen aroundAdvice) {
    InstructionHandle curr = aroundAdvice.getBody().getStart();
    InstructionHandle end = aroundAdvice.getBody().getEnd();
    ConstantPool cpg = aroundAdvice.getEnclosingClass().getConstantPool();
    InstructionFactory factory = aroundAdvice.getEnclosingClass().getFactory();

    boolean realizedCannotInline = false;
    while (curr != end) {
      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;
View Full Code Here

      }
    }
    il.append(getAdviceArgSetup(shadow, extraArgVar, null));
    il.append(getNonTestAdviceInstructions(shadow));

    InstructionHandle ifYesAdvice = il.getStart();
    il.insert(getTestInstructions(shadow, ifYesAdvice, ifNoAdvice, ifYesAdvice));

    // If inserting instructions at the start of a method, we need a nice line number for this entry
    // in the stack trace
    if (shadow.getKind() == Shadow.MethodExecution && getKind() == AdviceKind.Before) {
      int lineNumber = 0;
      // Uncomment this code if you think we should use the method decl line number when it exists...
      // // If the advised join point is in a class built by AspectJ, we can use the declaration line number
      // boolean b = shadow.getEnclosingMethod().getMemberView().hasDeclarationLineNumberInfo();
      // if (b) {
      // lineNumber = shadow.getEnclosingMethod().getMemberView().getDeclarationLineNumber();
      // } else { // If it wasn't, the best we can do is the line number of the first instruction in the method
      lineNumber = shadow.getEnclosingMethod().getMemberView().getLineNumberOfFirstInstruction();
      // }
      InstructionHandle start = il.getStart();
      if (lineNumber > 0) {
        start.addTargeter(new LineNumberTag(lineNumber));
      }
      // Fix up the local variables: find any that have a startPC of 0 and ensure they target the new start of the method
      LocalVariableTable lvt = shadow.getEnclosingMethod().getMemberView().getMethod().getLocalVariableTable();
      if (lvt != null) {
        LocalVariable[] lvTable = lvt.getLocalVariableTable();
        for (int i = 0; i < lvTable.length; i++) {
          LocalVariable lv = lvTable[i];
          if (lv.getStartPC() == 0) {
            start.addTargeter(new LocalVariableTag(lv.getSignature(), lv.getName(), lv.getIndex(), 0));
          }
        }
      }
    }
View Full Code Here

    // Look for ctors to modify
    for (LazyMethodGen aMethod : mgs) {
      if (LazyMethodGen.isConstructor(aMethod)) {
        InstructionList insList = aMethod.getBody();
        InstructionHandle handle = insList.getStart();
        while (handle != null) {
          if (handle.getInstruction().opcode == Constants.INVOKESPECIAL) {
            ConstantPool cpg = newParentTarget.getConstantPool();
            InvokeInstruction invokeSpecial = (InvokeInstruction) handle.getInstruction();
            if (invokeSpecial.getClassName(cpg).equals(currentParent)
                && invokeSpecial.getMethodName(cpg).equals("<init>")) {
              // System.err.println("Transforming super call '<init>" + invokeSpecial.getSignature(cpg) + "'");

              // 1. Check there is a ctor in the new parent with
              // the same signature
              ResolvedMember newCtor = getConstructorWithSignature(newParent, invokeSpecial.getSignature(cpg));

              if (newCtor == null) {

                // 2. Check ITDCs to see if the necessary ctor is provided that way
                boolean satisfiedByITDC = false;
                for (Iterator<ConcreteTypeMunger> ii = newParentTarget.getType()
                    .getInterTypeMungersIncludingSupers().iterator(); ii.hasNext() && !satisfiedByITDC;) {
                  ConcreteTypeMunger m = ii.next();
                  if (m.getMunger() instanceof NewConstructorTypeMunger) {
                    if (m.getSignature().getSignature().equals(invokeSpecial.getSignature(cpg))) {
                      satisfiedByITDC = true;
                    }
                  }
                }

                if (!satisfiedByITDC) {
                  String csig = createReadableCtorSig(newParent, cpg, invokeSpecial);
                  weaver.getWorld()
                      .getMessageHandler()
                      .handleMessage(
                          MessageUtil.error(
                              "Unable to modify hierarchy for " + newParentTarget.getClassName()
                                  + " - the constructor " + csig + " is missing",
                              this.getSourceLocation()));
                  return false;
                }
              }

              int idx = cpg.addMethodref(newParent.getName(), invokeSpecial.getMethodName(cpg),
                  invokeSpecial.getSignature(cpg));
              invokeSpecial.setIndex(idx);
            }
          }
          handle = handle.getNext();
        }
      }
    }
    return true;
  }
View Full Code Here

        body.append(fact.createInvoke(munger.getImplClassName(), "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
        body.append(Utility.createSet(fact, munger.getDelegate(weaver.getLazyClassGen().getType())));
      }

      // if not null use the instance we've got
      InstructionHandle ifNonNullElse = body.append(InstructionConstants.ALOAD_0);
      ifNonNull.setTarget(ifNonNullElse);
      body.append(Utility.createGet(fact, munger.getDelegate(weaver.getLazyClassGen().getType())));

      // args
      int pos = 0;
View Full Code Here

  }

  // ---- test visitors

  public void visit(And e) {
    InstructionHandle savedFk = fk;
    recur(e.getRight(), sk, fk, next);
    InstructionHandle ning = instructions.getStart();
    recur(e.getLeft(), ning, savedFk, ning);
  }
View Full Code Here

    InstructionHandle ning = instructions.getStart();
    recur(e.getLeft(), ning, savedFk, ning);
  }

  public void visit(Or e) {
    InstructionHandle savedSk = sk;
    recur(e.getRight(), sk, fk, next);
    recur(e.getLeft(), savedSk, instructions.getStart(), instructions.getStart());
  }
View Full Code Here

      range.insert(InstructionConstants.NOP, Range.InsideAfter);
    } else if (getKind() == ExceptionHandler) {

      ShadowRange range = getRange();
      InstructionList body = range.getBody();
      InstructionHandle start = range.getStart();

      // Create a store instruction to put the value from the top of the
      // stack into a local variable slot. This is a trimmed version of
      // what is in initializeArgVars() (since there is only one argument
      // at a handler jp and only before advice is supported) (pr46298)
      argVars = new BcelVar[1];
      // int positionOffset = (hasTarget() ? 1 : 0) + ((hasThis() && !getKind().isTargetSameAsThis()) ? 1 : 0);
      UnresolvedType tx = getArgType(0);
      argVars[0] = genTempVar(tx, "ajc$arg0");
      InstructionHandle insertedInstruction = range.insert(argVars[0].createStore(getFactory()), Range.OutsideBefore);

      // Now the exception range starts just after our new instruction.
      // The next bit of code changes the exception range to point at
      // the store instruction
      for (InstructionTargeter t : start.getTargetersCopy()) {
View Full Code Here

TOP

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

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.