Package org.aspectj.apache.bcel.generic

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


  private InstructionList createLoadInstructions(ResolvedType toType, InstructionFactory fact) {

    InstructionList il = new InstructionList();

    Type jlClass = BcelWorld.makeBcelType(UnresolvedType.JL_CLASS);
    Type jlString = BcelWorld.makeBcelType(UnresolvedType.JL_STRING);
    Type jlClassArray = BcelWorld.makeBcelType(UnresolvedType.JAVA_LANG_CLASS_ARRAY);
    Type jlaAnnotation = BcelWorld.makeBcelType(UnresolvedType.JAVA_LANG_ANNOTATION);

    Instruction pushConstant = fact.createConstant(new ObjectType(toType.getName()));

    if (kind == Shadow.MethodCall || kind == Shadow.MethodExecution || kind == Shadow.PreInitialization
        || kind == Shadow.Initialization || kind == Shadow.ConstructorCall || kind == Shadow.ConstructorExecution
        || kind == Shadow.AdviceExecution ||
        // annotations for fieldset/fieldget when an ITD is involved are stored against a METHOD
        ((kind == Shadow.FieldGet || kind == Shadow.FieldSet) && member.getKind() == Member.METHOD)) {

      Type jlrMethod = BcelWorld.makeBcelType(UnresolvedType.forSignature("Ljava/lang/reflect/Method;"));
      Type jlAnnotation = BcelWorld.makeBcelType(UnresolvedType.forSignature("Ljava/lang/annotation/Annotation;"));
      Type[] paramTypes = BcelWorld.makeBcelTypes(member.getParameterTypes());

      // il.append(fact.createConstant(BcelWorld.makeBcelType(containingType)));

      if (kind == Shadow.MethodCall
          || kind == Shadow.MethodExecution
          || kind == Shadow.AdviceExecution
          ||
          // annotations for fieldset/fieldget when an ITD is involved are stored against a METHOD
          ((kind == Shadow.FieldGet || kind == Shadow.FieldSet) && member.getKind() == Member.METHOD)
          || ((kind == Shadow.ConstructorCall || kind == Shadow.ConstructorExecution) && member.getKind() == Member.METHOD)) {

        // Need to look at the cached annotation before going to fetch it again
        Field annotationCachingField = shadow.getEnclosingClass().getAnnotationCachingField(shadow, toType);

        // Basic idea here is to check if the cached field is null, if it is then initialize it, otherwise use it
        il.append(fact
            .createGetStatic(shadow.getEnclosingClass().getName(), annotationCachingField.getName(), jlAnnotation));
        il.append(InstructionConstants.DUP);
        InstructionBranch ifNonNull = InstructionFactory.createBranchInstruction(Constants.IFNONNULL, null);
        il.append(ifNonNull);
        il.append(InstructionConstants.POP);
        il.append(fact.createConstant(BcelWorld.makeBcelType(containingType)));

        il.append(fact.createConstant(member.getName()));
        buildArray(il, fact, jlClass, paramTypes, 1);
        // OPTIMIZE cache result of getDeclaredMethod?
        il.append(fact.createInvoke("java/lang/Class", "getDeclaredMethod", jlrMethod,
            new Type[] { jlString, jlClassArray }, Constants.INVOKEVIRTUAL));
        il.append(pushConstant);// fact.createConstant(new ObjectType(toType.getName())));
        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);
        Type jlrCtor = BcelWorld.makeBcelType(UnresolvedType.JAVA_LANG_REFLECT_CONSTRUCTOR);
        // OPTIMIZE cache result of getDeclaredConstructor and getAnnotation? Might be able to use it again if someone else
        // needs the same annotations?
        il.append(fact.createInvoke("java/lang/Class", "getDeclaredConstructor", jlrCtor, new Type[] { jlClassArray },
            Constants.INVOKEVIRTUAL));
        il.append(pushConstant);
        il.append(fact.createInvoke("java/lang/reflect/Constructor", "getAnnotation", jlaAnnotation,
            new Type[] { jlClass }, Constants.INVOKEVIRTUAL));
      }
    } else if (kind == Shadow.FieldSet || kind == Shadow.FieldGet) {
      Type jlrField = BcelWorld.makeBcelType(UnresolvedType.JAVA_LANG_REFLECT_FIELD);
      il.append(fact.createConstant(BcelWorld.makeBcelType(containingType))); // Stick the target on the stack
      il.append(fact.createConstant(member.getName())); // Stick what we are after on the stack
      il.append(fact.createInvoke("java/lang/Class", "getDeclaredField", jlrField, new Type[] { jlString },
          Constants.INVOKEVIRTUAL));
      il.append(pushConstant);
View Full Code Here


    } else if (fromType.equals(ResolvedType.VOID)) {
      // assert toType.equals(UnresolvedType.OBJECT)
      il.append(InstructionFactory.createNull(Type.OBJECT));
      return;
    } else if (fromType.equals(UnresolvedType.OBJECT)) {
      Type to = BcelWorld.makeBcelType(toType);
      if (toType.isPrimitiveType()) {
        String name = toType.toString() + "Value";
        il.append(fact.createInvoke("org.aspectj.runtime.internal.Conversions", name, to, new Type[] { Type.OBJECT },
            Constants.INVOKESTATIC));
      } else {
        il.append(fact.createCheckCast((ReferenceType) to));
      }
    } else if (toType.equals(UnresolvedType.OBJECT)) {
      // assert fromType.isPrimitive()
      Type from = BcelWorld.makeBcelType(fromType);
      String name = fromType.toString() + "Object";
      il.append(fact.createInvoke("org.aspectj.runtime.internal.Conversions", name, Type.OBJECT, new Type[] { from },
          Constants.INVOKESTATIC));
    } else if (toType.getWorld().isInJava5Mode() && validBoxing.get(toType.getSignature() + fromType.getSignature()) != null) {
      // XXX could optimize by using any java boxing code that may be just
      // before the call...
      Type from = BcelWorld.makeBcelType(fromType);
      Type to = BcelWorld.makeBcelType(toType);
      String name = validBoxing.get(toType.getSignature() + fromType.getSignature());
      if (toType.isPrimitiveType()) {
        il.append(fact.createInvoke("org.aspectj.runtime.internal.Conversions", name, to, new Type[] { Type.OBJECT },
            Constants.INVOKESTATIC));
      } else {
        il.append(fact.createInvoke("org.aspectj.runtime.internal.Conversions", name, Type.OBJECT, new Type[] { from },
            Constants.INVOKESTATIC));
        il.append(fact.createCheckCast((ReferenceType) to));
      }
    } else if (fromType.isPrimitiveType()) {
      // assert toType.isPrimitive()
      Type from = BcelWorld.makeBcelType(fromType);
      Type to = BcelWorld.makeBcelType(toType);
      try {
        Instruction i = fact.createCast(from, to);
        if (i != null) {
          il.append(i);
        } else {
          il.append(fact.createCast(from, Type.INT));
          il.append(fact.createCast(Type.INT, to));
        }
      } catch (RuntimeException e) {
        il.append(fact.createCast(from, Type.INT));
        il.append(fact.createCast(Type.INT, to));
      }
    } else {
      Type to = BcelWorld.makeBcelType(toType);
      // assert ! fromType.isPrimitive() && ! toType.isPrimitive()
      il.append(fact.createCheckCast((ReferenceType) to));
    }
  }
