Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Java Books Media Programming Book Reviews IT Technology

Aspect-Oriented Programming with AspectJ 496

Verity Stob writes "There is a turning point in the emergence of a programming methodology. It doesn't matter how big and popular the website is, nor how many papers have been published in the ACM journals or development magazines, nor even whether the first conferences have been a sell-out. A methodology hasn't made really made it until somebody has published a Proper Book. With Aspect-Oriented Programming with AspectJ author Ivan Kiselev is bidding to drag AOP into the mainstream. He is motivated, he says in his introduction, by the recollection of the 25 odd years it took for the object-oriented concept to spread from its Simula origins in frosty Norway to being the everyday tool of Joe Coder. He aims to prevent this delay happening to AOP." Read on for Verity Stob's review of Kiselev's book.
Aspect-Oriented Programming with AspectJ
author Ivan Kiselev
pages 274
publisher SAMS
rating Excellent
reviewer Verity Stob
ISBN 0672324105
summary Introduction to a new programming technique using an extension to Java

He has divided the book into four parts. Part I provides a brief sketch of AOP and introduces its concepts. AOP builds on OOP, asserting that we need a new programming entity called, wait for it, an aspect. Mr Kiselev's explanation of aspects reminded me of that bit in The Hitchhiker's Guide to the Galaxy when the planet Golgafrincham divided its population into A types (who were the leaders, the scientists and the great artists), the C types (who were the people who did all the actual making of things and doing of things), and the B types, who comprised everybody left over: telephone sanitizers, advertising account executives and hairdressers. As I understand Mr Kiselev, the AOP view of things is that objects and classes (A type thinkers) and low-level procedures and APIs (C type doers) can be nicely encapsulated using traditional components. But aspects, software's little hairdressers, get their fingers into everything, and until now there has been no way to encapsulate them. This of course is what AOP in general and specifically the AspectJ superset of the Java language set out to do.

AspectJ's eponymous aspects are constructs not unlike ordinary classes. Mr Kiselev has not resisted the temptation to make an aspect Hello World example, and it looks reassuringly so-whatish:

package intro;

import java.io.*;

public aspect HelloWorldA
{
public static void main(String args[])
{
System.out.println(Hello, world!);
}
}

Mr Kiselev then lays out his stall of New Things. A join point is any point in execution flow that AspectJ can identify and -- to get slightly ahead of ourselves -- execute some extra code. The most frequently used kind of join point being the call to a method. Pointcuts specify collections of join points; as a regular expression is to an instance of matched text, so a pointcut is to a matching join point. An advice (with horrid plural 'advices') is the code to be executed when a given pointcut is matched. If you are familiar with Eiffel's pre- and post-conditions, then you'll understand if I say that it is common for advices to run in the same way, topping and/or tailing the execution of a method. The differences are that aspects are specified from outside the method without touching the method or its class's code, and that aspects can be applied to multiple methods in one go. Mr Kiselev concludes this section of the book with a few simplistic examples of 'here is class A, here is class B' kind.

In Part II Mr Kiselev rolls up his sleeves and takes us through an extended, realistic example. I did wonder if perhaps it weren't a wee bit too realistic, as it is a miniature website application for news story submission and reading -- sort of Slashdot Ultralite -- all done using JSP and a MySQL database. Just explaining this setup, without even using any AspectJ, consumes a 15-page chapter. Since I am a C++ programmer who has not had any contact with JSP, I was initially anxious that I might not be able to follow this. However, recalling that www.[name withheld].com, the clumsiest, ugliest corporate website on the Internet, is programmed in JSP, I reasoned that if the dolts that programmed that site could understand JSP then it couldn't be very hard. So it proved.

The first example comprises adding password protection to the application. This is achieved by adding an advice that intercepts calls to doStartTag() methods. The advice can test if the user is logged in and, if he isn't, throw an exception that will dump him back at the login page. (Who says exceptions aren't 21st century gotos?) At this point Mr Kiselev admits that the cute 10-line implementation that he initially shows is in reality a non-starter; for one thing not all pages that must be secured define doStartTag() methods, for another the aspect can't reach an instance variable it needs to read because it is declared in protected scope. The second problem is easily overcome. AOP offers a mechanism by which extra classes can be bodged ('introduced' is the preferred verb in the AOP community) into the hierarchy as parents of existing classes. He uses this to add an accessor method for the field in question. The other problem is not so neatly smothered, and it is somewhat ruefully that Mr Kiselev produces his final, two-page solution. But I think that it is greatly to Mr K's credit that he does this - it tastes like programming in the real world as I have experienced it.

For the rest of Part II, Mr K demonstrates other applications of AOP using the AspectNews code. This includes Eiffelish design-by-contract stuff, improved exception handling, various debugging and tuning techniques (specifically logging, tracing and profiling) and a chapter on runtime improvements - stream buffering, database connection pooling and result caching - which show the AOP way to do things, usually where I would expect to be putting in proxy classes.

In part III we get down and dirty with the AspectJ language. This is the part where the book explains the obscure stuff: how to make a pointcut that picks up object preinitialization, or make an advice that goes off only when you are exiting a method on the back of an exception. I skimmed this bit - I guess it will become vital when I start using AspectJ in earnest. It looked good and clear on a flick through. A brief part IV contains some patterns, to give one a start when engaging AspectJ in earnest. Apparently it is horribly easy to create infinitely recursive situations, so if you here a faint popping sound from your machine it will be the stack colliding with the heap. There are seven appendices, supplying such things as a summary of the API in AspectJ's packages and hints on obtaining and using the Open Source supplementary tools mentioned in the book (Tomcat JSP container, MySQL database and Ant make replacement). AspectJ itself, now escaped from Xerox PARC, can be downloaded from the Eclipse website.

