Package org.aspectj.apache.bcel.generic

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


            mg.addAnnotation(new BcelAnnotation(ag, weaver.getWorld()));
          }
        }
      }

      InstructionList body = new InstructionList();
      InstructionFactory fact = gen.getFactory();

      // getfield
      body.append(InstructionConstants.ALOAD_0);
      body.append(Utility.createGet(fact, munger.getDelegate(weaver.getLazyClassGen().getType())));
      InstructionBranch ifNonNull = InstructionFactory.createBranchInstruction(Constants.IFNONNULL, null);
      body.append(ifNonNull);

      // Create and store a new instance
      body.append(InstructionConstants.ALOAD_0); // 'this' is where we'll store the field value

      // TODO for non-static case, call aspectOf() then call the factory method on the retval
      // TODO decide whether the value can really be cached

      // locate the aspect and call the static method in it
      if (munger.specifiesDelegateFactoryMethod()) {
        ResolvedMember rm = munger.getDelegateFactoryMethod(weaver.getWorld());

        // Check the method parameter is compatible with the type of the instance to be passed
        if (rm.getArity() != 0) {
          ResolvedType parameterType = rm.getParameterTypes()[0].resolve(weaver.getWorld());
          if (!parameterType.isAssignableFrom(weaver.getLazyClassGen().getType())) {
            signalError("For mixin factory method '" + rm + "': Instance type '" + weaver.getLazyClassGen().getType()
                + "' is not compatible with factory parameter type '" + parameterType + "'", weaver);
            return false;
          }
        }
        if (Modifier.isStatic(rm.getModifiers())) {
          if (rm.getArity() != 0) {
            body.append(InstructionConstants.ALOAD_0);
          }
          body.append(fact.createInvoke(rm.getDeclaringType().getName(), rm.getName(), rm.getSignature(),
              Constants.INVOKESTATIC));
          body.append(Utility.createSet(fact, munger.getDelegate(weaver.getLazyClassGen().getType())));
        } else {
          // Need to call aspectOf() to obtain the aspect instance then call the factory method upon that
          UnresolvedType theAspect = munger.getAspect();
          body.append(fact.createInvoke(theAspect.getName(), "aspectOf", "()" + theAspect.getSignature(),
              Constants.INVOKESTATIC));
          if (rm.getArity() != 0) {
            body.append(InstructionConstants.ALOAD_0);
          }
          body.append(fact.createInvoke(rm.getDeclaringType().getName(), rm.getName(), rm.getSignature(),
              Constants.INVOKEVIRTUAL));
          body.append(Utility.createSet(fact, munger.getDelegate(weaver.getLazyClassGen().getType())));
        }
      } else {
        body.append(fact.createNew(munger.getImplClassName()));
        body.append(InstructionConstants.DUP);
        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;
      if (!Modifier.isStatic(introduced.getModifiers())) { // skip 'this' (?? can this really
        // happen)
        // body.append(InstructionFactory.createThis());
        pos++;
      }
      Type[] paramTypes = BcelWorld.makeBcelTypes(introduced.getParameterTypes());
      for (int i = 0, len = paramTypes.length; i < len; i++) {
        Type paramType = paramTypes[i];
        body.append(InstructionFactory.createLoad(paramType, pos));
        pos += paramType.getSize();
      }
      body.append(Utility.createInvoke(fact, Constants.INVOKEINTERFACE, introduced));
      body.append(InstructionFactory.createReturn(bcelReturnType));

      mg.getBody().append(body);
      weaver.addLazyMethodGen(mg);
      weaver.getLazyClassGen().warnOnAddedMethod(mg.getMethod(), getSignature().getSourceLocation());
      return true;
View Full Code Here


    }

    currentClass.addMethodGen(mg);
    // weaver.addLazyMethodGen(freshConstructor);

    InstructionList body = mg.getBody();

    // add to body: push arts for call to pre, from actual args starting at
    // 1 (skipping this), going to
    // declared argcount + 1
    UnresolvedType[] declaredParams = newConstructorTypeMunger.getSignature().getParameterTypes();
    Type[] paramTypes = mg.getArgumentTypes();
    int frameIndex = 1;
    for (int i = 0, len = declaredParams.length; i < len; i++) {
      body.append(InstructionFactory.createLoad(paramTypes[i], frameIndex));
      frameIndex += paramTypes[i].getSize();
    }
    // do call to pre
    Member preMethod = AjcMemberMaker.preIntroducedConstructor(aspectType, onType, declaredParams);
    body.append(Utility.createInvoke(fact, null, preMethod));

    // create a local, and store return pre stuff into it.
    int arraySlot = mg.allocateLocal(1);
    body.append(InstructionFactory.createStore(Type.OBJECT, arraySlot));

    // put this on the stack
    body.append(InstructionConstants.ALOAD_0);

    // unpack pre args onto stack
    UnresolvedType[] superParamTypes = explicitConstructor.getParameterTypes();

    for (int i = 0, len = superParamTypes.length; i < len; i++) {
      body.append(InstructionFactory.createLoad(Type.OBJECT, arraySlot));
      body.append(Utility.createConstant(fact, i));
      body.append(InstructionFactory.createArrayLoad(Type.OBJECT));
      body.append(Utility.createConversion(fact, Type.OBJECT, BcelWorld.makeBcelType(superParamTypes[i])));
    }

    // call super/this

    body.append(Utility.createInvoke(fact, null, explicitConstructor));

    // put this back on the stack

    body.append(InstructionConstants.ALOAD_0);

    // unpack params onto stack
    Member postMethod = AjcMemberMaker.postIntroducedConstructor(aspectType, onType, declaredParams);
    UnresolvedType[] postParamTypes = postMethod.getParameterTypes();

    for (int i = 1, len = postParamTypes.length; i < len; i++) {
      body.append(InstructionFactory.createLoad(Type.OBJECT, arraySlot));
      body.append(Utility.createConstant(fact, superParamTypes.length + i - 1));
      body.append(InstructionFactory.createArrayLoad(Type.OBJECT));
      body.append(Utility.createConversion(fact, Type.OBJECT, BcelWorld.makeBcelType(postParamTypes[i])));
    }

    // call post
    body.append(Utility.createInvoke(fact, null, postMethod));

    // don't forget to return!!
    body.append(InstructionConstants.RETURN);

    return true;
  }
