Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] XDoclet integration?

Has anyone looked at integrating AspectJ with XDoclet? I was thinking this could be helpful to prototype uses of metadata with AspectJ programs. With JSR-175 now available for community review, this seems like an opportune time. It should also be instructive in thinking about what pointcuts and compiler support will be required for 175...
 
I think it would be possible to use XDoclet to generate code like this:
 
/**
@tag attr="val"
*/
class Foo {...}
 
->
 
public interface tagMarker {}
class Foo implements tagMarker { ... }
 
and
 
class Foo {
    /** @tag */ void methodA(int x) { ... }
    /** @tag */ int methodB() { ... }
    /** @tag */ int field;
}
 
->
 
public abstract aspect tagReader {
    abstract public pointcut tagExecution();
    abstract public pointcut tagGet();
    abstract public pointcut tagSet();
}
 
/** empty class that contains any user-defined code to access these pointcuts */
public abstract aspect tagReaderExtension extends tagReader {
}
 
class Foo {
    // use the participant pattern: an alternative is to generate concrete
    // pointcuts in the tagReader aspect that contains all the pointcuts
    static aspect MetadataParticipant extends tagReaderExtension {
        public pointcut tagExecution() :
            execution(void Foo.methodA(int)) || execution(int Foo.methodB());
        public pointcut tagGet() : get(int Foo.field);
        public pointcut tagSet() : set(int Foo.field);
     }
     void methodA(int x) { ... }
     int methodB() { ... }
}
 
This would allow writing pointcuts that refer to tagged methods, interfaces, or classes. From a first look at how XDoclet generation works, this looks like it would be relatively straightforward. However, I've not tried to write custom XDoclet code before, so I'm curious if anyone who has could comment on the feasibility and ease of doing this. Would it be easier if we limited it to only generating new files instead of adding code to existing ones?
 
Ron
 
p.s. I believe it would also be possible to generate a way of accessing attributes for the tags as follows. However, this seems rather klunky, so I'd sooner wait til there's a primitive pointcut descriptor to access them than use XDoclet code generation...
 
/**
@tag attr="val"
*/
class Foo {...}
 
->
 
interface tagMarkerInterface {
    String getAttr();
}
 
aspect tagReaderMarkerInterface {
    String tagMarkerInterface.attr;
 
    public String tagMarkerInterface.getAttr() {
        return attr;
    }
 
    before(Foo obj) : initialization(Foo.new(..)) && target(obj) {
        obj.attr = "val";
    }
}
 
class Foo implements tagMarkerInterface { ... }
 
and
 
class Foo {
    /** @tag attr="val1" */ void methodA(int x) { ... }
    /** @tag attr="val2" */ int methodB() { ... }
}
 
->
 
class Foo {
   static aspect MetadataParticipant {
       ThreadLocal attrHolder = new ThreadLocal();
       pointcut tagExecution() :
           execution(void Foo.methodA(int)) || execution(int Foo.methodB());
       String getAttr() { return (String)(attrHolder.get()); }
       before() : execution(void Foo.methodA(int)) {
           attrHolder.set("val1");
       }
       before() : execution(int Foo.methodB()) {
           attrHolder.set("val2");
       }
    }
    void methodA(int x) { ... }
    int methodB() { ... }
}
 
Ron Bodkin
Chief Technology Officer
New Aspects of Security

Back to the top