Package org.aspectj.apache.bcel.generic

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


  InstructionList getAdviceInstructions(BcelShadow s, BcelVar extraArgVar, InstructionHandle ifNoAdvice) {
    BcelShadow shadow = s;
    InstructionFactory fact = shadow.getFactory();
    BcelWorld world = shadow.getWorld();

    InstructionList il = new InstructionList();

    // we test to see if we have the right kind of thing...
    // after throwing does this just by the exception mechanism.
    if (hasExtraParameter() && getKind() == AdviceKind.AfterReturning) {
      UnresolvedType extraParameterType = getExtraParameterType();
      if (!extraParameterType.equals(UnresolvedType.OBJECT) && !extraParameterType.isPrimitiveType()) {
        il.append(BcelRenderer.renderTest(fact, world,
            Test.makeInstanceof(extraArgVar, getExtraParameterType().resolve(world)), null, ifNoAdvice, null));
      }
    }
    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();
View Full Code Here


    List<LazyMethodGen> mgs = newParentTarget.getMethodGens();

    // 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)
View Full Code Here

    return false;
  }

  private void addFieldGetter(LazyClassGen gen, ResolvedMember field, ResolvedMember accessMethod) {
    LazyMethodGen mg = makeMethodGen(gen, accessMethod);
    InstructionList il = new InstructionList();
    InstructionFactory fact = gen.getFactory();
    if (Modifier.isStatic(field.getModifiers())) {
      il.append(fact.createFieldAccess(gen.getClassName(), field.getName(), BcelWorld.makeBcelType(field.getType()),
          Constants.GETSTATIC));
    } else {
      il.append(InstructionConstants.ALOAD_0);
      il.append(fact.createFieldAccess(gen.getClassName(), field.getName(), BcelWorld.makeBcelType(field.getType()),
          Constants.GETFIELD));
    }
    il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(field.getType())));
    mg.getBody().insert(il);

    gen.addMethodGen(mg, getSignature().getSourceLocation());
  }
View Full Code Here

    gen.addMethodGen(mg, getSignature().getSourceLocation());
  }

  private void addFieldSetter(LazyClassGen gen, ResolvedMember field, ResolvedMember accessMethod) {
    LazyMethodGen mg = makeMethodGen(gen, accessMethod);
    InstructionList il = new InstructionList();
    InstructionFactory fact = gen.getFactory();
    Type fieldType = BcelWorld.makeBcelType(field.getType());

    if (Modifier.isStatic(field.getModifiers())) {
      il.append(InstructionFactory.createLoad(fieldType, 0));
      il.append(fact.createFieldAccess(gen.getClassName(), field.getName(), fieldType, Constants.PUTSTATIC));
    } else {
      il.append(InstructionConstants.ALOAD_0);
      il.append(InstructionFactory.createLoad(fieldType, 1));
      il.append(fact.createFieldAccess(gen.getClassName(), field.getName(), fieldType, Constants.PUTFIELD));
    }
    il.append(InstructionFactory.createReturn(Type.VOID));
    mg.getBody().insert(il);

    gen.addMethodGen(mg, getSignature().getSourceLocation());
  }
View Full Code Here

    gen.addMethodGen(mg, getSignature().getSourceLocation());
  }

  private void addMethodDispatch(LazyClassGen gen, ResolvedMember method, ResolvedMember accessMethod) {
    LazyMethodGen mg = makeMethodGen(gen, accessMethod);
    InstructionList il = new InstructionList();
    InstructionFactory fact = gen.getFactory();
    // Type fieldType = BcelWorld.makeBcelType(field.getType());
    Type[] paramTypes = BcelWorld.makeBcelTypes(method.getParameterTypes());

    int pos = 0;

    if (!Modifier.isStatic(method.getModifiers())) {
      il.append(InstructionConstants.ALOAD_0);
      pos++;
    }
    for (int i = 0, len = paramTypes.length; i < len; i++) {
      Type paramType = paramTypes[i];
      il.append(InstructionFactory.createLoad(paramType, pos));
      pos += paramType.getSize();
    }
    il.append(Utility.createInvoke(fact, (BcelWorld) aspectType.getWorld(), method));
    il.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(method.getReturnType())));

    mg.getBody().insert(il);

    gen.addMethodGen(mg);
  }
