Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] declare parents and generic type parameters

Hello List,

I've been ruminating about some the problems I've been having with ITD's, and I'm speculating that some of them at least are due to a more rigorous treatment of generics in the last couple of major versions.  More specifically, I don't think it's possible for declare parents to reproduce an inheritance hierachy involving generics with type parameters.

To illustrate, lets consider this class hierarchy.  We have a class and superclass:

public class SuperThing {}
public class BaseObject extends SuperThing {}

And then we have some classes to interact with them:

public abstract class SuperManager<T> {
public T getSuperInstance() {
return getInstance();
}
public abstract T getInstance();
}

public class Manager extends SuperManager<BaseObject> {
public BaseObject getInstance() {
return new BaseObject();
}
}

So then if we call:

new Manager().getSuperInstance();

We get back an instance of type BaseObject, as we'd expect.  The type parameter T turns into BaseObject all the way up the tree.

Now let's replace the top level of inheritance with ITD declare parents statements.  First we remove the explicit inheritance:

public class BaseObject {}
public abstract class Manager {...}

and then reintroduce the inheritance via this aspect:

public aspect ThingExtender {
declare parents: Manager extends SuperManager;
declare parents: BaseObject extends SuperThing;
}

Now, if we call:

new Manager().getSuperInstance();

We get back an instance of type Object.  This is entirely understandable - AspectJ works with the erasure of generic types, and the erasure of T is just Object.

I know I could do:

declare parents: Manager extends SuperManager<BaseObject>;

to fix this particular case.  But in my real project, both Manager and BaseObject have many subclasses themselves, so this solution just pushes the problem down to the next level.

So, it's pretty clear that declare parents can't faithfully reproduce normal inheritance for generic type parameters.  Which leaves me wondering:

1. Is it supposed to?
2. Is that even possible?
3. Did this sort of thing really work once upon a time (around about AspectJ 1.6) and then stop working, or am I just imagining it?

Thanks for listening
Jaime



Back to the top