Skip to main content

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



On Sat, Feb 20, 2016 at 2:48 AM, Andy Clement <andrew.clement@xxxxxxxxx> wrote:
Hey,

To faithfully reproduce the inheritance with the declare parents statement, I think the declare parents statements needs to do the same thing as the original declaration in the source when you have explicit extends.

I think this is the key point.  For arbritrary subclasses of BaseObject, the only correct declaration that works in all cases is:

class Manager<T> extends SuperManager<T>

and then:

class SubClassAManager extends Manager<SubClassA>
class SubClassBManager extends Manager<SubClassB>

etc.

But to translate that to an aspect, we'd need to able to write:

declare parents: Manager<T> extends SuperManager<T>

which is not possible.

It is possible something funky used to work and it has been tightened up as fixes have gone in (that is, it never should have worked).

That's the impression I get.  Even tighter casting rules would do it.  The project I had where this worked was from the era where generics were relatively new and even javac has changed a lot since then.  It was also hugely complex and highly dependent on libraries of that era, so totally not worth trying to revive it to see how it worked.
 

What you would seem to need is that the declare parents infers the type bound for SuperManager based on the differing return values for getInstance() but AspectJ has never done that.  You could use a generic aspect, perhaps:

abstract aspect ThingExtender<Z> {
  declare parents: Manager extends SuperManager<Z>;
  declare parents: BaseObject extends SuperThing;
}

aspect BaseObjectVariant extends ThingExtender<BaseObject> {}

That's not a bad idea.  It would mean saying:

aspect SubClassAVariant extends ThingExtender<SubClassA>{}
aspect SubClassBVariant extends ThingExtender<SubClassB>{}

etc. (one for each subclass)

But given my real Thing extender is more complex than this example that's still a net win over replicating the raw declare parents lines X times. 

The other possiblity is to forget extending the inheritance chain and simply  introduce all of SuperManager's fields and methods into Manager using field and method ITD's, and ditto for SuperThing into BaseObject.  I went for the inheritance approach because a discrete superclass reads more coherently than a gaggle of ITDs, but otherwise this meets my needs just as well.  Then, if I need a visible change in the class hierarchy a non-generic marker interface would do the trick.


Anyway, thanks for the interesting discussion.  I think I have a better handle on what's possible and what's not - much improved from the "sometimes, magic happens!" level of understanding I started out with a couple of years ago :)

Regards
Jaime


Back to the top