View Full Code Here

      gen.addField(fg, getSourceLocation());

      Type fieldType = BcelWorld.makeBcelType(aspectType);
      LazyMethodGen mg = new LazyMethodGen(Modifier.PUBLIC, fieldType, NameMangler.perObjectInterfaceGet(aspectType),
          new Type[0], new String[0], gen);
      InstructionList il = new InstructionList();
      InstructionFactory fact = gen.getFactory();
      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);

      LazyMethodGen mg1 = new LazyMethodGen(Modifier.PUBLIC, Type.VOID, NameMangler.perObjectInterfaceSet(aspectType),

      new Type[] { fieldType, }, new String[0], gen);
      InstructionList il1 = new InstructionList();
      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);

      gen.addInterface(munger.getInterfaceType().resolve(weaver.getWorld()), getSourceLocation());
View Full Code Here

    // e.g.
    // "public com_blah_SecurityAspect ajc$com_blah_SecurityAspect$localAspectOf()"
    Type fieldType = BcelWorld.makeBcelType(aspectType);
    LazyMethodGen mg = new LazyMethodGen(Modifier.PUBLIC | Modifier.STATIC, fieldType,
        NameMangler.perTypeWithinLocalAspectOf(aspectType), new Type[0], new String[0], gen);
    InstructionList il = new InstructionList();
    // PTWIMPL ?? Should check if it is null and throw
    // NoAspectBoundException
    InstructionFactory fact = gen.getFactory();
    il.append(fact.createFieldAccess(gen.getClassName(), fg.getName(), fieldType, Constants.GETSTATIC));
    il.append(InstructionFactory.createReturn(fieldType));
    mg.getBody().insert(il);
    gen.addMethodGen(mg);
    return true;
    // } else {
    // return false;
