[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] hashCode() for mixin?


I have an inter-type declaration that I think of as a mixin - it adds a
private field and some associated methods to various classes.  That works

Now I want to alter the hashCode method on the affected classes so that
the value reflects not just the state of the original class, but includes
the mixin state too.

I can think of two approaches:

- as an additional inter-type method.  The trouble here is that when I
have code like:

  public void MixinInterface.hashCode() {
    return this.state.hashCode() ^ this.hashCode();

I'm going to get an infinite loop, since "this.hashCode()" is the method
I'm defining (I wondered about super.hashCode(), but doesn't that refer to
the superclass of the class I am extending?  The class I am extending
implements hashCode() itself...)

- as dynamic advice:

public aspect FieldDisplayableHash {

  pointcut miHash(MixinInterface mi):
    call(public int Object.hashCode()) && this(mi);

  int around(MixinInterface mi): miHash(mi) {
    int hash = proceed(mi);
    return hash ^ mi.state.hashCode();


Unfortunately this doesn't work, and I don't understand why.

Please can someone point out what I'm doing wrong?  Until now, using
aspects has been pretty simple, but I seem to be stuck here.  Apologies if
this is covered somewhere in documentation that I've missed, or if
something above  is stupid.