Skip to content
justin-calleja edited this page May 20, 2013 · 8 revisions

functiondef(s) are used to express similar pointcuts as those expressible in the ErlAOP project.

Consider the module [demo.erl](Modules for demo) and the "logging" aspect below (which has no type):

{aspect, [
    {id, "logging"},
    {advice, [
    	{type, X},
    	{mf, logadv, console_log},
    	{ptcuts, [{pointcut, [{functiondef, 'demo', 'add', 2}]}]}
    ]}
]}.

The following examples show the relevant transformation on demo.erl when the "logging" aspect is applied. Each application of the "logging" aspect is done with a different type for the aspect's advice to illustrate the different results.

Application of "logging" aspect with {type, before}:

add(P1, P2) ->
    TargetFunctionDetails = [demo,add@EAOP_target,[P1,P2]],
    logadv:console_log(TargetFunctionDetails),
    add@EAOP_target(P1, P2).
add@EAOP_target(N1, N2) ->
    N1 + N2.

With {type, 'after'}:

add(P1, P2) ->
    TargetFunctionDetails = [demo,add@EAOP_target,[P1,P2]],
    R = add@EAOP_target(P1, P2),
    logadv:console_log(TargetFunctionDetails, R).
add@EAOP_target(N1, N2) ->
    N1 + N2.

With {type, around}:

-export([add@EAOP_target/2]).
add(P1, P2) ->
    TargetFunctionDetails = [demo,add@EAOP_target,[P1,P2]],
    apply(logadv, console_log, [TargetFunctionDetails]).
add@EAOP_target(N1, N2) ->
    N1 + N2.

With {type, after_throw}:

add(P1, P2) ->
    TargetFunctionDetails = [demo,add@EAOP_target,[P1,P2]],
    try 
        add@EAOP_target(P1, P2) 
    catch
        ExClass:ExPattern ->
            logadv:console_log(TargetFunctionDetails,
                               {ExClass,ExPattern})
    end.
add@EAOP_target(N1, N2) ->
    N1 + N2. 

With {type, after_final}:

add(P1, P2) ->
    TargetFunctionDetails = [demo,add@EAOP_target,[P1,P2]],
    try
        add@EAOP_target(P1, P2)
    after
        logadv:console_log(TargetFunctionDetails)
    end.    
add@EAOP_target(N1, N2) ->     
    N1 + N2.

Clone this wiki locally