View Full Code Here

      }

      // If it doesn't target an interface and there is a body (i.e. it
      // isnt abstract)
      if (!onInterface && !Modifier.isAbstract(mangledInterMethod.getModifiers())) {
        InstructionList body = newMethod.getBody();
        InstructionFactory fact = classGen.getFactory();
        int pos = 0;

        if (!Modifier.isStatic(unMangledInterMethod.getModifiers())) {
          body.append(InstructionFactory.createThis());
          pos++;
        }
        Type[] paramTypes = BcelWorld.makeBcelTypes(mangledInterMethod.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, classWeaver.getWorld(), interMethodBody));
        body.append(InstructionFactory.createReturn(BcelWorld.makeBcelType(mangledInterMethod.getReturnType())));

        if (classWeaver.getWorld().isInJava5Mode()) { // Don't need bridge
          // methods if not in
          // 1.5 mode.
          createAnyBridgeMethodsForCovariance(classWeaver, munger, unMangledInterMethod, onType, classGen, paramTypes);
        }

      } else {
        // ??? this is okay
        // if (!(mg.getBody() == null)) throw new
        // RuntimeException("bas");
      }

      if (world.isInJava5Mode()) {
        String basicSignature = mangledInterMethod.getSignature();
        String genericSignature = ((ResolvedMemberImpl) mangledInterMethod).getSignatureForAttribute();
        if (!basicSignature.equals(genericSignature)) {
          // Add a signature attribute to it
          newMethod.addAttribute(createSignatureAttribute(classGen.getConstantPool(), genericSignature));
        }
      }
      // XXX make sure to check that we set exceptions properly on this
      // guy.
      classWeaver.addLazyMethodGen(newMethod);
      classWeaver.getLazyClassGen().warnOnAddedMethod(newMethod.getMethod(), getSignature().getSourceLocation());

      addNeededSuperCallMethods(classWeaver, onType, munger.getSuperMethodsCalled());

      return true;

    } else if (onInterface && !Modifier.isAbstract(unMangledInterMethod.getModifiers())) {

      // This means the 'gen' should be the top most implementor
      // - if it is *not* then something went wrong after we worked
      // out that it was the top most implementor (see pr49657)
      if (!classGen.getType().isTopmostImplementor(onType)) {
        ResolvedType rtx = classGen.getType().getTopmostImplementor(onType);
        if (rtx == null) {
          // pr302460
          // null means there is something wrong with what we are looking at
          ResolvedType rt = classGen.getType();
          if (rt.isInterface()) {
            ISourceLocation sloc = munger.getSourceLocation();
            classWeaver
                .getWorld()
                .getMessageHandler()
                .handleMessage(
                    MessageUtil.error(
                        "ITD target "
                            + rt.getName()
                            + " is an interface but has been incorrectly determined to be the topmost implementor of "
                            + onType.getName() + ". ITD is " + this.getSignature(), sloc));
          }
          if (!onType.isAssignableFrom(rt)) {
            ISourceLocation sloc = munger.getSourceLocation();
            classWeaver
                .getWorld()
                .getMessageHandler()
                .handleMessage(
                    MessageUtil.error(
                        "ITD target " + rt.getName() + " doesn't appear to implement " + onType.getName()
                            + " why did we consider it the top most implementor? ITD is "
                            + this.getSignature(), sloc));
          }
        } else if (!rtx.isExposedToWeaver()) {
          ISourceLocation sLoc = munger.getSourceLocation();
          classWeaver
              .getWorld()
              .getMessageHandler()
              .handleMessage(
                  MessageUtil.error(WeaverMessages.format(WeaverMessages.ITD_NON_EXPOSED_IMPLEMENTOR, rtx,
                      getAspectType().getName()), (sLoc == null ? getAspectType().getSourceLocation() : sLoc)));
        } else {
          // XXX what does this state mean?
          // We have incorrectly identified what is the top most
          // implementor and its not because
          // a type wasn't exposed to the weaver
        }
        return false;
      } else {

        ResolvedMember mangledInterMethod = AjcMemberMaker.interMethod(unMangledInterMethod, aspectType, false);

        LazyMethodGen mg = makeMethodGen(classGen, mangledInterMethod);

        // From 98901#29 - need to copy annotations across
        if (classWeaver.getWorld().isInJava5Mode()) {
          AnnotationAJ annotationsOnRealMember[] = null;
          ResolvedType toLookOn = aspectType;
          if (aspectType.isRawType()) {
            toLookOn = aspectType.getGenericType();
          }
          ResolvedMember realMember = getRealMemberForITDFromAspect(toLookOn, memberHoldingAnyAnnotations, false);
          if (realMember == null) {
            throw new BCException("Couldn't find ITD holder member '" + memberHoldingAnyAnnotations + "' on aspect "
                + aspectType);
          }
          annotationsOnRealMember = realMember.getAnnotations();

          if (annotationsOnRealMember != null) {
            for (AnnotationAJ annotationX : annotationsOnRealMember) {
              AnnotationGen a = ((BcelAnnotation) annotationX).getBcelAnnotation();
              AnnotationGen ag = new AnnotationGen(a, classWeaver.getLazyClassGen().getConstantPool(), true);
              mg.addAnnotation(new BcelAnnotation(ag, classWeaver.getWorld()));
            }
          }

          copyOverParameterAnnotations(mg, realMember);
        }

        if (mungingInterface) {
          // we want the modifiers of the ITD to be used for all
          // *implementors* of the
          // interface, but the method itself we add to the interface
          // must be public abstract
          mg.setAccessFlags(Modifier.PUBLIC | Modifier.ABSTRACT);
        }

        Type[] paramTypes = BcelWorld.makeBcelTypes(mangledInterMethod.getParameterTypes());
        Type returnType = BcelWorld.makeBcelType(mangledInterMethod.getReturnType());

        InstructionList body = mg.getBody();
        InstructionFactory fact = classGen.getFactory();
        int pos = 0;

        if (!Modifier.isStatic(mangledInterMethod.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));
          pos += paramType.getSize();
        }

        body.append(Utility.createInvoke(fact, classWeaver.getWorld(), interMethodBody));
        Type t = BcelWorld.makeBcelType(interMethodBody.getReturnType());
        if (!t.equals(returnType)) {
          body.append(fact.createCast(t, returnType));
        }
        body.append(InstructionFactory.createReturn(returnType));
        mg.definingType = onType;

        if (world.isInJava5Mode()) {
          String basicSignature = mangledInterMethod.getSignature();
          String genericSignature = ((ResolvedMemberImpl) mangledInterMethod).getSignatureForAttribute();
View Full Code Here

  private void createBridge(BcelClassWeaver weaver, ResolvedMember unMangledInterMethod, LazyClassGen classGen,
      ResolvedMember toBridgeTo) {
    Type[] paramTypes;
    Type returnType;
    InstructionList body;
    InstructionFactory fact;
    int pos;
    ResolvedMember bridgerMethod = AjcMemberMaker.bridgerToInterMethod(unMangledInterMethod, classGen.getType());
    ResolvedMember bridgingSetter = AjcMemberMaker.interMethodBridger(toBridgeTo, aspectType, false); // pr250493

    LazyMethodGen bridgeMethod = makeMethodGen(classGen, bridgingSetter);
    paramTypes = BcelWorld.makeBcelTypes(bridgingSetter.getParameterTypes());
    Type[] bridgingToParms = BcelWorld.makeBcelTypes(unMangledInterMethod.getParameterTypes());
    returnType = BcelWorld.makeBcelType(bridgingSetter.getReturnType());
    body = bridgeMethod.getBody();
    fact = classGen.getFactory();
    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(
          unMangledInterMethod.getParameterTypes()[i].getErasureSignature())) {
        // System.err.println("Putting in cast from "+
        // paramType+" to "+bridgingToParms[i]);
        body.append(fact.createCast(paramType, bridgingToParms[i]));
      }
      pos += paramType.getSize();
    }

    body.append(Utility.createInvoke(fact, weaver.getWorld(), bridgerMethod));
    body.append(InstructionFactory.createReturn(returnType));
    classGen.addMethodGen(bridgeMethod);
    // mg.definingType = onType;
  }
