Using @Aspect in a Scala project

57 views
Skip to first unread message

Nikoh

unread,
Dec 6, 2011, 7:52:05 AM12/6/11
to scala-i...@googlegroups.com
Hi,

Against good advice I'm using annotation-based aspects in a Scala project. I can write an @Aspect class in Scala, compile it in scalac, then weave the .class files with iajc, and it works fine. With Scala IDE's dependency on the weaving feature I was hoping it might also weave my aspects, but this doesn't seem to be the case from running my tests within Eclipse. I just wanted to check if this is something that was meant to work, or if the weaving feature is just required internally for the Scala IDE plugin?

If it's not currently a feature, do you think it's something that might get developed (or even something that could get added to AJDT)? I know it's a long shot, with traits being the preferred way of doing things round these parts.

Thanks.

Nikoh

unread,
Dec 6, 2011, 7:56:40 AM12/6/11
to scala-i...@googlegroups.com
then weave the .class files with iajc

When I say iajc I mean the Ant task that runs the AspectJ compiler.  

iulian dragos

unread,
Dec 6, 2011, 9:54:00 AM12/6/11
to scala-i...@googlegroups.com
On Tue, Dec 6, 2011 at 1:52 PM, Nikoh <nick...@gmail.com> wrote:
Hi,

Against good advice I'm using annotation-based aspects in a Scala project. I can write an @Aspect class in Scala, compile it in scalac, then weave the .class files with iajc, and it works fine. With Scala IDE's dependency on the weaving feature I was hoping it might also weave my aspects, but this doesn't seem to be the case from running my tests within Eclipse. I just wanted to check if this is something that was meant to work, or if the weaving feature is just required internally for the Scala IDE plugin?

Are you working on an Eclipse plugin project, or it is a plain Scala application? I don't know much about how aspectJ weaving works, but I imagine you need to tell the JVM something about it in order to kick in? Probably your Run Configuration is missing the required command line options to make it happen.

iulian
 

If it's not currently a feature, do you think it's something that might get developed (or even something that could get added to AJDT)? I know it's a long shot, with traits being the preferred way of doing things round these parts.

Thanks.



--
« Je déteste la montagne, ça cache le paysage »
Alphonse Allais

Nikoh

unread,
Dec 6, 2011, 10:10:40 AM12/6/11
to scala-i...@googlegroups.com
Are you working on an Eclipse plugin project, or it is a plain Scala application? I don't know much about how aspectJ weaving works, but I imagine you need to tell the JVM something about it in order to kick in? Probably your Run Configuration is missing the required command line options to make it happen.

It's a plain Scala application. I did think about whether I could alter the Run configuration to set the Java agent to be aspectjweaver.jar (to enable load-time weaving), but the behaviour in AJDT for Java projects at least is that weaving happens transparently within the project, so that the output classes from the project builder are automatically woven. When AJDT converts a project to support Aspects it replaces the Java builder with its own builder, which weaves as it compiles. I was hopeful that a similar solution existed for Scala. (I did try the load-time weaving agent but didn't have much success. I think it requires an extra configuration file.)

I don't know an awful lot about the way Eclipse project builders worked but I wondered if it would be possible to make an AspectJ compiler that worked on existing class files. That way the Scala builder could compile the source as normal (treating the aspect annotations as nothing special), then the additional builder would go through weaving them as appropriate. At the moment, if you try to add both the Scala builder and the AspectJ builder into the same project, things go weird and the AspectJ builder starts removing class files that Scala just compiled. It was never meant to work like that so I'm not surprised it doesn't.

Nikoh

unread,
Mar 1, 2012, 12:02:08 PM3/1/12
to scala-i...@googlegroups.com
Thought I'd come back here and note that I got my tests running in Eclipse by using load-time-weaving rather than compile-time-weaving - AspectJ can run as a Java instrumentation agent so I just specify it on the command line as -javaagent:lib/build/aspectj/aspectjweaver.jar and provide a META-INF/aop.xml file to tell it what to do, and now my tests run in Eclipse. Somewhat more straightforward than building a whole new Eclipse plugin :)
Reply all
Reply to author
Forward
0 new messages