Package org.aspectj.weaver

Examples of org.aspectj.weaver.ResolvedMember


  private boolean mungeNewMethod(BcelClassWeaver classWeaver, NewMethodTypeMunger munger) {
    World world = classWeaver.getWorld();

    // Resolving it will sort out the tvars
    ResolvedMember unMangledInterMethod = munger.getSignature().resolve(world);

    // do matching on the unMangled one, but actually add them to the mangled method
    ResolvedMember interMethodBody = munger.getDeclaredInterMethodBody(aspectType, world);
    ResolvedMember interMethodDispatcher = munger.getDeclaredInterMethodDispatcher(aspectType, world);
    ResolvedMember memberHoldingAnyAnnotations = interMethodDispatcher;
    LazyClassGen classGen = classWeaver.getLazyClassGen();

    ResolvedType onType = world.resolve(unMangledInterMethod.getDeclaringType(), munger.getSourceLocation());
    if (onType.isRawType()) {
      onType = onType.getGenericType();
    }

    // Simple checks, can't ITD on annotations or enums
    if (onType.isAnnotation()) {
      signalError(WeaverMessages.ITDM_ON_ANNOTATION_NOT_ALLOWED, classWeaver, onType);
      return false;
    }

    if (onType.isEnum()) {
      signalError(WeaverMessages.ITDM_ON_ENUM_NOT_ALLOWED, classWeaver, onType);
      return false;
    }

    boolean mungingInterface = classGen.isInterface();
    boolean onInterface = onType.isInterface();

    if (onInterface
        && classGen.getLazyMethodGen(unMangledInterMethod.getName(), unMangledInterMethod.getSignature(), true) != null) {
      // this is ok, we could be providing the default implementation of a
      // method
      // that the target has already declared
      return false;
    }

    // If we are processing the intended ITD target type (might be an interface)
    if (onType.equals(classGen.getType())) {
      ResolvedMember mangledInterMethod = AjcMemberMaker.interMethod(unMangledInterMethod, aspectType, onInterface);

      LazyMethodGen newMethod = makeMethodGen(classGen, mangledInterMethod);
      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
        newMethod.setAccessFlags(Modifier.PUBLIC | Modifier.ABSTRACT);
      }

      // pr98901
      // For copying the annotations across, we have to discover the real
      // member in the aspect which is holding them.
      if (classWeaver.getWorld().isInJava5Mode()) {
        AnnotationAJ annotationsOnRealMember[] = null;
        ResolvedType toLookOn = aspectType;
        if (aspectType.isRawType()) {
          toLookOn = aspectType.getGenericType();
        }
        ResolvedMember realMember = getRealMemberForITDFromAspect(toLookOn, memberHoldingAnyAnnotations, false);
        // 266602 - consider it missing to mean that the corresponding aspect had errors
        if (realMember == null) {
          // signalWarning("Unable to apply any annotations attached to " + munger.getSignature(), weaver);
          // throw new BCException("Couldn't find ITD init member '" + interMethodBody + "' on aspect " + aspectType);
        } else {
          annotationsOnRealMember = realMember.getAnnotations();
        }
        Set<ResolvedType> addedAnnotations = new HashSet<ResolvedType>();
        if (annotationsOnRealMember != null) {
          for (AnnotationAJ anno : annotationsOnRealMember) {
            AnnotationGen a = ((BcelAnnotation) anno).getBcelAnnotation();
            AnnotationGen ag = new AnnotationGen(a, classGen.getConstantPool(), true);
            newMethod.addAnnotation(new BcelAnnotation(ag, classWeaver.getWorld()));
            addedAnnotations.add(anno.getType());
          }
        }
        if (realMember != null) {
          copyOverParameterAnnotations(newMethod, realMember);
        }
        // the code below was originally added to cope with the case where an aspect declares an annotation on an ITD
        // declared within itself (an unusual situation). However, it also addresses the case where we may not find the
        // annotation on the real representation of the ITD. This can happen in a load-time weaving situation where
        // we couldn't add the annotation in time - and so here we recheck the declare annotations. Not quite ideal but
        // works. pr288635
        List<DeclareAnnotation> allDecams = world.getDeclareAnnotationOnMethods();
        for (DeclareAnnotation declareAnnotationMC : allDecams) {
          if (declareAnnotationMC.matches(unMangledInterMethod, world)) {
            // && newMethod.getEnclosingClass().getType() == aspectType) {
            AnnotationAJ annotation = declareAnnotationMC.getAnnotation();
            if (!addedAnnotations.contains(annotation.getType())) {
              newMethod.addAnnotation(annotation);
            }
          }
        }
      }

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


  private void createBridgeIfNecessary(BcelClassWeaver classWeaver, NewMethodTypeMunger munger,
      ResolvedMember unMangledInterMethod, LazyClassGen classGen) {
    if (munger.getDeclaredSignature() != null) {
      boolean needsbridging = false;
      ResolvedMember mungerSignature = munger.getSignature();
      ResolvedMember toBridgeTo = munger.getDeclaredSignature().parameterizedWith(null,
          mungerSignature.getDeclaringType().resolve(getWorld()), false, munger.getTypeVariableAliases());
      if (!toBridgeTo.getReturnType().getErasureSignature().equals(mungerSignature.getReturnType().getErasureSignature())) {
        needsbridging = true;
      }
      UnresolvedType[] originalParams = toBridgeTo.getParameterTypes();
      UnresolvedType[] newParams = mungerSignature.getParameterTypes();
      for (int ii = 0; ii < originalParams.length; ii++) {
        if (!originalParams[ii].getErasureSignature().equals(newParams[ii].getErasureSignature())) {
          needsbridging = true;
        }
View Full Code Here

    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]));
      }