View Full Code Here

   *        already created them.
   * @param theBridgeMethod
   */
  private void createBridgeMethod(BcelWorld world, NewMethodTypeMunger munger, ResolvedMember unMangledInterMethod,
      LazyClassGen clazz, Type[] paramTypes, ResolvedMember theBridgeMethod) {
    InstructionList body;
    InstructionFactory fact;
    int pos = 0;

    LazyMethodGen bridgeMethod = makeMethodGen(clazz, theBridgeMethod); // The
    // bridge
    // method
    // in
    // this
    // type
    // will
    // have
    // the
    // same
    // signature
    // as
    // the
    // one
    // in
    // the
    // supertype
    bridgeMethod.setAccessFlags(bridgeMethod.getAccessFlags() | 0x00000040 /*
                                         * BRIDGE = 0x00000040
                                         */);
    // UnresolvedType[] newParams =
    // munger.getSignature().getParameterTypes();
    Type returnType = BcelWorld.makeBcelType(theBridgeMethod.getReturnType());
    body = bridgeMethod.getBody();
    fact = clazz.getFactory();

    if (!Modifier.isStatic(unMangledInterMethod.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
      // (unMangledInterMethod.getParameterTypes()[i].getErasureSignature
      // ())) {
      // System.err.println("Putting in cast from "+paramType+" to "+
      // bridgingToParms[i]);
      // body.append(fact.createCast(paramType,bridgingToParms[i]));
      // }
      pos += paramType.getSize();
    }

    body.append(Utility.createInvoke(fact, world, unMangledInterMethod));
    body.append(InstructionFactory.createReturn(returnType));
    clazz.addMethodGen(bridgeMethod);
  }
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.