Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Mixins with @DeclareParents (M5)

I'm trying to implement mixins using the @Aspect/@DeclareParents annotations. To begin with, the following works:
PersistentEntity.java:
public @interface PersistentEntity { }

PersistentEntityAspect.aj:
public aspect PersistentEntityAspect {
  public interface Persistent {
     long getId();
     void setId(long id);
  }
declare parents : @PersistentEntity * implements Persistent; long Persistent.id = 0; public long Persistent.getId() { return id; }
  public void Persistent.setId(long id) { this.id = id; }
}

Test.java:
@PersistentEntity
public class Test { }

Using Jython, I can see that Test implements PersistentEntityAspect.Persistent and can call both getId() and setId().

The following does not work (replacing PersistentEntityAspect.aj with PersistentEntityAspect.java):
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

@Aspect
public class PersistentEntityAspect {
  public interface Persistent {
     long getId();
     void setId(long id);
  }
public class PersistentImpl implements Persistent {
     long id;
public long getId() { return id; }
     public void setId(long id) { this.id = id; }
  }
@DeclareParents("@PersistentEntity *") public static Persistent introduced = new PersistentEntityAspect().new PersistentImpl();
}

Test is not advised and does not implement any interface. (I did use the recent thread http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg05032.html as reference, but this neither compiled (... introduced = new MoodyImpl();) nor did I get the advice bit working out. I must be doing something simple wrong (maybe I need to post-compile the result to weave it or use load-time weaving? I'm not sure how to do either (more research!))...

Other questions... Eclipse/AJDT does not allow me to use methods introduced by advice. For example, Test.java could not look as follows:
public Test {
  long test { return getId(); }
}

This actually compiles and runs correctly using ajc, but won't get compile in the IDE (The method getId() is undefined...).

Also, how're people using AspectJ with Maven 2? Confessing to be a newbie to both AspectJ and Maven, I'm currently "tricking" it by using ajc as my java compiler.


Back to the top