View Full Code Here

    int len = proceedParamTypes.length;
    BcelVar[] ret = new BcelVar[len];

    for (int i = len - 1; i >= 0; i--) {
      ResolvedType typeX = proceedParamTypes[i];
      Type type = BcelWorld.makeBcelType(typeX);
      int local = enclosingMethod.allocateLocal(type);

      il.append(InstructionFactory.createStore(type, local));
      ret[i] = new BcelVar(typeX, local);
    }
View Full Code Here

        }
      }
      // A field access instruction may tell us the real type of what the clone() call is on
      if (searchPtr.getInstruction() instanceof FieldInstruction) {
        FieldInstruction si = (FieldInstruction) searchPtr.getInstruction();
        Type t = si.getFieldType(getEnclosingClass().getConstantPool());
        return BcelWorld.fromBcel(t);
      }
      // A new array instruction obviously tells us it is an array type !
      if (searchPtr.getInstruction().opcode == Constants.ANEWARRAY) {
        // ANEWARRAY ana = (ANEWARRAY)searchPoint.getInstruction();
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();
View Full Code Here

    Type[] stateTypes = callbackMethod.getArgumentTypes();
    // System.out.println("stateTypes: " + Arrays.asList(stateTypes));

    for (int i = 0, len = stateTypes.length; i < len; i++) {
      Type stateType = stateTypes[i];
      ResolvedType stateTypeX = BcelWorld.fromBcel(stateType).resolve(world);
      if (proceedMap.hasKey(i)) {
        // throw new RuntimeException("unimplemented");
        proceedVars[proceedMap.get(i)].appendLoadAndConvert(ret, fact, stateTypeX);
      } else {
View Full Code Here

    // store the Object[] array on stack if proceed with args
    if (isProceedWithArgs) {

      // STORE the Object[] into a local variable
      Type objectArrayType = Type.OBJECT_ARRAY;
      int theObjectArrayLocalNumber = localAdviceMethod.allocateLocal(objectArrayType);
      ret.append(InstructionFactory.createStore(objectArrayType, theObjectArrayLocalNumber));

      // STORE the ProceedingJoinPoint instance into a local variable
      Type proceedingJpType = Type.getType("Lorg/aspectj/lang/ProceedingJoinPoint;");
      int pjpLocalNumber = localAdviceMethod.allocateLocal(proceedingJpType);
      ret.append(InstructionFactory.createStore(proceedingJpType, pjpLocalNumber));

      // Aim here initially is to determine whether the user will have provided a new
      // this/target in the object array and consume them if they have, leaving us the rest of
      // the arguments to process as regular arguments to the invocation at the original join point

      boolean pointcutBindsThis = bindsThis(munger);
      boolean pointcutBindsTarget = bindsTarget(munger);
      boolean targetIsSameAsThis = getKind().isTargetSameAsThis();

      int nextArgumentToProvideForCallback = 0;

      if (hasThis()) {
        if (!(pointcutBindsTarget && targetIsSameAsThis)) {
          if (pointcutBindsThis) {
            // they have supplied new this as first entry in object array, consume it
            ret.append(InstructionFactory.createLoad(objectArrayType, theObjectArrayLocalNumber));
            ret.append(Utility.createConstant(fact, 0));
            ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
            ret.append(Utility.createConversion(fact, Type.OBJECT, callbackMethod.getArgumentTypes()[0]));
          } else {
            // use local variable 0
            ret.append(InstructionFactory.createALOAD(0));
          }
          nextArgumentToProvideForCallback++;
        }
      }

      if (hasTarget()) {
        if (pointcutBindsTarget) {
          if (getKind().isTargetSameAsThis()) {
            ret.append(InstructionFactory.createLoad(objectArrayType, theObjectArrayLocalNumber));
            ret.append(Utility.createConstant(fact, pointcutBindsThis ? 1 : 0));
            ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
            ret.append(Utility.createConversion(fact, Type.OBJECT, callbackMethod.getArgumentTypes()[0]));
          } else {
            int position = (hasThis()/* && pointcutBindsThis */? 1 : 0);
            ret.append(InstructionFactory.createLoad(objectArrayType, theObjectArrayLocalNumber));
            ret.append(Utility.createConstant(fact, position));
            ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
            ret.append(Utility.createConversion(fact, Type.OBJECT, callbackMethod.getArgumentTypes()[position]));
          }
          nextArgumentToProvideForCallback++;
        } else {
          if (getKind().isTargetSameAsThis()) {
            // ret.append(new ALOAD(0));
          } else {
            ret.append(InstructionFactory.createLoad(localAdviceMethod.getArgumentTypes()[0], hasThis() ? 1 : 0));
            nextArgumentToProvideForCallback++;
          }
        }
      }

      // Where to start in the object array in order to pick up arguments
      int indexIntoObjectArrayForArguments = (pointcutBindsThis ? 1 : 0) + (pointcutBindsTarget ? 1 : 0);

      int len = callbackMethod.getArgumentTypes().length;
      for (int i = nextArgumentToProvideForCallback; i < len; i++) {
        Type stateType = callbackMethod.getArgumentTypes()[i];
        BcelWorld.fromBcel(stateType).resolve(world);
        if ("Lorg/aspectj/lang/JoinPoint;".equals(stateType.getSignature())) {
          ret.append(new InstructionLV(Constants.ALOAD, pjpLocalNumber));
        } else {
          ret.append(InstructionFactory.createLoad(objectArrayType, theObjectArrayLocalNumber));
          ret.append(Utility
              .createConstant(fact, i - nextArgumentToProvideForCallback + indexIntoObjectArrayForArguments));
          ret.append(InstructionFactory.createArrayLoad(Type.OBJECT));
          ret.append(Utility.createConversion(fact, Type.OBJECT, stateType));
        }
      }

    } else {
      Type proceedingJpType = Type.getType("Lorg/aspectj/lang/ProceedingJoinPoint;");
      int localJp = localAdviceMethod.allocateLocal(proceedingJpType);
      ret.append(InstructionFactory.createStore(proceedingJpType, localJp));

      int idx = 0;
      for (int i = 0, len = callbackMethod.getArgumentTypes().length; i < len; i++) {
        Type stateType = callbackMethod.getArgumentTypes()[i];
        /* ResolvedType stateTypeX = */
        BcelWorld.fromBcel(stateType).resolve(world);
        if ("Lorg/aspectj/lang/JoinPoint;".equals(stateType.getSignature())) {
          ret.append(InstructionFactory.createALOAD(localJp));// from localAdvice signature
          // } else if ("Lorg/aspectj/lang/ProceedingJoinPoint;".equals(stateType.getSignature())) {
          // //FIXME ALEX?
          // ret.append(new ALOAD(localJp));// from localAdvice signature
          // // ret.append(fact.createCheckCast(
          // // (ReferenceType) BcelWorld.makeBcelType(stateTypeX)
          // // ));
          // // cast ?
          //
          idx++;
        } else {
          ret.append(InstructionFactory.createLoad(stateType, idx));
          idx += stateType.getSize();
        }
      }
    }

    // do the callback invoke
View Full Code Here

   */

  private LazyMethodGen makeClosureClassAndReturnConstructor(String closureClassName, LazyMethodGen callbackMethod,
      IntMap proceedMap) {
    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();

    for (int i = 0, len = stateTypes.length; i < len; i++) {
      Type stateType = stateTypes[i];
      ResolvedType stateTypeX = BcelWorld.fromBcel(stateType).resolve(world);
      if (proceedMap.hasKey(i)) {
        mbody.append(proceedVar.createConvertableArrayLoad(fact, proceedMap.get(i), stateTypeX));
      } else {
        mbody.append(stateVar.createConvertableArrayLoad(fact, i, stateTypeX));
View Full Code Here

    ConstantPool cpg = cg.getConstantPool();
    Member retval = null;

    if (i.opcode == Constants.ANEWARRAY) {
      // ANEWARRAY arrayInstruction = (ANEWARRAY)i;
      Type ot = i.getType(cpg);
      UnresolvedType ut = fromBcel(ot);
      ut = UnresolvedType.makeArray(ut, 1);
      retval = MemberImpl.method(ut, Modifier.PUBLIC, ResolvedType.VOID, "<init>", new ResolvedType[] { ResolvedType.INT });
    } else if (i instanceof MULTIANEWARRAY) {
      MULTIANEWARRAY arrayInstruction = (MULTIANEWARRAY) i;
      UnresolvedType ut = null;
      short dimensions = arrayInstruction.getDimensions();
      ObjectType ot = arrayInstruction.getLoadClassType(cpg);
      if (ot != null) {
        ut = fromBcel(ot);
        ut = UnresolvedType.makeArray(ut, dimensions);
      } else {
        Type t = arrayInstruction.getType(cpg);
        ut = fromBcel(t);
      }
      ResolvedType[] parms = new ResolvedType[dimensions];
      for (int ii = 0; ii < dimensions; ii++) {
        parms[ii] = ResolvedType.INT;
      }
      retval = MemberImpl.method(ut, Modifier.PUBLIC, ResolvedType.VOID, "<init>", parms);

    } else if (i.opcode == Constants.NEWARRAY) {
      // NEWARRAY arrayInstruction = (NEWARRAY)i;
      Type ot = i.getType();
      UnresolvedType ut = fromBcel(ot);
      retval = MemberImpl.method(ut, Modifier.PUBLIC, ResolvedType.VOID, "<init>", new ResolvedType[] { ResolvedType.INT });
    } else {
      throw new BCException("Cannot create array construction signature for this non-array instruction:" + i);
    }
View Full Code Here

    if ((newflags & 0x00000100) != 0) {
      newflags = newflags - 0x100;// NATIVE = 0x00000100 - need to clear it
    }

    bridgeMethod.setAccessFlags(newflags);
    Type returnType = BcelWorld.makeBcelType(theBridgeMethod.getReturnType());
    Type[] paramTypes = BcelWorld.makeBcelTypes(theBridgeMethod.getParameterTypes());
    Type[] newParamTypes = whatToBridgeToMethodGen.getArgumentTypes();
    body = bridgeMethod.getBody();
    fact = clazz.getFactory();

    if (!whatToBridgeToMethodGen.isStatic()) {
      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 (!newParamTypes[i].equals(paramTypes[i])) {
        if (world.forDEBUG_bridgingCode) {
          System.err.println("Bridging: Cast " + newParamTypes[i] + " from " + paramTypes[i]);
        }
        body.append(fact.createCast(paramTypes[i], newParamTypes[i]));
      }
      pos += paramType.getSize();
    }

    body.append(Utility.createInvoke(fact, world, whatToBridgeTo));
    body.append(InstructionFactory.createReturn(returnType));
    clazz.addMethodGen(bridgeMethod);
View Full Code Here

TOP

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

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.