This page is no longer maintained — Please continue to the home page at www.scala-lang.org

AOP with scala

6 replies
Maxime Lévesque
Joined: 2009-08-18,
User offline. Last seen 42 years 45 weeks ago.

  I'm writing a framwork that needs to intercept getter and setter calls
on external scala classes (classes that will be written by users of the
framework). It seems that there is no feature to do this in the language,
is this correct ?
I know some form of interception can be done using implicit conversions,
but it seems that for a generic interception mechanism, one still needs an
AOP framework like AspectWerks ...

 Has anyone used an AOP framework with scala ?  Any recommendations
on which one ?

 Thanks
Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: AOP with scala
You can always use mixins (traits)The only catch is that they have to be mixed in to a class at instantiation:
i.e. val x = new Foo with Bar
so you can't use them as decorators over existing object instances.
A few ppeople (myself included) are working on ways to address this shortfall and allow for dynamic mixins, but you're unlikely to see anything before the 2.8 release.


2009/9/22 Maxime Lévesque <maxime.levesque@gmail.com>

  I'm writing a framwork that needs to intercept getter and setter calls
on external scala classes (classes that will be written by users of the
framework). It seems that there is no feature to do this in the language,
is this correct ?
I know some form of interception can be done using implicit conversions,
but it seems that for a generic interception mechanism, one still needs an
AOP framework like AspectWerks ...

 Has anyone used an AOP framework with scala ?  Any recommendations
on which one ?

 Thanks

Jonas Bonér
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: AOP with scala

Hi.

I wrote a blog post about an AOP framework that I wrote on top of
AspectJ together with mixin composition.
http://jonasboner.com/2008/02/06/aop-style-mixin-composition-stacks-in-s...

The code is OSS and available here:
http://github.com/jboner/skalman

Also, I'm using AspectWerkz in the Akka Transactors project now with
great success.

/Jonas

2009/9/22 Maxime Lévesque :
>
>   I'm writing a framwork that needs to intercept getter and setter calls
> on external scala classes (classes that will be written by users of the
> framework). It seems that there is no feature to do this in the language,
> is this correct ?
> I know some form of interception can be done using implicit conversions,
> but it seems that for a generic interception mechanism, one still needs an
> AOP framework like AspectWerks ...
>
>  Has anyone used an AOP framework with scala ?  Any recommendations
> on which one ?
>
>  Thanks
>

Dean Wampler
Joined: 2008-12-26,
User offline. Last seen 42 years 45 weeks ago.
Re: AOP with scala
We discuss AspectJ-Scala interop a bit in "Programming Scala".
http://programming-scala.labs.oreilly.com/ch14.html#AspectJInterop
dean

2009/9/22 Jonas Bonér <lists@jonasboner.com>
Hi.

I wrote a blog post about an AOP framework that I wrote on top of
AspectJ together with mixin composition.
http://jonasboner.com/2008/02/06/aop-style-mixin-composition-stacks-in-scala.html

The code is OSS and available here:
http://github.com/jboner/skalman

Also, I'm using AspectWerkz in the Akka Transactors project now with
great success.

/Jonas

2009/9/22 Maxime Lévesque <maxime.levesque@gmail.com>:
>
>   I'm writing a framwork that needs to intercept getter and setter calls
> on external scala classes (classes that will be written by users of the
> framework). It seems that there is no feature to do this in the language,
> is this correct ?
> I know some form of interception can be done using implicit conversions,
> but it seems that for a generic interception mechanism, one still needs an
> AOP framework like AspectWerks ...
>
>  Has anyone used an AOP framework with scala ?  Any recommendations
> on which one ?
>
>  Thanks
>



--
Jonas Bonér

twitter: @jboner
blog:    http://jonasboner.com
work:   http://crisp.se
work:   http://scalablesolutions.se
code:   http://github.com/jboner
code:   http://akkasource.org



--
Dean Wampler
coauthor of "Programming Scala" (O'Reilly)
-  http://programmingscala.com
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala
-  http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: AOP with scala

2009/9/22 Maxime Lévesque :
> Has anyone used an AOP framework with scala ?  Any recommendations
> on which one ?

The Scala IDE for Eclipse uses AspectJ extensively.

Cheers,

Miles

Dean Wampler
Joined: 2008-12-26,
User offline. Last seen 42 years 45 weeks ago.
Re: AOP with scala
Miles,
You use it to advise the Java code in the JDT, right? Do you also use it to advise Scala code? If so, any particular gotchas? In the book section, I discuss the issue of "name mangling", e.g., a "+" method has to be referred to as "$plus" in an AspectJ pointcut.
dean

2009/9/22 Miles Sabin <miles@milessabin.com>
2009/9/22 Maxime Lévesque <maxime.levesque@gmail.com>:
> Has anyone used an AOP framework with scala ?  Any recommendations
> on which one ?

The Scala IDE for Eclipse uses AspectJ extensively.

Cheers,


Miles

--
Miles Sabin
tel: +44 (0)7813 944 528
skype:  milessabin
http://www.chuusai.com/
http://twitter.com/milessabin



--
Dean Wampler
coauthor of "Programming Scala" (O'Reilly)
-  http://programmingscala.com
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala
-  http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: AOP with scala

2009/9/22 Dean Wampler :
> You use it to advise the Java code in the JDT, right?

Right. The advice typically calls out to Scala code via Java interfaces.

> Do you also use it to advise Scala code?

I don't currently because when I initially tried to mix Scala and
AspectJ in a single project ajc (the AspectJ compiler) choked on the
bytecode generated by Scala constructs which were then heavily used in
the IDE (specifically, early initializers). But I think things might
have changed since then and I know of several people who are mixing
Scala and AspectJ directly.

> If so, any particular gotchas? In the book section, I discuss the issue
> of "name mangling", e.g., a "+" method has to be referred to as "$plus"
> in an AspectJ pointcut.

This is the exact same encoding you would need to use to interact with
Scala from vanilla Java.

Cheers,

Miles

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland