javaaspectjajdt

@DeclareMixin added methods not visible by other classes


I'm trying to use @DeclareMixin for the first time, and either I am doing something incorrect, or there is a bug somewhere.

I've published my sample code to github: https://github.com/benze/AspectJError.git. I'm pasting the little bits here as well.

If I look at the compiled code of ApplyAspect.class using a decompiler, I can see that ajc has properly added in the implemented interface. However, the compiler complains in Test that ApplyAspect does not have the setCreated() or the getCreated() methods.

Additionally, if I try to compile the project from the command line, I get the same compilation errors as well.

I'm not sure what I am doing wrong, or if there is a bug somewhere else with @DeclareMixin directive.

Interface CreatedBean.java:

public interface CreatedBean {
    public Object getCreated();
    public void setCreated(final Object created);
}

Implementation CreatedBeanImpl.java:

public class CreatedBeanImpl implements CreatedBean{
        private Object created;

        public Object getCreated(){
            return this.created;
        }

        public void setCreated(final Object created ){
            this.created = created;
        }
}

Aspect definition:

@Aspect
public class DeclareMixinAspect {
    @DeclareMixin("com.benze.bo.ApplyAspect")
    public CreatedBean auditableBeanMixin(){
        return new CreatedBeanImpl();
    }
}

Class being advised (com.benze.bo pkg):

public class ApplyAspect {
    private String name = "test class";
}

Class trying to use ApplyAspect:

public class Test {

    public static void main(String[] args) {
        ApplyAspect aa = new ApplyAspect();
        aa.setCreated(new Date());
        System.out.println( aa.getCreated().toString());
        System.out.println(aa.toString());
        System.out.println("all done");
    }
}

The pom is very basic with only the aspectj plugin (and dependencies) added. I'm using AJ 1.8.2.


Solution

  • I think you need casts in your Test class:

    ((CreatedBean)aa).setCreated(new Date());
    
    System.out.println(((CreatedBean)aa).getCreated().toString());
    

    IIRC the reason is that annotation style code is intended to be compilable with javac, which would not know about the affect of DeclareMixin.