Introduction

Java is an object-oriented programming language developed by Sun Microsystems that has proven itself in numerous projects. The Java Platform is hardware and operating system independent and comes in three editions: Micro Edition for PDAs and cell phones; Standard Edition for computer programs; and Enterprise Edition for distributed, transactional, and portable applications.

Thursday, May 6th, 2004 at 5:42 pm

AOP: Advice

As I already said, an advice is the code to be executed at a join point that has been selected by a pointcut. There exist three types of advices: the before, after and around advice. The before advice executes before the execution of a join point, the after advice after the execution of a join point and the around advice surrounds the join point’s execution. We will now look at each advice in turn.

Before advice:

  • Structure:
    before() : <Pointcut> {

    }
  • Example:
    before() : call(* MyClass.myMethod()) {
    System.out.println(”about to execute MyClass.myMethod()”);
    }

After advice:

  • Structure:
    after() : <Pointcut> {

    }

    after() returning(<ReturnType returnObject>) : <Pointcut> {

    }

    after() throwing(<ExceptionType exceptionObject>) : <Pointcut> {

    }

  • Example:
    after() : call(* MyClass.myMethod()) {
    System.out.println(”executed MyClass.myMethod()”);
    }

    after() returning(int result) : call(* int MyClass.myMethod()) {
    System.out.println(”The method MyClass.myMethod() returned ” + result);
    }

    after() throwing(Exception e) : call(* int MyClass.myMethod() throws Exception) {
    System.out.println(”The method MyClass.myMethod() threw the exception ” + e);
    }

  • Explanation:
    As you can see is it also possible to capture the result of a method call or the exception the method threw. You can deal with these the same way you do with normal arguments/parameters.

Around advice:

  • Explanation:
    The before and after advice are quite simple. But the around advice will need a little more explanation.
    If you do not explicitly call the method proceed() in the around advice the originally called method won’t be executed. The method will then just be bypassed. When calling proceed() you must pass the same number and types of arguments as collected by the advice. The proceed() method will also return the result of the method call.
    Another thing you have to do is specifying the return type the adviced join point has. If you are not sure what the return type of the join point is because you capture multiple different join points independent of their return type use Object. AspectJ will then automatically adjust the type based on the return type of the particular join point.
  • Structure:
    <returnType> around() : <Pointcut> {

    }
  • Example:
    int around() : call(* int MyClass.myMethod()) {
    // doSomething
    int result = proceed(); //
    // doAnotherThing
    return result;
    }
order generic viagra accutane online find cheap viagra buy generic soma price of clomid cialis online cheap viagra drug drug cialis buying generic viagra pharmacy cialis buy cheapest viagra purchase soma viagra cost order cialis viagra for sale buy cialis purchase synthroid online generic zithromax viagra overnight shipping cheapest viagra online cheapest zithromax cheap cialis tablet buy viagra from india acomplia prescription buy viagra no prescription required levitra pharmacy buying generic cialis buy zithromax without prescription lasix online viagra buy online zithromax without a prescription buy cialis from canada purchase viagra overnight delivery cialis for sale levitra online stores buy acomplia without prescription cheap generic levitra cheap propecia online cheap price viagra buy cheap viagra internet synthroid pharmacy viagra online cheap cialis uk cheap viagra from uk cheapest generic cialis online buy cialis in canada accutane pharmacy find cialis no prescription required order acomplia cheapest propecia price of acomplia buy generic cialis viagra in bangkok buy cheap propecia online buy lasix purchase levitra online find cheap viagra online buy discount viagra online clomid pharmacy clomid without a prescription buy generic acomplia soma prices cheapest soma buy zithromax lasix prescription viagra discount levitra without a prescription buy zithromax cheap acomplia pills cheap accutane cheap viagra overnight delivery soma buy viagra us