Hi, Perhaps you would like to see the Take an Advice project ( http://code.google.com/p/take-an-advice/). Some cool stuff there related to "const correctness". Tell me what you think.
Manuel On Fri, Jun 10, 2011 at 22:18, Michael Arndt <[email protected]> wrote: > Hi folks! > This is the first thing I'm trying to do with AspectJ (and AOP for that > matter), so things might be far from perfect. > > I'm trying to implement aspects that generate compiler errors if the > java-program is not "const-correct". > As clarification: As opposed to the "final" keyword this doesn't prevent > the reference from changing, but rather the contents of the object: The > object becomes immutable on request. > > As a first step I want to document all "mutator"-Methods. That is: each > method that modifies the internal state of an object must be annotated > with "@Mutator". > > So an example class looks like this: > > public class SomeClass { > public int a = 0; > public int b = 0; > > public int getA() { > return a; > } > > @Mutator > public void setA(int value) { > this.a = value; > } > > public int getB() { > return b; > } > > /* undocumented mutator > * -> compiler error > */ > public void setB(int b) { > this.b = b; > } > } > > > > To create this behavior I created these Aspects: > > > > public abstract aspect CheckConst<T> { > pointcut missingMutator() : > withincode(!@Mutator * T.*(..)) && set(* T.*); > > declare error : missingMutator() : "Add @Mutator to method declaration"; > } > > aspect SomeClassConst extends CheckConst<SomeClass>{} > > > > As you can see I need (?) one Aspect for each class. So to make sure the > Aspect is created I created another Aspect and modified this on a bit: > > > > public abstract aspect CheckConst<T> { > declare @type: T : @AspectDeclared; > > pointcut missingMutator() : > withincode(!@Mutator * T.*(..)) && set(* T.*); > > declare error : missingMutator() : "Add @Mutator to method declaration"; > } > > aspect SomeClassConst extends CheckConst<SomeClass>{} > > aspect EnforceAspectCreation { > declare error : staticinitialization( > !@AspectDeclared org.company.something..* > && !org.company.something.CheckConst+ > && !EnforceAspectCreation > && !java.lang.annotation.Annotation+) > : "Create a new aspect with CheckConst as base class"; > } > > If I now have another class: > > > /* Compiler error: Create a new aspect with CheckConst as base class */ > public class SomeOtherClass { > > public int a = 0; > > public int getA() { > return a; > } > > /* Currently not a compiler error because > * aspect SomeOtherClassConst extends CheckConst<SomeOtherClass>{} > * is missing > */ > public void setA(int aValue) { > this.a = aValue; > } > } > > > > What it does: EnforceAspectCreation looks for classes which are not > annotated with @AspectDeclared. AspectDeclared is only used interanally. > SomeClassConst extends CheckConst<SomeClass> and thus annotates the > class SomeClass with @AspectDeclared and veryfies that the mutators are > marked. > > Hope thats clear ;D > > While this solution works (there are probably a lot of cases I don't > catch at the moment) I think that it's very tedious to write hundrets of > concrete aspects all extending CheckConst<*>. It's even ironic to do > this, since this is actually a crosscut concern, isn't it? > > The problem, from my perspective, is in the CheckConst<T>-Aspect, at line > withincode(!@Mutator * T.*(..)) && set(* T.*); > > Here both occurences of T need to refere to the same type. Unfortunately > it seems not possible to bind a type to a name like you can do it with > arguments etc, with the exception of abstract aspects. > What I was hoping for was something like this: > > aspect AllMyClassesConst extends CheckConst<thisType> > pertypewithin(org.company.something..*) {} > > Which would create all concrete aspects automatically. > > Is something like this possible? > > Greetings, > Mene > _______________________________________________ > aspectj-users mailing list > [email protected] > https://dev.eclipse.org/mailman/listinfo/aspectj-users > -- http://www.mmsequeira.pro/
_______________________________________________ aspectj-users mailing list [email protected] https://dev.eclipse.org/mailman/listinfo/aspectj-users