View Full Code Here

      modifiers |= Modifier.ABSTRACT;
    }

    LazyMethodGen mg = new LazyMethodGen(modifiers, returnType, dispatchName, paramTypes, UnresolvedType.getNames(superMethod
        .getExceptions()), onGen);
    InstructionList body = mg.getBody();

    if (onGen.isInterface()) {
      return mg;
    }

    // assert (!superMethod.isStatic())
    InstructionFactory fact = onGen.getFactory();
    int pos = 0;

    body.append(InstructionFactory.createThis());
    pos++;
    for (int i = 0, len = paramTypes.length; i < len; i++) {
      Type paramType = paramTypes[i];
      body.append(InstructionFactory.createLoad(paramType, pos));
      pos += paramType.getSize();
    }
    if (isSuper) {
      body.append(Utility.createSuperInvoke(fact, world, superMethod));
    } else {
      body.append(Utility.createInvoke(fact, world, superMethod));
    }
    body.append(InstructionFactory.createReturn(returnType));

    return mg;
  }
View Full Code Here

      // weaver.getShadowMungers().add(makeInitCallShadowMunger(initMethod)
      // );

      ResolvedMember itdfieldGetter = AjcMemberMaker.interFieldInterfaceGetter(field, gen.getType()/* onType */, aspectType);
      LazyMethodGen mg = makeMethodGen(gen, itdfieldGetter);
      InstructionList il = new InstructionList();
      InstructionFactory fact = gen.getFactory();
      if (Modifier.isStatic(field.getModifiers())) {
        il.append(fact.createFieldAccess(gen.getClassName(), fg.getName(), fieldType, Constants.GETSTATIC));
      } else {
        il.append(InstructionConstants.ALOAD_0);
        il.append(fact.createFieldAccess(gen.getClassName(), fg.getName(), fieldType, Constants.GETFIELD));
      }
      il.append(InstructionFactory.createReturn(fieldType));
      mg.getBody().insert(il);

      gen.addMethodGen(mg);

      // Check if we need bridge methods for the field getter and setter
      if (munger.getDeclaredSignature() != null) { // is this munger a
        // parameterized
        // form of some
        // original munger?
        ResolvedMember toBridgeTo = munger.getDeclaredSignature().parameterizedWith(null,
            munger.getSignature().getDeclaringType().resolve(getWorld()), false, munger.getTypeVariableAliases());
        boolean needsbridging = false;
        if (!toBridgeTo.getReturnType().getErasureSignature()
            .equals(munger.getSignature().getReturnType().getErasureSignature())) {
          needsbridging = true;
        }
        if (needsbridging) {
          ResolvedMember bridgingGetter = AjcMemberMaker.interFieldInterfaceGetter(toBridgeTo, gen.getType(), aspectType);
          createBridgeMethodForITDF(weaver, gen, itdfieldGetter, bridgingGetter);
        }
      }

      ResolvedMember itdfieldSetter = AjcMemberMaker.interFieldInterfaceSetter(field, gen.getType(), aspectType);
      LazyMethodGen mg1 = makeMethodGen(gen, itdfieldSetter);
      InstructionList il1 = new InstructionList();
      if (Modifier.isStatic(field.getModifiers())) {
        il1.append(InstructionFactory.createLoad(fieldType, 0));
        il1.append(fact.createFieldAccess(gen.getClassName(), fg.getName(), fieldType, Constants.PUTSTATIC));
      } else {
        il1.append(InstructionConstants.ALOAD_0);
        il1.append(InstructionFactory.createLoad(fieldType, 1));
        il1.append(fact.createFieldAccess(gen.getClassName(), fg.getName(), fieldType, Constants.PUTFIELD));
      }
      il1.append(InstructionFactory.createReturn(Type.VOID));
      mg1.getBody().insert(il1);

      gen.addMethodGen(mg1);

      if (munger.getDeclaredSignature() != null) {
View Full Code Here

    LazyMethodGen bridgeMethod = makeMethodGen(gen, bridgingSetter);
    bridgeMethod.setAccessFlags(bridgeMethod.getAccessFlags() | 0x00000040); // BRIDGE = 0x00000040
    Type[] paramTypes = BcelWorld.makeBcelTypes(bridgingSetter.getParameterTypes());
    Type[] bridgingToParms = BcelWorld.makeBcelTypes(itdfieldSetter.getParameterTypes());
    Type returnType = BcelWorld.makeBcelType(bridgingSetter.getReturnType());
    InstructionList body = bridgeMethod.getBody();
    fact = gen.getFactory();
    int pos = 0;

    if (!Modifier.isStatic(bridgingSetter.getModifiers())) {
      body.append(InstructionFactory.createThis());
      pos++;
    }
    for (int i = 0, len = paramTypes.length; i < len; i++) {
      Type paramType = paramTypes[i];
      body.append(InstructionFactory.createLoad(paramType, pos));
      if (!bridgingSetter.getParameterTypes()[i].getErasureSignature().equals(
          itdfieldSetter.getParameterTypes()[i].getErasureSignature())) {
        body.append(fact.createCast(paramType, bridgingToParms[i]));
      }
      pos += paramType.getSize();
    }

    body.append(Utility.createInvoke(fact, weaver.getWorld(), itdfieldSetter));
    body.append(InstructionFactory.createReturn(returnType));
    gen.addMethodGen(bridgeMethod);
  }
