Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] AspectJ generic constraint?

Just to make the advice work, you can of course specify the erasure of
the type variable rather than the type variable ... but i guess that
isnt what you want... :

public aspect GenericClassHandler {
      pointcut getMethodInvocation(GenericClass thisObject) :
              call(* GenericClass.getElement()) && target(thisObject);

      Object around(GenericClass thisObject) : getMethodInvocation(){
              System.out.println("Get inside aspect");
              return proceed(thisObject);
      }

      pointcut setMethodInvocation(GenericClass thisObject, Object element) :
               call(void GenericClass.setElement(..)) &&
target(thisObject) && args(element);

      void around(GenericClass thisObject, Object element) :
setMethodInvocation(thisObject,element){
              System.out.println("Set inside aspect");
              proceed(thisObject,element);
      }
}

And you could get the type of the element at runtime with a call to
getClass() on the element parameter to the around advice.  But you
cannot use 'T' like that in the aspect and have it be resolved to the
type variable in the advised type.

Andy.

On 14/02/07, Norbert Radyk <norbert.radyk@xxxxxxxxx> wrote:
Hi,

I've found a limitation during working with AspectJ and generic
classes and I can't find a solution for this anywhere. Maybe
somebody here can help me and have already found the
solution for the problem showed below.

We ve got a typical generic class:

public class GenericClass<T> {
        private T element;

        public void setElement(T element){
                System.out.println("Set inside class");
                this.element = element;
        }
        public T getElement(){
                System.out.println("Get inside class");
                return element;
        }
}

Is there any possibility to make an advice to get the method generic parameter
(which is of the type T)  for setElement method? I cannot use here args()
statement because T isn't recognized parameter type in the aspect that
should be declared the advice in.

What's more is there any possibility to prepare the around advice for
the getElement() method in such way that it would not return the
element of known type for example Object type, but the unknown T type.

As I said, I cannot implement an aspect looking like sth like that:

public aspect GenericClassHandler {
        pointcut getMethodInvocation(GenericClass thisObject) :
                call(void *.GenericClass.setElement()) && target(thisObject);

        T around(GenericClass thisObject) : getMethodInvocation(){
                System.out.println("Get inside aspect");
                return proceed(thisObject);
        }

        pointcut setMethodInvocation(GenericClass thisObject, T element) :
                 call(void *.GenericClass.setElement(..)) && target(thisObject) && args(element);

        void around(GenericClass thisObject, T element) : setMethodInvocation(thisObject,element){
                System.out.println("Set inside aspect");
                proceed(thisObject,element);
        }
}

because T is not recognized type whithin the aspect.

Thx,

Norbert Radyk,
Wroclaw University of Technology


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



Back to the top