Skip to main content

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

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




Back to the top