Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Match a method call on a class field with specific annotation


This is an interesting use case because.... there's no easy way to do this.

At a call join point, we know about the the *caller*, and the *callee*, we know the static context in which the call is being made (within, withincode), and the dynamic context in which the call is being made (cflow[below]), so whilst we can say:

"any call where the type of the target of the call has an annotation @ABC..."

we don't have a way to say:

"any call where the target of the call is currently refered to by a member variable, and that member variable has an annotation of type @ABC..."

The closest thing you can say is

"any access to a field with annotation @MyAnnotation"  : get(@MyAnnotation * *)

Using reflection it should be possible, but awkward to get closer what you want:

pointcut annotatedFieldCall() : call(* *(..)) && if(calleeIsAFieldWithMyAnnotation(thisJoinPoint);

private static boolean calleeIsAFieldWithMyAnnotation(JoinPoint tjp) {

  / / pseudo-code...

   Object obj = tjp.getThis();
   for-all (Field field : obj.getClass().getDeclaredFields()) {
      if (field.hasAnnotation(MyAnnotation.class) return true;
   }
   

   return false;
}


but it's not pretty.... (and only works for fields defined within the calling class).

If you *could* write a pointcut to match this, how would you want it to look?

-- Adrian
Adrian_Colyer@xxxxxxxxxx



Rifflard Mickaël <Mickael.Rifflard@xxxxxxxxxxxxxx>
Sent by: aspectj-users-admin@xxxxxxxxxxx

23/02/2005 08:22

Please respond to
aspectj-users@xxxxxxxxxxx

To
<aspectj-users@xxxxxxxxxxx>
cc
Subject
[aspectj-users] Match a method call on a class field with specific annotation





Hi all,

I don't succeed to express with AspectJ 5 this _expression_ :

                Match a method call on a class field with specific annotation
                (Of course, I don't know the name of the class and field)

Exemple :

                public class MyClass {

                                 @MyAnnotation
                                 public static SortedMap myMap1 = new TreeMap();

                                 public static SortedMap myMap2 = new TreeMap();

                                 public static void main(String[] args) {
                                                  myMap1.put("key","value"); // Match this method call
                                                  myMap2.put("key","value"); // Don't match this method call
                                 }
                }

Any suggestion would be appreciated,

Regards

                Mickaël Rifflard


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top