Slashdot Mirror


Aspect-Oriented Programming with AspectJ

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.

7 of 426 comments (clear)

  1. Have to do it... by Angry+White+Guy · · Score: -1, Offtopic

    First Post

    --
    You think that I'm crazy, you should see this guy!
  2. dang by Anonymous Coward · · Score: -1, Offtopic

    not fp... :(

  3. Smelly by Anonymous Coward · · Score: -1, Offtopic

    Joe Coder

    Joke Odour

  4. Re:Hmm by Anonymous Coward · · Score: -1, Offtopic

    Good point. I hadn't considered that aspect.

  5. Mandatory karma whoring by Anonymous Coward · · Score: -1, Offtopic

    Kudos, my man. Kudos.

  6. Quite upset Ivan is getting this publicity by Anonymous Coward · · Score: -1, Offtopic

    Writing this post stems from a desperation to be heard, if not by a court of law, then by a court of public opinion. Let me cut to the chase: If you're interested in the finagling, double-dealing, chicanery, cheating, cajolery, cunning, rascality, and abject villainy by which Ivan Kiselev may destroy any resistance by channeling it into ineffective paths by next weekend, then you'll want to consider the following very carefully. You'll especially want to consider that I am tired of hearing or reading that everyone with a different set of beliefs from Ivan's is going to get a one-way ticket to Hell. You know that that is simply not true. Because we continue to share a common, albeit abused, atmospheric envelope, I, for one, find him the most morally questionable person in the world. To cap that off, he is unquestionably up to something. I don't know exactly what, but if you've read any of the scornful slop that Ivan has concocted, you'll really recall Ivan's description of his plan to turn over our country to raving, sanctimonious slackers. If you haven't read any of it, well, all you really need to know is that Ivan is the picture of the insane person on the street, babbling to a tree, a wall, or a cloud, which cannot and does not respond to his assertions.

    Even if we accepted Ivan's theories, so what? Does that mean that laws are meant to be broken? Of course not. Ivan's views are a mere cavil, a mere scarecrow, one of the last shifts of a desperate and dying cause.

    Mere association tends to lend credence to Ivan's slaves because people assume they can't be that bad if a respected person is associated with them. Well, that's getting away from my main topic, which is that my goal is to hinder the power of headstrong slaves to fashion like him. I might not be successful at achieving that goal, but I unmistakably do have to try. If Ivan is going to talk about higher standards, then he needs to live by those higher standards. We have a number of problems for which he bears most of the responsibility. It is tempting to look for simple solutions to that problem, but there are no simple solutions. He wants all of us to believe that doing the fashionable thing is more important than life or liberty. That's why he sponsors brainwashing in the schools, brainwashing by the government, brainwashing statements made to us by politicians, entertainers, and sports stars, and brainwashing by the big advertisers and the news media. Things that you or I might regard as snippy or ignominious might be considered by Ivan's toadies as an article of faith, a philosophical conviction, a political opinion, or even an innocuous form of entertainment. He vehemently denies that, of course. But he obviously would, because he is reluctant to resolve problems. He always just looks the other way and hopes no one will notice that I overheard one of his apologists say, "We have no reason to be fearful about the criminally violent trends in our society today and over the past ten to fifteen years." This quotation demonstrates the power of language, as it epitomizes the "us/them" dichotomy within hegemonic discourse. As for me, I prefer to use language to put the kibosh on Ivan's viewpoints.

    I am sorry to have to put this so bluntly, but the tone of Ivan's imprecations is eerily reminiscent of that of acrimonious used-car salesmen of the late 1940s, in the sense that remaining silent and inactive in the face of Ivan's propositions negates our duty as civilized members of the community. And that's why I'm writing this letter; this is my manifesto, if you will, on how to fight scurrility and slander. There's no way I can do that alone, and there's no way I can do it without first stating that when I'm through with him, he'll think twice before attempting to befuddle the public and make sin seem like merely a sophisticated fashion. I know how most of you feel. This implies that it would be wrong to imply that Ivan is involved in some kind of conspiracy to make individuals indifferent to the survival of their families. It would be wrong because his threats are far beyond the conspiracy stage. Not only that, but he has spent untold hours trying to evade responsibility. During that time, did it ever once occur to him that his pranks are a cancer that is slowly eating away at our flesh? The answer is quite simple. I already listed several possibilities, but because Ivan lacks the ability to remember beyond the last two seconds of his life, I will restate what I said before, for his sake: Even when he isn't lying, he's using facts, emphasizing facts, bearing down on facts, sliding off facts, quietly ignoring facts, and, above all, interpreting facts in a way that will enable him to prevent the real problems from being solved. There's no point in arguing with Ivan. And if that seems like a modest claim, I disagree. It's the most radical claim of all.

    He is terrified that there might be an absolute reality outside himself, a reality that is what it is, regardless of his wishes, theories, hopes, daydreams, or decrees. I don't mean to imply that his real enmity against us comes through in his anecdotes, which Ivan uses to restructure the social, political, and economic relationships throughout the entire society, but it's true, nonetheless. There are some simple truths in this world. First, he backstabs his spokesmen. Second, those who believe that his bromides are Right with a capital R are either naive or deliberately misled. And finally, he managed to convince a bunch of refractory survivalists to help him impose ideology, control thought, and punish virtually any behavior he disapproves of. What was the quid pro quo there? A complete answer to that question would take more space than I can afford, so I'll have to give you a simplified answer. For starters, his prevarications are based on two fundamental errors. They assume that arriving at a true state of comprehension is too difficult and/or time-consuming. And they promote the mistaken idea that he is beyond reproach. It is high time for someone to argue about Ivan Kiselev's opuscula. Will that someone be you?

  7. Re:So, what is this? by Anonymous Coward · · Score: -1, Offtopic

    My COCK is SO HUGE! Wemon FAINT when the see it. YOU TOO CAN HAVE A HUGE MAMBER! My patented suction device will not only LENGHTEN YOUR COCK, it will MAKE YOU CLIMAX! Simply place in dishwasher to clean. JUST IMAGEN your BIG LONG COCK stroking IN AND OUT of a HOT WET SNATCH! Feel your BALLS slapping the BUSH as you STROKE!!!! PULL it OUT and CREAM all over her TUMMY! Your SPERM will flow like a RIVER into the VIRGEN PUSSY as the WEMEN run screaming from the sight of your BIG SHLONG erupting like a VOLCANO. Your BALLS will GROW to be like WATERMELLONS full of JUICE that runs down the MOUTH of the HOT BITCHES with POUTING LIPS of LOVE. They will invite your LOVE TOOL into there BLAST FURNACE of CARNAL PLESURE to pump its HOT LAVA of MAN JUICE. Just the SIGHT of your JACK HAMMER OF LOVE will make them WET THERE CROTCHLESS PANTIES! Your new STEAMING CROTCH ROCKET OF SIN will be like the PIED PIPER of YOUNG VIRGEN CUNT dripping with your MILKSHAKE of DESIRE! Just wipe your new PENIS SUCTION TUBE clean and PLACE in DISH WASHER. Your SPERM LOAD will SPIRT like a ROCKET of CREAMY WHITE LITTLE VIRGEN GIRL CANDY CAIN. JUST imagen your LOVE BEAST slapping that VIRGEN SNATCH BURGER like a RABBI at the HEELS of the TEMPLE OF SIN! CAN THIS BE TRUE???? YES! Just two WEEKS of suction therapy and your MASTER BLASTER will need you to have a THIRD PANTS LEG, they will call you TRIPOD as they LICK your NUTS and SUCK your COCK! Your JIZZ LOAD will DRIBBLE out the taut LIPS OF LOVE like an ocean of LIFE CREAM! You will CHOKE YOUR CHICKEN as the VIRGEN GIRLS take you ALL THE WAY IN as you SHOOT YOUR CUM in there TUMMY! The ANAL PLEASURE TUBE WILL SLIDE LIKE A WET little girl virgen SNATCH steming from the FRICTION of your ONE EYED GREAT DANE! Your TOOL OF LOVE will FEED all the little SLUT GIRLS that hang out at the BUS STATION looking for COCK! Can this be TRUE? But YES! You will shout OH GOD BABY BITCH! as you ROCKET your STEAMING LAVA down the THROAT of the BITCHES! They will invite your LOVE TOOL into there BLAST FURNACE of CARNAL PLESURE to pump its HOT LAVA of MAN JUICE. Just the SIGHT of your JACK HAMMER OF LOVE will make them WET THERE CROTCHLESS PANTIES! Your new STEAMING CROTCH ROCKET OF SIN will be like the PIED PIPER of YOUNG VIRGEN CUNT dripping with your MILKSHAKE of DESIRE! Just wipe your new PENIS SUCTION TUBE clean and PLACE in DISH WASHER. Your SPERM LOAD will SPIRT like a ROCKET of CREAMY WHITE LITTLE VIRGEN GIRL CANDY CAIN. JUST imagen your LOVE BEAST slapping that VIRGEN SNATCH BURGER like a RABBI at the HEELS of the TEMPLE OF SIN! CAN THIS BE TRUE???? YES! Just two WEEKS of suction therapy and your MASTER BLASTER will need you to have a THIRD PANTS LEG, they will call you TRIPOD as they LICK your NUTS and SUCK your COCK! Your JIZZ LOAD will DRIBBLE out the taut LIPS OF LOVE like an ocean of LIFE CREAM! You will CHOKE YOUR CHICKEN as the VIRGEN GIRLS take you ALL THE WAY IN as you SHOOT YOUR CUM in there TUMMY! The ANAL PLEASURE TUBE WILL SLIDE LIKE A WET little girl virgen SNATCH steming from the FRICTION of your ONE EYED GREAT DANE! Your TOOL OF LOVE will FEED all the little SLUT GIRLS that hang out at the BUS STATION looking for COCK! Can this be TRUE? But YES! You will shout OH GOD BABY BITCH! as you ROCKET your STEAMING LAVA down the THROAT of the BITCHES! JUST imagen your LOVE BEAST slapping that VIRGEN SNATCH BURGER like a RABBI at the HEELS of the TEMPLE OF SIN! CAN THIS BE TRUE???? YES! Just two WEEKS of suction therapy and your MASTER BLASTER will need you to have a THIRD PANTS LEG, they will call you TRIPOD as they LICK your NUTS and SUCK your COCK! Your JIZZ LOAD will DRIBBLE out the taut LIPS OF LOVE like an ocean of LIFE CREAM! You will CHOKE YOUR CHICKEN as the VIRGEN GIRLS take you ALL THE WAY IN as you SHOOT YOUR CUM in there TUMMY! The ANAL PLEASURE TUBE WILL SLIDE LIKE A WET little girl virgen SNATCH steming from the FRICTION of your ONE EYED GREAT DANE! Your TOOL OF LOVE will FEED all the little SLUT GIRLS that hang out at the BUS STATION looking for COCK! Can this be TRUE? But YES! You will shout OH GOD BABY BITCH! as you ROCKET your STEAMING LAVA down the THROAT of the BITCHES! My patented suction device will not only LENGHTEN YOUR COCK, it will MAKE YOU CLIMAX! Simply place in dishwasher to clean. JUST IMAGEN your BIG LONG COCK stroking IN AND OUT of a HOT WET SNATCH! Feel your BALLS slapping the BUSH as you STROKE!!!! PULL it OUT and CREAM all over her TUMMY!