Complaints? None really. Oh all right, here's a nitpicklette because it's you: at page 75 Mr Kiselev adopts the irritating Internet habit of writing 'loosing' when he means 'losing'. Note to publisher SAMS proofreaders: do I win 25 cents?

For the rest, this is a lucid and readable book that describes the Next Big Methodology. I'm a bit alarmed at the prospect of squeezing new actions into the cracks of existing code, but I dare say I'll grow to love it.

A word of warning to the eager: since this technology is currently implemented as a species of preprocessor that relies on having all the source code available at once, so it is rather slow and probably isn't going into production shops for a while. There again, I seem to remember the comparable Cfront C++ compiler doing rather well, before we had platform-native C++ compilers.

And to the sceptics: if you think you can ignore AOP, don't forget the fate of the A and C type inhabitants of Golgafrincham, who having sent their B type telephone sanitizers into exile were all wiped out by a germ caught from a particularly dirty telephone.


You can purchase Aspect-Oriented Programming with AspectJ from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

This discussion has been archived. No new comments can be posted.

Aspect-Oriented Programming with AspectJ

Comments Filter:
  • by borgdows ( 599861 ) on Tuesday March 04, 2003 @12:23PM (#5433311)
    Microsoft-oriented development book at Microsoft Press!

    This book contains every programming concepts and practices to avoid like hell!
  • Fascinating (Score:5, Funny)

    by Limburgher ( 523006 ) on Tuesday March 04, 2003 @12:25PM (#5433325) Homepage Journal
    I love that aspects seems to provide alternative techniques without loosing any ease-of-use, coing-wise. I was afraid that in switching to this new method I'd loose some functionality, or maybe loose some speed, but so far so good. Nothing to loose any sleep over. :)P
  • Bad code (Score:4, Funny)

    by Anonymous Coward on Tuesday March 04, 2003 @12:29PM (#5433357)
    System.out.println(Hello, world!);

    Where are the quotes?
    I dont trust this guy if he cant write a HelloWorld app.
  • How true (Score:5, Funny)

    by arvindn ( 542080 ) on Tuesday March 04, 2003 @12:44PM (#5433479) Homepage Journal
    It doesn't matter how big and popular the website is.

    Yup. Nothing can save it from slashdot.

  • by Anonymous Coward on Tuesday March 04, 2003 @12:45PM (#5433493)
    ((((((((((((((((((((((( I ((((((((((( absolutely ))))))))))) agree )))))))))))))))))))))))

  • by Pinky3 ( 22411 ) on Tuesday March 04, 2003 @12:45PM (#5433494) Homepage
    I almost loost it when i read that someone modded this interesting instead of funny!
  • by WebfishUK ( 249858 ) on Tuesday March 04, 2003 @01:07PM (#5433648)


    Instead of looking for radically new approaches in computer language development why doesn't someone introduce more colour into standard C. For instance we have if and while, how about adding verbs such as when, where or how.

    Taking it further we might like to introduce more polite language into code such WouldYouBeSoKindAsTo or WhenYouAreReady. Imagine the code you could write...

    when (time == "morning")
    WouldYouBeSoKindAsTo (turn_on_alarm);


    Alternatively we could convert C from English into, say, German. So if would become wenn etc. You could even mix nationalities. For instance if you wanted to be very forceful about a particular if statement you could use the German. If you wanted to seduce the code you might use the French.

    Come on all it takes is some imagination. (Alternatively we could all just start using befunge [catseye.mb.ca]!)

  • by mvw ( 2916 ) on Tuesday March 04, 2003 @01:12PM (#5433676) Journal
    the gimmick dejour

    I don't know if I am just becoming old and closed-minded, or if there were too many methodologies-de-jour in the last years.

    • OOP was a nice addition to structured programming, but already with UML I had my pains, because of course, it was closely connected to buying the right tools (Rose, Together..) and too much hyped for my taste.

    • Extreme Programming seems to have lost its impetus, or am I just not up to date? :)

    • AOP has great potential to reintroduce spaghetti code, something fought by structured programming in the 70ies. I believe it will lead to truely incomprehensible control flow without the right killer tool/killer IDE. From all the candidates either I grok it the least or my feeling is right that it is the gimmick-est.

    And please note that some schools of programming, like some disciples of the school of functional programming think that even OOP was snake oil!

    Man I wish Microsoft or Sun would hype Visual OCaml or Erlang2EE or Prolog.NET for a change. That would be refreshing.

    Regards,
    Marc

  • by Anonymous Coward on Tuesday March 04, 2003 @01:44PM (#5433962)
    My cousin's friend tried AOP, and lost his arm. Not just his hand, but the whole fucken arm. Just gone like that!
  • by Mysticalfruit ( 533341 ) on Tuesday March 04, 2003 @02:33PM (#5434450) Homepage Journal
    I work my ass off and write a program and when I send it to my boss he's always sitting infront of his monitor at a different angle and it looks and works totally different.

Stellar rays prove fibbing never pays. Embezzlement is another matter.

Working...