Package org.aspectj.weaver

Examples of org.aspectj.weaver.World$AspectPrecedenceCalculator$PrecedenceCacheKey


    }
    return onType.equals(classWeaver.getLazyClassGen().getType());
  }

  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);
              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();
          if (!basicSignature.equals(genericSignature)) {
            // Add a signature attribute to it
            mg.addAttribute(createSignatureAttribute(classGen.getConstantPool(), genericSignature));
View Full Code Here


    }
    return buf.toString();
  }

  private boolean mungeMethodDelegate(BcelClassWeaver weaver, MethodDelegateTypeMunger munger) {
    World world = weaver.getWorld();

    LazyClassGen gen = weaver.getLazyClassGen();
    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);
View Full Code Here

    weaver.getLazyClassGen().addField(field, null);
    return true;
  }

  private ResolvedMember getRealMemberForITDFromAspect(ResolvedType aspectType, ResolvedMember lookingFor, boolean isCtorRelated) {
    World world = aspectType.getWorld();
    boolean debug = false;
    if (debug) {
      System.err.println("Searching for a member on type: " + aspectType);
      System.err.println("Member we are looking for: " + lookingFor);
    }
View Full Code Here

    }
  }

  private boolean hasField(ResolvedType type) {
    // TODO what about ITDs
    World world = type.getWorld();
    for (Iterator iter = type.getFields(); iter.hasNext();) {
      Member field = (Member) iter.next();
      if (signaturePattern.matches(field, type.getWorld(), false)) {
        if (field.getDeclaringType().resolve(world) != type) {
          if (Modifier.isPrivate(field.getModifiers())) continue;
View Full Code Here

    return false;
  }
 
  private boolean hasMethod(ResolvedType type) {
    // TODO what about ITDs
    World world = type.getWorld();
    for (Iterator iter = type.getMethods(); iter.hasNext();) {
      Member method = (Member) iter.next();
      if (signaturePattern.matches(method, type.getWorld(), false)) {
        if (method.getDeclaringType().resolve(world) != type) {
          if (Modifier.isPrivate(method.getModifiers())) continue;
View Full Code Here

      }
    }
    entryBindings.copyContext(bindings);
    //System.out.println(this + " bindings: " + entryBindings);
   
    World world = inAspect.getWorld();
   
    Pointcut concreteEntry;
   
    ResolvedType concreteAspect = bindings.getConcreteAspect();
   
    CrosscuttingMembers xcut = concreteAspect.crosscuttingMembers;   
    Collection previousCflowEntries = xcut.getCflowEntries();
   
   
    entryBindings.pushEnclosingDefinition(CFLOW_MARKER);
    // This block concretizes the pointcut within the cflow pointcut
    try {
      concreteEntry = entry.concretize(inAspect, declaringType, entryBindings);
    } finally {
      entryBindings.popEnclosingDefinitition();
    }

    List innerCflowEntries = new ArrayList(xcut.getCflowEntries());
    innerCflowEntries.removeAll(previousCflowEntries);

    Object field = getCflowfield(concreteEntry,concreteAspect);
   
    // Four routes of interest through this code (did I hear someone say refactor??)
    // 1) no state in the cflow - we can use a counter *and* we have seen this pointcut
    //    before - so use the same counter as before.
    // 2) no state in the cflow - we can use a counter, but this is the first time
    //    we have seen this pointcut, so build the infrastructure.
    // 3) state in the cflow - we need to use a stack *and* we have seen this pointcut
    //    before - so share the stack.
    // 4) state in the cflow - we need to use a stack, but this is the first time
    //    we have seen this pointcut, so build the infrastructure.
   
    if (freeVars==null || freeVars.length == 0) { // No state, so don't use a stack, use a counter.
      ResolvedMember localCflowField = null;

      // Check if we have already got a counter for this cflow pointcut
      if (field != null) {
         localCflowField = (ResolvedMember)field; // Use the one we already have
        
      } else {
       
        // Create a counter field in the aspect
        localCflowField = new ResolvedMemberImpl(Member.FIELD,concreteAspect,Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL,
          NameMangler.cflowCounter(xcut),UnresolvedType.forName(NameMangler.CFLOW_COUNTER_TYPE).getSignature());
      
        // Create type munger to add field to the aspect
        concreteAspect.crosscuttingMembers.addTypeMunger(world.makeCflowCounterFieldAdder(localCflowField));
     
        // Create shadow munger to push stuff onto the stack
        concreteAspect.crosscuttingMembers.addConcreteShadowMunger(
        Advice.makeCflowEntry(world,concreteEntry,isBelow,localCflowField,freeVars==null?0:freeVars.length,innerCflowEntries,inAspect));
     
      putCflowfield(concreteEntry,concreteAspect,localCflowField); // Remember it
        }
       
      Pointcut ret = new ConcreteCflowPointcut(localCflowField, null,true);
      ret.copyLocationFrom(this);
      return ret;
    } else {

      List slots = new ArrayList();
     
      for (int i=0, len=freeVars.length; i < len; i++) {
        int freeVar = freeVars[i];
       
        // we don't need to keep state that isn't actually exposed to advice
        //??? this means that we will store some state that we won't actually use, optimize this later
        if (!bindings.hasKey(freeVar)) continue;
       
        int formalIndex = bindings.get(freeVar);
       
        // We need to look in the right place for the type of the formal.  Suppose the advice looks like this:
        //  before(String s):  somePointcut(*,s)
        // where the first argument in somePointcut is of type Number
        // for free variable 0 we want to ask the pointcut for the type of its first argument, if we only
        // ask the advice for the type of its first argument then we'll get the wrong type (pr86903)
       
        ResolvedPointcutDefinition enclosingDef = bindings.peekEnclosingDefinition();
        ResolvedType formalType = null;
       
        // Is there a useful enclosing pointcut?
        if (enclosingDef!=null && enclosingDef.getParameterTypes().length>0) {
          formalType = enclosingDef.getParameterTypes()[freeVar].resolve(world);
        } else {
          formalType = bindings.getAdviceSignature().getParameterTypes()[formalIndex].resolve(world);
        }
       
        ConcreteCflowPointcut.Slot slot =
          new ConcreteCflowPointcut.Slot(formalIndex, formalType, i);
        slots.add(slot);
      }
      ResolvedMember localCflowField = null;
      if (field != null) {
        localCflowField = (ResolvedMember)field;
      } else {
         
        localCflowField = new ResolvedMemberImpl(
        Member.FIELD, concreteAspect, Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL,
            NameMangler.cflowStack(xcut),
            UnresolvedType.forName(NameMangler.CFLOW_STACK_TYPE).getSignature());
        //System.out.println("adding field to: " + inAspect + " field " + cflowField);
           
        // add field and initializer to inAspect
        //XXX and then that info above needs to be mapped down here to help with
        //XXX getting the exposed state right
        concreteAspect.crosscuttingMembers.addConcreteShadowMunger(
          Advice.makeCflowEntry(world, concreteEntry, isBelow, localCflowField, freeVars.length, innerCflowEntries,inAspect));
     
        concreteAspect.crosscuttingMembers.addTypeMunger(
        world.makeCflowStackFieldAdder(localCflowField));
        putCflowfield(concreteEntry,concreteAspect,localCflowField);
        }
      Pointcut ret = new ConcreteCflowPointcut(localCflowField, slots,false);
      ret.copyLocationFrom(this);
      return ret;
View Full Code Here

   
    Member cflowStackField = new ResolvedMemberImpl(
      Member.FIELD, inAspect, Modifier.STATIC|Modifier.PUBLIC|Modifier.FINAL,
            UnresolvedType.forName(NameMangler.CFLOW_STACK_TYPE), NameMangler.PERCFLOW_FIELD_NAME, UnresolvedType.NONE);
           
    World world = inAspect.getWorld();
   
    CrosscuttingMembers xcut = inAspect.crosscuttingMembers;
   
    Collection previousCflowEntries = xcut.getCflowEntries();
    Pointcut concreteEntry = entry.concretize(inAspect, inAspect, 0, null); //IntMap.EMPTY);
View Full Code Here

    ret.copyLocationFrom(this);
    ret.inAspect = inAspect;
    if (inAspect.isAbstract()) return ret;
   
   
    World world = inAspect.getWorld();
   
    SignaturePattern sigpat = new SignaturePattern(
        Member.STATIC_INITIALIZATION,
        ModifiersPattern.ANY,
        TypePattern.ANY,
        TypePattern.ANY,//typePattern,
        NamePattern.ANY,
        TypePatternList.ANY,
        ThrowsPattern.ANY,
        AnnotationTypePattern.ANY
        );
   
    Pointcut staticInitStar = new KindedPointcut(Shadow.StaticInitialization,sigpat);
    Pointcut withinTp= new WithinPointcut(typePattern);
    Pointcut andPcut = new AndPointcut(staticInitStar,withinTp);
    // We want the pointcut to be 'staticinitialization(*) && within(<typepattern>' -
    // we *cannot* shortcut this to staticinitialization(<typepattern>) because it
    // doesnt mean the same thing.

    // This munger will initialize the aspect instance field in the matched type
    inAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makePerTypeWithinEntry(world, andPcut, inAspect));
   
    ResolvedTypeMunger munger = new PerTypeWithinTargetTypeMunger(inAspect, ret);
    inAspect.crosscuttingMembers.addTypeMunger(world.concreteTypeMunger(munger, inAspect));

        //ATAJ: add a munger to add the aspectOf(..) to the @AJ aspects
        if (inAspect.isAnnotationStyleAspect() && !inAspect.isAbstract()) {
            inAspect.crosscuttingMembers.addLateTypeMunger(
                    inAspect.getWorld().makePerClauseAspect(inAspect, getKind())
View Full Code Here

        if (type.matchesInstanceof(argRTX).alwaysTrue()) {
          continue;
        }
      }

      World world = shadow.getIWorld();
      ResolvedType typeToExpose = type.getExactType().resolve(world);
      if (typeToExpose.isParameterizedType()) {
        boolean inDoubt = (type.matchesInstanceof(argRTX) == FuzzyBoolean.MAYBE);       
        if (inDoubt && world.getLint().uncheckedArgument.isEnabled()) {
          String uncheckedMatchWith = typeToExpose.getSimpleBaseName();
          if (argRTX.isParameterizedType() && (argRTX.getRawType() == typeToExpose.getRawType())) {
            uncheckedMatchWith = argRTX.getSimpleName();
          }
          if (!isUncheckedArgumentWarningSuppressed()) {
            world.getLint().uncheckedArgument.signal(
                new String[] {
                    typeToExpose.getSimpleName(),
                    uncheckedMatchWith,
                    typeToExpose.getSimpleBaseName(),
                    shadow.toResolvedString(world)},
View Full Code Here

    }

    public void visit(Instanceof i) {
      ReflectionVar v = (ReflectionVar) i.getVar();
      Object value = v.getBindingAtJoinPoint(thisObject,targetObject, args);
      World world = v.getType().getWorld();
      ResolvedType desiredType = i.getType().resolve(world);
      ResolvedType actualType = world.resolve(value.getClass().getName());
      matches = desiredType.isAssignableFrom(actualType);
    }
View Full Code Here

TOP

Related Classes of org.aspectj.weaver.World$AspectPrecedenceCalculator$PrecedenceCacheKey

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.