Package org.aspectj.apache.bcel.generic

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


        // high priority if our args are on the stack
        getKind().hasHighPriorityExceptions());
  }

  public void weavePerObjectEntry(final BcelAdvice munger, final BcelVar onVar) {
    final InstructionFactory fact = getFactory();

    InstructionList entryInstructions = new InstructionList();
    InstructionList entrySuccessInstructions = new InstructionList();
    onVar.appendLoad(entrySuccessInstructions, fact);
View Full Code Here


  public void weavePerTypeWithinAspectInitialization(final BcelAdvice munger, UnresolvedType t) {

    if (t.resolve(world).isInterface()) {
      return; // Don't initialize statics in
    }
    final InstructionFactory fact = getFactory();

    InstructionList entryInstructions = new InstructionList();
    InstructionList entrySuccessInstructions = new InstructionList();

    BcelWorld.getBcelObjectType(munger.getConcreteAspect());
    String aspectname = munger.getConcreteAspect().getName();

    String ptwField = NameMangler.perTypeWithinFieldForTarget(munger.getConcreteAspect());
    entrySuccessInstructions.append(InstructionFactory.PUSH(fact.getConstantPool(), t.getName()));

    entrySuccessInstructions.append(fact.createInvoke(aspectname, "ajc$createAspectInstance", new ObjectType(aspectname),
        new Type[] { new ObjectType("java.lang.String") }, Constants.INVOKESTATIC));
    entrySuccessInstructions.append(fact.createPutStatic(t.getName(), ptwField, new ObjectType(aspectname)));

    entryInstructions.append(entrySuccessInstructions);

    range.insert(entryInstructions, Range.InsideBefore);
  }
View Full Code Here

  public void weaveCflowEntry(final BcelAdvice munger, final Member cflowField) {
    final boolean isPer = munger.getKind() == AdviceKind.PerCflowBelowEntry || munger.getKind() == AdviceKind.PerCflowEntry;

    final Type objectArrayType = new ArrayType(Type.OBJECT, 1);
    final InstructionFactory fact = getFactory();

    final BcelVar testResult = genTempVar(ResolvedType.BOOLEAN);

    InstructionList entryInstructions = new InstructionList();
    {
      InstructionList entrySuccessInstructions = new InstructionList();

      if (munger.hasDynamicTests()) {
        entryInstructions.append(Utility.createConstant(fact, 0));
        testResult.appendStore(entryInstructions, fact);

        entrySuccessInstructions.append(Utility.createConstant(fact, 1));
        testResult.appendStore(entrySuccessInstructions, fact);
      }

      if (isPer) {
        entrySuccessInstructions.append(fact.createInvoke(munger.getConcreteAspect().getName(),
            NameMangler.PERCFLOW_PUSH_METHOD, Type.VOID, new Type[] {}, Constants.INVOKESTATIC));
      } else {
        BcelVar[] cflowStateVars = munger.getExposedStateAsBcelVars(false);

        if (cflowStateVars.length == 0) {
          // This should be getting managed by a counter - lets make sure.
          if (!cflowField.getType().getName().endsWith("CFlowCounter")) {
            throw new RuntimeException("Incorrectly attempting counter operation on stacked cflow");
          }
          entrySuccessInstructions.append(Utility.createGet(fact, cflowField));
          // arrayVar.appendLoad(entrySuccessInstructions, fact);
          entrySuccessInstructions.append(fact.createInvoke(NameMangler.CFLOW_COUNTER_TYPE, "inc", Type.VOID,
              new Type[] {}, Constants.INVOKEVIRTUAL));
        } else {
          BcelVar arrayVar = genTempVar(UnresolvedType.OBJECTARRAY);

          int alen = cflowStateVars.length;
          entrySuccessInstructions.append(Utility.createConstant(fact, alen));
          entrySuccessInstructions.append(fact.createNewArray(Type.OBJECT, (short) 1));
          arrayVar.appendStore(entrySuccessInstructions, fact);

          for (int i = 0; i < alen; i++) {
            arrayVar.appendConvertableArrayStore(entrySuccessInstructions, fact, i, cflowStateVars[i]);
          }

          entrySuccessInstructions.append(Utility.createGet(fact, cflowField));
          arrayVar.appendLoad(entrySuccessInstructions, fact);

          entrySuccessInstructions.append(fact.createInvoke(NameMangler.CFLOW_STACK_TYPE, "push", Type.VOID,
              new Type[] { objectArrayType }, Constants.INVOKEVIRTUAL));
        }
      }

      InstructionList testInstructions = munger.getTestInstructions(this, entrySuccessInstructions.getStart(), range
          .getRealStart(), entrySuccessInstructions.getStart());
      entryInstructions.append(testInstructions);
      entryInstructions.append(entrySuccessInstructions);
    }

    // this is the same for both per and non-per
    weaveAfter(new BcelAdvice(null, null, null, 0, 0, 0, null, munger.getConcreteAspect()) {
      @Override
      public InstructionList getAdviceInstructions(BcelShadow s, BcelVar extraArgVar, InstructionHandle ifNoAdvice) {
        InstructionList exitInstructions = new InstructionList();
        if (munger.hasDynamicTests()) {
          testResult.appendLoad(exitInstructions, fact);
          exitInstructions.append(InstructionFactory.createBranchInstruction(Constants.IFEQ, ifNoAdvice));
        }
        exitInstructions.append(Utility.createGet(fact, cflowField));
        if (munger.getKind() != AdviceKind.PerCflowEntry && munger.getKind() != AdviceKind.PerCflowBelowEntry
            && munger.getExposedStateAsBcelVars(false).length == 0) {
          exitInstructions.append(fact.createInvoke(NameMangler.CFLOW_COUNTER_TYPE, "dec", Type.VOID, new Type[] {},
              Constants.INVOKEVIRTUAL));
        } else {
          exitInstructions.append(fact.createInvoke(NameMangler.CFLOW_STACK_TYPE, "pop", Type.VOID, new Type[] {},
              Constants.INVOKEVIRTUAL));
        }
        return exitInstructions;
      }
    });
View Full Code Here

    IntMap varMap = IntMap.idMap(nVars);
    for (int i = extraParamOffset; i < nVars; i++) {
      varMap.put(i - extraParamOffset, i);
    }

    final InstructionFactory fact = getFactory();

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

    localAdviceMethod.setMaxLocals(nVars);
View Full Code Here

      return node;
    }
  }

  public void weaveAroundClosure(BcelAdvice munger, boolean hasDynamicTest) {
    InstructionFactory fact = getFactory();

    enclosingMethod.setCanInline(false);

    int linenumber = getSourceLine();
    // MOVE OUT ALL THE INSTRUCTIONS IN MY SHADOW INTO ANOTHER METHOD!
    LazyMethodGen callbackMethod = extractShadowInstructionsIntoNewMethod(NameMangler.aroundShadowMethodName(getSignature(),
        getEnclosingClass().getNewGeneratedNameTag()), 0, munger.getSourceLocation(), new ArrayList<String>());

    BcelVar[] adviceVars = munger.getExposedStateAsBcelVars(true);

    String closureClassName = NameMangler.makeClosureClassName(getEnclosingClass().getType(), getEnclosingClass()
        .getNewGeneratedNameTag());

    Member constructorSig = new MemberImpl(Member.CONSTRUCTOR, UnresolvedType.forName(closureClassName), 0, "<init>",
        "([Ljava/lang/Object;)V");

    BcelVar closureHolder = null;

    // This is not being used currently since getKind() == preinitializaiton
    // cannot happen in around advice
    if (getKind() == PreInitialization) {
      closureHolder = genTempVar(AjcMemberMaker.AROUND_CLOSURE_TYPE);
    }

    InstructionList closureInstantiation = makeClosureInstantiation(constructorSig, closureHolder);

    /* LazyMethodGen constructor = */
    makeClosureClassAndReturnConstructor(closureClassName, callbackMethod, makeProceedArgumentMap(adviceVars));

    InstructionList returnConversionCode;
    if (getKind() == PreInitialization) {
      returnConversionCode = new InstructionList();

      BcelVar stateTempVar = genTempVar(UnresolvedType.OBJECTARRAY);
      closureHolder.appendLoad(returnConversionCode, fact);

      returnConversionCode.append(Utility.createInvoke(fact, world, AjcMemberMaker.aroundClosurePreInitializationGetter()));
      stateTempVar.appendStore(returnConversionCode, fact);

      Type[] stateTypes = getSuperConstructorParameterTypes();

      returnConversionCode.append(InstructionConstants.ALOAD_0); // put "this" back on the stack
      for (int i = 0, len = stateTypes.length; i < len; i++) {
        UnresolvedType bcelTX = BcelWorld.fromBcel(stateTypes[i]);
        ResolvedType stateRTX = world.resolve(bcelTX, true);
        if (stateRTX.isMissing()) {
          world.getLint().cantFindType.signal(new String[] { WeaverMessages.format(
              WeaverMessages.CANT_FIND_TYPE_DURING_AROUND_WEAVE_PREINIT, bcelTX.getClassName()) },
              getSourceLocation(), new ISourceLocation[] { munger.getSourceLocation() });
          // IMessage msg = new Message(
          // WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_DURING_AROUND_WEAVE_PREINIT,bcelTX.getClassName()),
          // "",IMessage.ERROR,getSourceLocation(),null,
          // new ISourceLocation[]{ munger.getSourceLocation()});
          // world.getMessageHandler().handleMessage(msg);
        }
        stateTempVar.appendConvertableArrayLoad(returnConversionCode, fact, i, stateRTX);
      }
    } else {
      // pr226201
      Member mungerSignature = munger.getSignature();
      if (munger.getSignature() instanceof ResolvedMember) {
        if (((ResolvedMember) mungerSignature).hasBackingGenericMember()) {
          mungerSignature = ((ResolvedMember) mungerSignature).getBackingGenericMember();
        }
      }
      UnresolvedType returnType = mungerSignature.getReturnType();
      returnConversionCode = Utility.createConversion(getFactory(), BcelWorld.makeBcelType(returnType), callbackMethod
          .getReturnType(), world.isInJava5Mode());
      if (!isFallsThrough()) {
        returnConversionCode.append(InstructionFactory.createReturn(callbackMethod.getReturnType()));
      }
    }

    // initialize the bit flags for this shadow
    int bitflags = 0x000000;
    if (getKind().isTargetSameAsThis()) {
      bitflags |= 0x010000;
    }
    if (hasThis()) {
      bitflags |= 0x001000;
    }
    if (bindsThis(munger)) {
      bitflags |= 0x000100;
    }
    if (hasTarget()) {
      bitflags |= 0x000010;
    }
    if (bindsTarget(munger)) {
      bitflags |= 0x000001;
    }

    // ATAJ for @AJ aspect we need to link the closure with the joinpoint instance
    if (munger.getConcreteAspect() != null && munger.getConcreteAspect().isAnnotationStyleAspect()
        && munger.getDeclaringAspect() != null && munger.getDeclaringAspect().resolve(world).isAnnotationStyleAspect()) {
      // stick the bitflags on the stack and call the variant of linkClosureAndJoinPoint that takes an int
      closureInstantiation.append(fact.createConstant(Integer.valueOf(bitflags)));
      closureInstantiation.append(Utility.createInvoke(getFactory(), getWorld(), new MemberImpl(Member.METHOD, UnresolvedType
          .forName("org.aspectj.runtime.internal.AroundClosure"), Modifier.PUBLIC, "linkClosureAndJoinPoint",
          "(I)Lorg/aspectj/lang/ProceedingJoinPoint;")));
    }

View Full Code Here

    }
  }

  // exposed for testing
  InstructionList makeCallToCallback(LazyMethodGen callbackMethod) {
    InstructionFactory fact = getFactory();
    InstructionList callback = new InstructionList();
    if (thisVar != null) {
      callback.append(InstructionConstants.ALOAD_0);
    }
    if (targetVar != null && targetVar != thisVar) {
View Full Code Here

  /** side-effect-free */
  private InstructionList makeClosureInstantiation(Member constructor, BcelVar holder) {

    // LazyMethodGen constructor) {
    InstructionFactory fact = getFactory();
    BcelVar arrayVar = genTempVar(UnresolvedType.OBJECTARRAY);
    // final Type objectArrayType = new ArrayType(Type.OBJECT, 1);
    final InstructionList il = new InstructionList();
    int alen = getArgCount() + (thisVar == null ? 0 : 1) + ((targetVar != null && targetVar != thisVar) ? 1 : 0)
        + (thisJoinPointVar == null ? 0 : 1);
    il.append(Utility.createConstant(fact, alen));
    il.append(fact.createNewArray(Type.OBJECT, (short) 1));
    arrayVar.appendStore(il, fact);

    int stateIndex = 0;
    if (thisVar != null) {
      arrayVar.appendConvertableArrayStore(il, fact, stateIndex, thisVar);
      thisVar.setPositionInAroundState(stateIndex);
      stateIndex++;
    }
    if (targetVar != null && targetVar != thisVar) {
      arrayVar.appendConvertableArrayStore(il, fact, stateIndex, targetVar);
      targetVar.setPositionInAroundState(stateIndex);
      stateIndex++;
    }
    for (int i = 0, len = getArgCount(); i < len; i++) {
      arrayVar.appendConvertableArrayStore(il, fact, stateIndex, argVars[i]);
      argVars[i].setPositionInAroundState(stateIndex);
      stateIndex++;
    }
    if (thisJoinPointVar != null) {
      arrayVar.appendConvertableArrayStore(il, fact, stateIndex, thisJoinPointVar);
      thisJoinPointVar.setPositionInAroundState(stateIndex);
      stateIndex++;
    }
    il.append(fact.createNew(new ObjectType(constructor.getDeclaringType().getName())));
    il.append(InstructionConstants.DUP);
    arrayVar.appendLoad(il, fact);
    il.append(Utility.createInvoke(fact, world, constructor));
    if (getKind() == PreInitialization) {
      il.append(InstructionConstants.DUP);
View Full Code Here

    String superClassName = "org.aspectj.runtime.internal.AroundClosure";
    Type objectArrayType = new ArrayType(Type.OBJECT, 1);

    LazyClassGen closureClass = new LazyClassGen(closureClassName, superClassName, getEnclosingClass().getFileName(),
        Modifier.PUBLIC, new String[] {}, getWorld());
    InstructionFactory fact = new InstructionFactory(closureClass.getConstantPool());

    // constructor
    LazyMethodGen constructor = new LazyMethodGen(Modifier.PUBLIC, Type.VOID, "<init>", new Type[] { objectArrayType },
        new String[] {}, closureClass);
    InstructionList cbody = constructor.getBody();
    cbody.append(InstructionFactory.createLoad(Type.OBJECT, 0));
    cbody.append(InstructionFactory.createLoad(objectArrayType, 1));
    cbody.append(fact
        .createInvoke(superClassName, "<init>", Type.VOID, new Type[] { objectArrayType }, Constants.INVOKESPECIAL));
    cbody.append(InstructionFactory.createReturn(Type.VOID));

    closureClass.addMethodGen(constructor);

    // method
    LazyMethodGen runMethod = new LazyMethodGen(Modifier.PUBLIC, Type.OBJECT, "run", new Type[] { objectArrayType },
        new String[] {}, closureClass);
    InstructionList mbody = runMethod.getBody();
    BcelVar proceedVar = new BcelVar(UnresolvedType.OBJECTARRAY.resolve(world), 1);
    // int proceedVarIndex = 1;
    BcelVar stateVar = new BcelVar(UnresolvedType.OBJECTARRAY.resolve(world), runMethod.allocateLocal(1));
    // int stateVarIndex = runMethod.allocateLocal(1);
    mbody.append(InstructionFactory.createThis());
    mbody.append(fact.createGetField(superClassName, "state", objectArrayType));
    mbody.append(stateVar.createStore(fact));
    // mbody.append(fact.createStore(objectArrayType, stateVarIndex));

    Type[] stateTypes = callbackMethod.getArgumentTypes();
View Full Code Here

    return newMethod;
  }

  private void addPreInitializationReturnCode(LazyMethodGen extractedMethod, Type[] superConstructorTypes) {
    InstructionList body = extractedMethod.getBody();
    final InstructionFactory fact = getFactory();

    BcelVar arrayVar = new BcelVar(world.getCoreType(UnresolvedType.OBJECTARRAY), extractedMethod.allocateLocal(1));

    int len = superConstructorTypes.length;

    body.append(Utility.createConstant(fact, len));

    body.append(fact.createNewArray(Type.OBJECT, (short) 1));
    arrayVar.appendStore(body, fact);

    for (int i = len - 1; i >= 0; i++) {
      // convert thing on top of stack to object
      body.append(Utility.createConversion(fact, superConstructorTypes[i], Type.OBJECT));
View Full Code Here

   */
  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;
        ResolvedType callee = m_aspectGen.getWorld().resolve(UnresolvedType.forName(invoke.getClassName(cpg)));

        // look in the whole method list and not just declared for super calls and alike
        List<ResolvedMember> methods = callee.getMethodsWithoutIterator(false, true, false);
        for (ResolvedMember resolvedMember : methods) {
          if (invoke.getName(cpg).equals(resolvedMember.getName())
              && invoke.getSignature(cpg).equals(resolvedMember.getSignature()) && !resolvedMember.isPublic()) {
            if ("<init>".equals(invoke.getName(cpg))) {
              // skipping open up for private constructor
              // can occur when aspect new a private inner type
              // too complex to handle new + dup + .. + invokespecial here.
              aroundAdvice.setCanInline(false);
              realizedCannotInline = true;
            } else {
              // specific handling for super.foo() calls, where foo is non public
              ResolvedType memberType = m_aspectGen.getWorld().resolve(resolvedMember.getDeclaringType());
              if (!aspectType.equals(memberType) && memberType.isAssignableFrom(aspectType)) {
                // old test was...
                // if (aspectType.getSuperclass() != null
                // && aspectType.getSuperclass().getName().equals(resolvedMember.getDeclaringType().getName())) {
                ResolvedMember accessor = createOrGetInlineAccessorForSuperDispatch(resolvedMember);
                InvokeInstruction newInst = factory.createInvoke(aspectType.getName(), accessor.getName(),
                    BcelWorld.makeBcelType(accessor.getReturnType()),
                    BcelWorld.makeBcelTypes(accessor.getParameterTypes()), Constants.INVOKEVIRTUAL);
                curr.setInstruction(newInst);
              } else {
                ResolvedMember accessor = createOrGetInlineAccessorForMethod(resolvedMember);
                InvokeInstruction newInst = factory.createInvoke(aspectType.getName(), accessor.getName(),
                    BcelWorld.makeBcelType(accessor.getReturnType()),
                    BcelWorld.makeBcelTypes(accessor.getParameterTypes()), Constants.INVOKESTATIC);
                curr.setInstruction(newInst);
              }
            }

            break;// ok we found a matching callee member and swapped the instruction with the accessor
          }
        }
      } else if (inst instanceof FieldInstruction) {
        FieldInstruction invoke = (FieldInstruction) inst;
        ResolvedType callee = m_aspectGen.getWorld().resolve(UnresolvedType.forName(invoke.getClassName(cpg)));
        for (int i = 0; i < callee.getDeclaredJavaFields().length; i++) {
          ResolvedMember resolvedMember = callee.getDeclaredJavaFields()[i];
          if (invoke.getName(cpg).equals(resolvedMember.getName())
              && invoke.getSignature(cpg).equals(resolvedMember.getSignature()) && !resolvedMember.isPublic()) {
            final ResolvedMember accessor;
            if ((inst.opcode == Constants.GETFIELD) || (inst.opcode == Constants.GETSTATIC)) {
              accessor = createOrGetInlineAccessorForFieldGet(resolvedMember);
            } else {
              accessor = createOrGetInlineAccessorForFieldSet(resolvedMember);
            }
            InvokeInstruction newInst = factory.createInvoke(aspectType.getName(), accessor.getName(),
                BcelWorld.makeBcelType(accessor.getReturnType()),
                BcelWorld.makeBcelTypes(accessor.getParameterTypes()), Constants.INVOKESTATIC);
            curr.setInstruction(newInst);

            break;// ok we found a matching callee member and swapped the instruction with the accessor
View Full Code Here

TOP

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

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.