Monday, March 22, 2010

AspectJ Programming in Scala and Groovy

AspectJ is a very important and useful tool for me.

AspectJ has been very useful for me while programming in Java (programming language), and I still think it'd be useful in Groovy and Scala programming. However, some of the things that I did with AspectJ is "obsolete" with introduction of Groovy and Scala. Take a look at Java bean getter/setters: it's fun to use AspectJ to solve that problem, but the problem doesn't exist at all in Groovy and Scala. You just use native support or add i.e. @BeanProperty to Scala beans.

AspectJ can be used in Java to implement mixins... but Scala and Groovy has its own integrated way for doing mixins/traits. I saw AspectJ as an alternative programming language! And I guess I was abusing AspectJ for things not at its core goals.

I was using AspectJ for two things: micro-structural aspects (traits, etc.) and for cross-cutting concerns aka Aspect-Oriented Programming (AOP).

Now, the micro-structural aspects are what bugs me here. Traits/mixins, getter/setters, PropertyChangeListener support can be done in AspectJ, but I think it's an abuse of AOP to implement something that Java didn't have. It's not AOP, it's fixing a programming language!

You can do the above pretty easily in Scala, natively. For example, Eclipse ScalaModules enhances OSGi BundleContext with RichBundleContext.

Cross cutting concerns aka AOP is a more valid reason to use AspectJ. Things like security, logging, Data-Context-Interaction pattern, and other macro-infrastructural aspects.

I'm not sure yet how AspectJ fares with Groovy and Scala classes. I know AspectJ deals with bytecode, not Java source, so I hope AspectJ weaves Groovy/Scala classes just fine.

AspectJ aspects are also written in Java programming language, which in some way feels backwards. (come on, enhance Scala classes using Java code? Shouldn't it be the other way around?) With @AspectJ annotation style aspects, it's possible to write Groovy/Scala aspects too, though I'm not sure about its practicalness or convenience.

If you're not familiar yet with Scala I strongly recommend Programming Scala: Scalability = Functional Programming + Objects.

Do you think AOP or Scala matters for you?

1 comment:

  1. I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

    ReplyDelete