View Full Code Here

    // Step1
    boolean alreadyDone = false; // Compiler might have done it
    ResolvedMember[] localMethods = onType.getDeclaredMethods();
    for (int i = 0; i < localMethods.length; i++) {
      ResolvedMember member = localMethods[i];
      if (member.getName().equals(localMethodName)) {
        // Check the params
        if (member.getParameterSignature().equals(localParameterSig)) {
          alreadyDone = true;
        }
      }
    }

    // Step2
    if (!alreadyDone) {
      // Use the iterator form of 'getMethods()' so we do as little work
      // as necessary
      for (Iterator<ResolvedMember> iter = onType.getSuperclass().getMethods(true, true); iter.hasNext() && !quitRightNow;) {
        ResolvedMember aMethod = iter.next();
        if (aMethod.getName().equals(localMethodName) && aMethod.getParameterSignature().equals(localParameterSig)) {
          // check the return types, if they are different we need a
          // bridging method.
          if (!aMethod.getReturnType().getErasureSignature().equals(localReturnTypeESig)
              && !Modifier.isPrivate(aMethod.getModifiers())) {
            // Step3
            createBridgeMethod(weaver.getWorld(), munger, unMangledInterMethod, gen, paramTypes, aMethod);
            quitRightNow = true;
          }
        }
View Full Code Here

    if (gen.getType().isAnnotation() || gen.getType().isEnum()) {
      // don't signal error as it could be a consequence of a wild type pattern
      return false;
    }

    ResolvedMember introduced = munger.getSignature();

    ResolvedType fromType = world.resolve(introduced.getDeclaringType(), munger.getSourceLocation());
    if (fromType.isRawType()) {
      fromType = fromType.getGenericType();
    }

    boolean shouldApply = munger.matches(weaver.getLazyClassGen().getType(), aspectType);

    if (shouldApply) {
      Type bcelReturnType = BcelWorld.makeBcelType(introduced.getReturnType());

      // If no implementation class was specified, the intention was that
      // the types matching the pattern
      // already implemented the interface, let's check that now!
      if (munger.getImplClassName() == null && !munger.specifiesDelegateFactoryMethod()) {
        boolean isOK = false;
        List<LazyMethodGen> existingMethods = gen.getMethodGens();
        for (LazyMethodGen m : existingMethods) {
          if (m.getName().equals(introduced.getName())
              && m.getParameterSignature().equals(introduced.getParameterSignature())
              && m.getReturnType().equals(bcelReturnType)) {
            isOK = true;
          }
        }
        if (!isOK) {
          // the class does not implement this method, they needed to
          // supply a default impl class
          IMessage msg = new Message("@DeclareParents: No defaultImpl was specified but the type '" + gen.getName()
              + "' does not implement the method '" + stringifyMember(introduced) + "' defined on the interface '"
              + introduced.getDeclaringType() + "'", weaver.getLazyClassGen().getType().getSourceLocation(), true,
              new ISourceLocation[] { munger.getSourceLocation() });
          weaver.getWorld().getMessageHandler().handleMessage(msg);
          return false;
        }

        return true;
      }

      LazyMethodGen mg = new LazyMethodGen(introduced.getModifiers() - Modifier.ABSTRACT, bcelReturnType,
          introduced.getName(), BcelWorld.makeBcelTypes(introduced.getParameterTypes()),
          BcelWorld.makeBcelTypesAsClassNames(introduced.getExceptions()), gen);

      // annotation copy from annotation on ITD interface
      if (weaver.getWorld().isInJava5Mode()) {
        AnnotationAJ annotationsOnRealMember[] = null;
        ResolvedType toLookOn = weaver.getWorld().lookupOrCreateName(introduced.getDeclaringType());
        if (fromType.isRawType()) {
          toLookOn = fromType.getGenericType();
        }
        // lookup the method
        ResolvedMember[] ms = toLookOn.getDeclaredJavaMethods();
        for (ResolvedMember m : ms) {
          if (introduced.getName().equals(m.getName()) && introduced.getSignature().equals(m.getSignature())) {
            annotationsOnRealMember = m.getAnnotations();
            break;
          }
        }
        if (annotationsOnRealMember != null) {
          for (AnnotationAJ anno : annotationsOnRealMember) {
            AnnotationGen a = ((BcelAnnotation) anno).getBcelAnnotation();
            AnnotationGen ag = new AnnotationGen(a, weaver.getLazyClassGen().getConstantPool(), true);
            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()));
View Full Code Here

    }
    // boolean shouldApply =
    munger.matches(weaver.getLazyClassGen().getType(), aspectType); // why
    // do
    // this?
    ResolvedMember host = AjcMemberMaker.itdAtDeclareParentsField(weaver.getLazyClassGen().getType(), munger.getSignature()
        .getType(), aspectType);
    FieldGen field = makeFieldGen(weaver.getLazyClassGen(), host);
    field.setModifiers(field.getModifiers() | BcelField.AccSynthetic);
    weaver.getLazyClassGen().addField(field, null);
    return true;
View Full Code Here

    if (debug) {
      System.err.println("Searching for a member on type: " + aspectType);
      System.err.println("Member we are looking for: " + lookingFor);
    }

    ResolvedMember aspectMethods[] = aspectType.getDeclaredMethods();
    UnresolvedType[] lookingForParams = lookingFor.getParameterTypes();

    ResolvedMember realMember = null;
    for (int i = 0; realMember == null && i < aspectMethods.length; i++) {
      ResolvedMember member = aspectMethods[i];
      if (member.getName().equals(lookingFor.getName())) {
        UnresolvedType[] memberParams = member.getGenericParameterTypes();
        if (memberParams.length == lookingForParams.length) {
          if (debug) {
            System.err.println("Reviewing potential candidates: " + member);
          }
          boolean matchOK = true;
View Full Code Here

  private void addNeededSuperCallMethods(BcelClassWeaver weaver, ResolvedType onType, Set<ResolvedMember> neededSuperCalls) {
    LazyClassGen gen = weaver.getLazyClassGen();

    for (Iterator<ResolvedMember> iter = neededSuperCalls.iterator(); iter.hasNext();) {
      ResolvedMember superMethod = iter.next();
      if (weaver.addDispatchTarget(superMethod)) {
        // System.err.println("super type: " +
        // superMethod.getDeclaringType() + ", " + gen.getType());
        boolean isSuper = !superMethod.getDeclaringType().equals(gen.getType());
        String dispatchName;
        if (isSuper) {
          dispatchName = NameMangler.superDispatchMethod(onType, superMethod.getName());
        } else {
          dispatchName = NameMangler.protectedDispatchMethod(onType, superMethod.getName());
        }
        superMethod = superMethod.resolve(weaver.getWorld());
        LazyMethodGen dispatcher = makeDispatcher(gen, dispatchName, superMethod, weaver.getWorld(), isSuper);

        weaver.addLazyMethodGen(dispatcher);
      }
    }
View Full Code Here

  private boolean mungeNewConstructor(BcelClassWeaver weaver, NewConstructorTypeMunger newConstructorTypeMunger) {

    final LazyClassGen currentClass = weaver.getLazyClassGen();
    final InstructionFactory fact = currentClass.getFactory();

    ResolvedMember newConstructorMember = newConstructorTypeMunger.getSyntheticConstructor();
    ResolvedType onType = newConstructorMember.getDeclaringType().resolve(weaver.getWorld());
    if (onType.isRawType()) {
      onType = onType.getGenericType();
    }

    if (onType.isAnnotation()) {
      signalError(WeaverMessages.ITDC_ON_ANNOTATION_NOT_ALLOWED, weaver, onType);
      return false;
    }

    if (onType.isEnum()) {
      signalError(WeaverMessages.ITDC_ON_ENUM_NOT_ALLOWED, weaver, onType);
      return false;
    }

    if (!onType.equals(currentClass.getType())) {
      return false;
    }

    ResolvedMember explicitConstructor = newConstructorTypeMunger.getExplicitConstructor();
    // int declaredParameterCount =
    // newConstructorTypeMunger.getDeclaredParameterCount();
    LazyMethodGen mg = makeMethodGen(currentClass, newConstructorMember);
    mg.setEffectiveSignature(newConstructorTypeMunger.getSignature(), Shadow.ConstructorExecution, true);

    // pr98901
    // For copying the annotations across, we have to discover the real
    // member in the aspect
    // which is holding them.
    if (weaver.getWorld().isInJava5Mode()) {

      ResolvedMember interMethodDispatcher = AjcMemberMaker.postIntroducedConstructor(aspectType, onType,
          newConstructorTypeMunger.getSignature().getParameterTypes());
      AnnotationAJ annotationsOnRealMember[] = null;
      ResolvedMember realMember = getRealMemberForITDFromAspect(aspectType, interMethodDispatcher, true);
      // 266602 - consider it missing to mean that the corresponding aspect had errors
      if (realMember == null) {
        // signalWarning("Unable to apply any annotations attached to " + munger.getSignature(), weaver);
        // throw new BCException("Couldn't find ITD init member '" + interMethodBody + "' on aspect " + aspectType);
      } else {
        annotationsOnRealMember = realMember.getAnnotations();
      }
      if (annotationsOnRealMember != null) {
        for (int i = 0; i < annotationsOnRealMember.length; i++) {
          AnnotationAJ annotationX = annotationsOnRealMember[i];
          AnnotationGen a = ((BcelAnnotation) annotationX).getBcelAnnotation();
View Full Code Here

  }

  private boolean mungeNewField(BcelClassWeaver weaver, NewFieldTypeMunger munger) {
    /* ResolvedMember initMethod = */munger.getInitMethod(aspectType);
    LazyClassGen gen = weaver.getLazyClassGen();
    ResolvedMember field = munger.getSignature();

    ResolvedType onType = weaver.getWorld().resolve(field.getDeclaringType(), munger.getSourceLocation());
    if (onType.isRawType()) {
      onType = onType.getGenericType();
    }

    boolean onInterface = onType.isInterface();

    if (onType.isAnnotation()) {
      signalError(WeaverMessages.ITDF_ON_ANNOTATION_NOT_ALLOWED, weaver, onType);
      return false;
    }

    if (onType.isEnum()) {
      signalError(WeaverMessages.ITDF_ON_ENUM_NOT_ALLOWED, weaver, onType);
      return false;
    }

    ResolvedMember interMethodBody = munger.getInitMethod(aspectType);

    AnnotationAJ annotationsOnRealMember[] = null;
    // pr98901
    // For copying the annotations across, we have to discover the real
    // member in the aspect
    // which is holding them.
    if (weaver.getWorld().isInJava5Mode()) {
      // the below line just gets the method with the same name in
      // aspectType.getDeclaredMethods();
      ResolvedType toLookOn = aspectType;
      if (aspectType.isRawType()) {
        toLookOn = aspectType.getGenericType();
      }
      ResolvedMember realMember = getRealMemberForITDFromAspect(toLookOn, interMethodBody, false);
      if (realMember == null) {
        // signalWarning("Unable to apply any annotations attached to " + munger.getSignature(), weaver);
        // throw new BCException("Couldn't find ITD init member '" + interMethodBody + "' on aspect " + aspectType);
      } else {
        annotationsOnRealMember = realMember.getAnnotations();
      }
    }

    if (onType.equals(gen.getType())) {
      if (onInterface) {
        ResolvedMember itdfieldGetter = AjcMemberMaker.interFieldInterfaceGetter(field, onType, aspectType);
        LazyMethodGen mg = makeMethodGen(gen, itdfieldGetter);
        gen.addMethodGen(mg);

        LazyMethodGen mg1 = makeMethodGen(gen, AjcMemberMaker.interFieldInterfaceSetter(field, onType, aspectType));
        gen.addMethodGen(mg1);
      } else {
        weaver.addInitializer(this);
        ResolvedMember newField = AjcMemberMaker.interFieldClassField(field, aspectType,
            munger.version == NewFieldTypeMunger.VersionTwo);
        FieldGen fg = makeFieldGen(gen, newField);

        if (annotationsOnRealMember != null) {
          for (int i = 0; i < annotationsOnRealMember.length; i++) {
            AnnotationAJ annotationX = annotationsOnRealMember[i];
            AnnotationGen a = ((BcelAnnotation) annotationX).getBcelAnnotation();
            AnnotationGen ag = new AnnotationGen(a, weaver.getLazyClassGen().getConstantPool(), true);
            fg.addAnnotation(ag);
          }
        }

        if (weaver.getWorld().isInJava5Mode()) {
          String basicSignature = field.getSignature();
          String genericSignature = field.getReturnType().resolve(weaver.getWorld()).getSignatureForAttribute();
          // String genericSignature =
          // ((ResolvedMemberImpl)field).getSignatureForAttribute();
          if (!basicSignature.equals(genericSignature)) {
            // Add a signature attribute to it
            fg.addAttribute(createSignatureAttribute(gen.getConstantPool(), genericSignature));
          }
        }
        gen.addField(fg, getSourceLocation());

      }
      return true;
    } else if (onInterface && gen.getType().isTopmostImplementor(onType)) {
      // wew know that we can't be static since we don't allow statics on
      // interfaces
      if (Modifier.isStatic(field.getModifiers())) {
        throw new RuntimeException("unimplemented");
      }
      weaver.addInitializer(this);
      // System.err.println("impl body on " + gen.getType() + " for " +
      // munger);

      Type fieldType = BcelWorld.makeBcelType(field.getType());

      FieldGen fg = makeFieldGen(gen, AjcMemberMaker.interFieldInterfaceField(field, onType, aspectType));

      if (annotationsOnRealMember != null) {
        for (int i = 0; i < annotationsOnRealMember.length; i++) {
          AnnotationAJ annotationX = annotationsOnRealMember[i];
          AnnotationGen a = ((BcelAnnotation) annotationX).getBcelAnnotation();
          AnnotationGen ag = new AnnotationGen(a, weaver.getLazyClassGen().getConstantPool(), true);
          fg.addAnnotation(ag);
        }
      }

      gen.addField(fg, getSourceLocation());
      // this uses a shadow munger to add init method to constructors
      // 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) {
        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 bridgingSetter = AjcMemberMaker.interFieldInterfaceSetter(toBridgeTo, gen.getType(), aspectType);
          createBridgeMethodForITDF(weaver, gen, itdfieldSetter, bridgingSetter);
        }
      }

      return true;
View Full Code Here

TOP

Related Classes of org.aspectj.weaver.ResolvedMember

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.