View Full Code Here

  private BcelRenderer(InstructionFactory fact, BcelWorld world) {
    super();
    this.fact = fact;
    this.world = world;
    this.instructions = new InstructionList();
  }
View Full Code Here

  }

  public static InstructionList renderExpr(InstructionFactory fact, BcelWorld world, Expr e, Type desiredType) {
    BcelRenderer renderer = new BcelRenderer(fact, world);
    e.accept(renderer);
    InstructionList il = renderer.instructions;
    il.append(Utility.createConversion(fact, BcelWorld.makeBcelType(e.getType()), desiredType));
    return il;
  }
View Full Code Here

    // load var onto the stack (done for us later)
    // invokevirtual java/lang/Object.getClass:()Ljava/lang/Class
    // ldc_w annotationClass
    // invokevirtual java/lang/Class.isAnnotationPresent:(Ljava/lang/Class;)Z
    InstructionList il = new InstructionList();

    // If it is null jump past the advice call
    il.append(InstructionFactory.createBranchInstruction(Constants.IFNULL, fk));

    // Load up the var again
    il.append(((BcelVar) hasAnnotation.getVar()).createLoad(fact));

    Member getClass = MemberImpl.method(UnresolvedType.OBJECT, 0, UnresolvedType.JL_CLASS, "getClass",
        UnresolvedType.NONE);
    il.append(Utility.createInvoke(fact, world, getClass));
    // aload annotationClass
    il.append(fact.createConstant(new ObjectType(hasAnnotation.getAnnotationType().getName())));
    // int annClassIndex = fact.getConstantPool().addClass(hasAnnotation.getAnnotationType().getSignature());
    // il.append(new LDC_W(annClassIndex));
    Member isAnnotationPresent = MemberImpl.method(UnresolvedType.JL_CLASS, 0, ResolvedType.BOOLEAN,
        "isAnnotationPresent", new UnresolvedType[] { UnresolvedType.JL_CLASS });
    il.append(Utility.createInvoke(fact, world, isAnnotationPresent));
    il.append(createJumpBasedOnBooleanOnStack());
    instructions.insert(il);
    hasAnnotation.getVar().accept(this);
  }
