I want my class to implement an interface, but I want to provide the implementation of the methods using ITD in an aspect. Is this possible?
Interface:
public interface CloningService<T> {
public T clone(T object);
}
Default implementation:
public class DefaultCloningServiceImpl implements CloningService<T> {
public T clone(T object) {
// implementation of the clone method
}
}
Specific implementation:
public class PersonService implements CloningService<Person> {
// no code (!)
}
The class PersonService would declare that it implements the CloningService interface, but the actual implementation of the methods would be provided in DefaultCloningServiceImpl and an aspect would introduce them to PersonService.
I followed the example on Eclipse.com and I tried to use @DeclareParents to achieve the above functionality. However, I was getting a compiler error from AspectJ, which had to do with generics. It’s as if the @DeclareParents annotation did not expect the generics to be used…
Thank you.
I’d recommend that you use code style aspectj to solve this rather than annotation style.
This could be done simply by having an aspect like this:
To make this more general and attached to an annotation, you can do something like this:
And if you wanted to package this up into a standalone jar, just make sure to add all code that you want to weave adds this jar to its aspect path (if using compile-time weaving).