michael.moser@freesurf.ch (4) [Avatar] Offline
#1
Hi Ramnivas and all,

I am trying to get rid of some clutter in our code that only hinders readability and is never correctly updated anyway, which should sound familiar to many of you:

for legal reasons we need to have a line
--------------------
public final static String COPYRIGHT = "bla bla...";
--------------------
in each class. The idea is, that this string constant makes it into the final class-file and thus - when the file is displayed/dumped - the origin of the file becomes visible.

This - I thought - should be a perfect case for an aspect: define a pointcut that matches ALL classes within and below a certain package and add said variable to each matching class as a static variable.

But how do I specify such a pointcut? It's clearly a static, not a dynamic pointcut, but which pointcut type matches a class? And how do I reference that class in the advice???

I tried e.g. this:
public aspect AddCopyRightString {
public pointcut allActClasses(Class x): staticinitialization(com.mycompany..*) && this(x);
/**
* add a static copyright String member to each class:
*/
public final static String x.COPYRIGHT = "bla bla...";
}

... but this only yields an error saying "x can not be resolved". How can I get a reference to a matching class?

Another approach I tried was:

public aspect AddCopyRightString {
/**
* add a static copyright String member to each class:
*/
public final static String com.mycompany..*.COPYRIGHT = "bla bla...";
}

but this only yielded: "Syntax error on tokens, JavaIdentifier expected instead".


The following variant works during runtime, i.e. can request the copyright-string's value from each and every class, but - alas, in this case - the string's value is not actually copied into each classfile and thus this does not fulfil my requirement. smilie

------------------
public aspect AddCopyRightString
{
public interface HasCopyRightString {
// add a static copyright String member to each class:
public final static String COPYRIGHT = "bla bla...";
}

declare parents: (com.mycompany..* && // all classes in and under com.tivoli.act
!com.mycompany.AddCopyRightString) // except this aspect (to prevent recursion)...
implements HasCopyRightString; // ...will implement the above interface
}
------------------

I also experimented with static aspects, i.e.:

------------------
public aspect AddCopyRightString
{
public interface HasCopyRightString {
static aspect Impl {
// add a static copyright String member to each class:
public final static String COPYRIGHT = "bla bla...";
// ^^^^^^ not allowed...
}
}
declare parents: (com.mycompany..* && // all classes in and under com.tivoli.act
!com.mycompany.AddCopyRightString) && // except this aspect
!com.mycompany.AddCopyRightString.Impl) // and this aspect (to prevent recursion)...
implements HasCopyRightString; // ...will implement the above interface
}
------------------
but alas, static fields are not supported in static aspects. And I am not sure either, whether this would mean that the string's value would actually be "woven" into each class file.

Any idea or any help in better understanding how to apply AspectJ to such cases would be highly appreciated!

Michael

PS.: Your book is excellent! I hadn't had any similarly interesting reading time for years!


Message was edited by: michael.moser@freesurf.ch
michael.moser@freesurf.ch (4) [Avatar] Offline
#2
Re: ow to do static crosscutting (adding of variables) across multiple classes?
Oops - I just noticed, that this website does not maintain spaces nor any other formatting when posting.
Thus, alas, the code-snippets in my append look rather unreadable. Apologies...
Michael
ramnivas (171) [Avatar] Offline
#3
Re: ow to do static crosscutting (adding of variables) across multiple clas
Hi Michel,

Thanks for your positive comments on my book.

The behavior you are getting is correct from the AspectJ
perspective (and space efficient when you really don't
want to duplicate the strings in each of the leaf classes).

In current version of AspectJ, there isn't a way to
meet your needs. In future version, there is a
potential feature called "pertype association" that
may do something close. However, generally speaking,
its implementation may be such that you get the
effect of having a string per type without the
class files showing the copyright string.

-Ramnivas
michael.moser@freesurf.ch (4) [Avatar] Offline
#4
Re: ow to do static crosscutting (adding of variables) across multiple clas
> The behavior you are getting is correct from the
> AspectJ
> perspective (and space efficient when you really
> don't
> want to duplicate the strings in each of the leaf
> classes).

I agree - from any technical perspective it would of course be very UNdesireable to have that string duplicated in each class. Alas, the legals (as always) see things differently which may require such non-sense or let's say: technically sub-optimal solutions...

> In current version of AspectJ, there isn't a way to
> meet your needs. In future version, there is a
> potential feature called "pertype association" that
> may do something close.

Thanks for that "pre-anouncement". At least some hope (well, maybe).

> However, generally speaking,
> its implementation may be such that you get the
> effect of having a string per type without the
> class files showing the copyright string.

Well - since legal requirements are most definitely cross-cutting aspects, they may pose some requirements to allow also also technically less than optimal solutions. Hopefully, the AspectJ-folks are listening!

Cheers,
Michael