Wednesday, November 23, 2011

Inner type methods in multiple classes with Aspect Oriented languages


Declaring inner type methods in multiple classes with Aspect Oriented languages

Aspect Oriented Languages are not made for declare methods, but, we can use this powerful new paradigm to to this, when composite or inheritance aren't the best approach.

Andy Clement, engaged with the AspectJ project, answer me there is a good way to do this. The follow example is in AspectJ.

Declare an interface:
interface Holder {}
Make your intertype declarations on the interface:
public int Holder.getMeAnInt() {
  return 42;
}
Declaring a method on a interface, in aspects, you are providing a 'default implementation'. If you create a class Foo that implements this interface and don't implement the getMeAnInt() method explicitly, for example, the new Foo class will have the implementation declared on this interface.
The final piece of the puzzle is then to use declare parents to specify which group of types implement your interface:
declare parents: @Anno * implements Holder;
So now, any type annotated with @Anno will implement Holder and have the getMeAnInt() method.

References

  • StackOverFlow. Available in: http://stackoverflow.com/questions/3738545/aspectj-creating-innter-type-methods-in-multiple-classes Accessed in November of 2010.
  • AspectJ. Available in: http://www.eclipse.org/aspectj/ Accessed in November of 2010.
  • Wikipedia. Available in: http://en.wikipedia.org/wiki/Aspect-oriented_programming Accessed in November of 2010

No comments:

Post a Comment