Package org.aspectj.apache.bcel.generic

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


    // 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

  /**
   * Returns null if this is not a Java constructor, and then we won't weave into it at all
   */
  private InstructionHandle findSuperOrThisCall(LazyMethodGen mg) {
    int depth = 1;
    InstructionHandle start = mg.getBody().getStart();
    while (true) {
      if (start == null) {
        return null;
      }

      Instruction inst = start.getInstruction();
      if (inst.opcode == Constants.INVOKESPECIAL && ((InvokeInstruction) inst).getName(cpg).equals("<init>")) {
        depth--;
        if (depth == 0) {
          return start;
        }
      } else if (inst.opcode == Constants.NEW) {
        depth++;
      }
      start = start.getNext();
    }
  }
View Full Code Here

  }

  private boolean matchInit(LazyMethodGen mg, List<BcelShadow> shadowAccumulator) {
    BcelShadow enclosingShadow;
    // XXX the enclosing join point is wrong for things before ignoreMe.
    InstructionHandle superOrThisCall = findSuperOrThisCall(mg);

    // we don't walk bodies of things where it's a wrong constructor thingie
    if (superOrThisCall == null) {
      return false;
    }

    enclosingShadow = BcelShadow.makeConstructorExecution(world, mg, superOrThisCall);
    if (mg.getEffectiveSignature() != null) {
      enclosingShadow.setMatchingSignature(mg.getEffectiveSignature().getEffectiveSignature());
    }

    // walk the body
    boolean beforeSuperOrThisCall = true;
    if (shouldWeaveBody(mg)) {
      if (canMatchBodyShadows) {
        for (InstructionHandle h = mg.getBody().getStart(); h != null; h = h.getNext()) {
          if (h == superOrThisCall) {
            beforeSuperOrThisCall = false;
            continue;
          }
          match(mg, h, beforeSuperOrThisCall ? null : enclosingShadow, shadowAccumulator);
        }
      }
      if (canMatch(Shadow.ConstructorExecution)) {
        match(enclosingShadow, shadowAccumulator);
      }
    }

    // XXX we don't do pre-inits of interfaces

    // now add interface inits
    if (!isThisCall(superOrThisCall)) {
      InstructionHandle curr = enclosingShadow.getRange().getStart();
      for (Iterator<IfaceInitList> i = addedSuperInitializersAsList.iterator(); i.hasNext();) {
        IfaceInitList l = i.next();

        Member ifaceInitSig = AjcMemberMaker.interfaceConstructor(l.onType);
View Full Code Here

            // split them - adding a
            // NOP before the invokestatic that gets all the
            // targeters
            // that were aimed at the INVOKESTATIC
            mg.getBody().insert(ih, InstructionConstants.NOP);
            InstructionHandle newNOP = ih.getPrev();
            // what about a try..catch that starts at the start
            // of the exception handler? need to only include
            // certain targeters really.
            er.updateTarget(ih, newNOP, mg.getBody());
            for (InstructionTargeter t2 : targeters) {
              newNOP.addTargeter(t2);
            }
            ih.removeAllTargeters();
            match(BcelShadow.makeExceptionHandler(world, er, mg, newNOP, enclosingShadow), shadowAccumulator);
          } else {
            match(BcelShadow.makeExceptionHandler(world, er, mg, ih, enclosingShadow), shadowAccumulator);
          }
        }
      }
    }

    if ((i instanceof FieldInstruction) && (canMatch(Shadow.FieldGet) || canMatch(Shadow.FieldSet))) {
      FieldInstruction fi = (FieldInstruction) i;

      if (fi.opcode == Constants.PUTFIELD || fi.opcode == Constants.PUTSTATIC) {
        // check for sets of constant fields. We first check the
        // previous
        // instruction. If the previous instruction is a LD_WHATEVER
        // (push
        // constant on the stack) then we must resolve the field to
        // determine
        // if it's final. If it is final, then we don't generate a
        // shadow.
        InstructionHandle prevHandle = ih.getPrev();
        Instruction prevI = prevHandle.getInstruction();
        if (Utility.isConstantPushInstruction(prevI)) {
          Member field = BcelWorld.makeFieldJoinPointSignature(clazz, (FieldInstruction) i);
          ResolvedMember resolvedField = field.resolve(world);
          if (resolvedField == null) {
            // we can't find the field, so it's not a join point.
View Full Code Here

  private boolean isInitFailureHandler(InstructionHandle ih) {
    // Skip the astore_0 and aload_0 at the start of the handler and
    // then check if the instruction following these is
    // 'putstatic ajc$initFailureCause'. If it is then we are
    // in the handler we created in AspectClinit.generatePostSyntheticCode()
    InstructionHandle twoInstructionsAway = ih.getNext().getNext();
    if (twoInstructionsAway.getInstruction().opcode == Constants.PUTSTATIC) {
      String name = ((FieldInstruction) twoInstructionsAway.getInstruction()).getFieldName(cpg);
      if (name.equals(NameMangler.INITFAILURECAUSE_FIELD_NAME)) {
        return true;
      }
    }
    return false;
View Full Code Here

  /**
   * Prepare the around advice, flag it as cannot be inlined if it can't be
   */
  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

  }

  // ----

  boolean isEmpty() {
    InstructionHandle ih = start;
    // System.err.println("  looking for " + end);
    while (ih != end) {
      // System.err.println("    ih " + ih);
      if (!Range.isRangeHandle(ih)) {
        return false;
      }
      ih = ih.getNext();
    }
    return true;
  }
View Full Code Here

  // ----

  InstructionHandle insert(Instruction i, Where where) {
    InstructionList il = new InstructionList();
    InstructionHandle ret = il.insert(i);
    insert(il, where);
    return ret;
  }
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.