Hey guys,

I have been using AspectJ on and off for some time now, to good success. 
However, today I was having what seems like a relatively simple problem that 
took me some time to resolve. I have in the meantime found a workaround, but I 
would still be interested in why my original solution was not ok.

Basically, I am writing a Java-based piece of middleware that needs to keep 
track of objects. To this end, I want to potentially be able to inject a 
java.util.UUID into any Java object. To do this, I want to define a very simple 
Mixin:

public class IdentifiableMixin implements IIdentifiable {
        
        private UUID id;
        
        public UUID getPlatformId() {
                return id;
        }
        
        public void setPlatformId(UUID id) {
                this.id = id;
        }
}

@Aspect
public class MyAspect {
                
        public interface IIdentifiable {
                UUID getPlatformId();
                void setPlatformId(UUID id);
        }

        @DeclareMixin("*")
        public static IIdentifiable createIIdentifiable() {
                return new IdentifiableMixin();
        }
}

Note that I wanted to match the mixin to every possible type of object, hence 
*. I quickly figured out that this does not work, as AspectJ then also tries to 
weave into enums and interfaces (which it can't). Hence, I updated to:

@Aspect
public class MyAspect {
                
        public interface IIdentifiable {
                UUID getPlatformId();
                void setPlatformId(UUID id);
        }


        @DeclareMixin("!is(InterfaceType) && !is(EnumType)")
        public static IIdentifiable createIIdentifiable() {
                return new IdentifiableMixin();
        }
}

Still not working, though. It seems like this is working fine as long as the 
classes being weaved are not part of a class hierarchy. However, as soon as two 
classes extend from each other, I get plenty of weave errors indicating 
conflicts. To me, it was not clear why this would happen. Intuitively, it seems 
like it should be ok if both parent and child in a class hierarchy implement 
the same mixin, correct? I can post the concrete errors I was getting if this 
is of interest.

Now, after some further experimentation I found that it seems to work if I move 
the interface of the mixin to a separate Java file:

public interface IIdentifiable {
        UUID getPlatformId();
        void setPlatformId(UUID id);
}

@Aspect
public class MyAspect {

        @DeclareMixin("!is(InterfaceType) && !is(EnumType)")
        public static IIdentifiable createIIdentifiable() {
                return new IdentifiableMixin();
        }
}

To me this was rather odd. Is this the expected behavior, and, if so, why?

thanks, /philipp
_______________________________________________
aspectj-users mailing list
aspectj-users@eclipse.org
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Reply via email to