View Full Code Here

  public void visit(MatchingContextBasedTest matchingContextTest) {
    throw new UnsupportedOperationException("matching context extension not supported in bytecode weaving");
  }

  private InstructionList createJumpBasedOnBooleanOnStack() {
    InstructionList il = new InstructionList();
    if (sk == fk) {
      // don't bother generating if it doesn't matter
      if (sk != next) {
        il.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, sk));
      }
      return il;
    }

    if (fk == next) {
      il.insert(InstructionFactory.createBranchInstruction(Constants.IFNE, sk));
    } else if (sk == next) {
      il.insert(InstructionFactory.createBranchInstruction(Constants.IFEQ, fk));
    } else {
      il.insert(InstructionFactory.createBranchInstruction(Constants.GOTO, sk));
      il.insert(InstructionFactory.createBranchInstruction(Constants.IFEQ, fk));
    }
    return il;
  }
View Full Code Here

  public void visit(Call call) {
    Member method = call.getMethod();
    // assert method.isStatic()
    Expr[] args = call.getArgs();
    // System.out.println("args: " + Arrays.asList(args));
    InstructionList callIl = new InstructionList();
    for (int i = 0, len = args.length; i < len; i++) {
      // XXX only correct for static method calls
      Type desiredType = BcelWorld.makeBcelType(method.getParameterTypes()[i]);
      Expr arg = args[i];
      // if arg is null it is because we couldn't bind it properly, for example see 162135
      if (arg == null) {
        InstructionList iList = new InstructionList();
        iList.append(InstructionFactory.createNull(desiredType));
        callIl.append(iList);
      } else {
        callIl.append(renderExpr(fact, world, arg, desiredType));
      }
    }
View Full Code Here

TOP

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

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.