<
aspect xmlns:cpp="
http://www.sdml.info/srcML/cpp"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
xmlns:util="
http://www.pnp-software.com/util"
xmlns="
http://control.ee.ethz.ch/XWeaver/AspectX"
xmlns:src="
http://www.sdml.info/srcML/src"
xsi:schemaLocation="
http://control.ee.ethz.ch/XWeaver/AspectX ../../../../src/xsd/AspectX.xsd"
name="
Trace">
<
description>
This aspect traces all methods and constructors.
The java base code is taken from the examples in the book 'AspectJ in Action'.
It exercises both weaving rules specific to Java, adding imports (advice <
code>
addImports</
code>
) and
adding members (advice <
code>
addLoggerField</
code>
).
It also uses common weaving rules for adding trace calls at the begin and end of a method and
before return statements.
<
author>
I. Birrer</
author>
</
description>
<
pointcut name="
allFunctions"
type="
src:function">
<
description>
Points to the all methods.</
description>
</
pointcut>
<
pointcut name="
allConstructors"
type="
src:constructor">
<
description>
Points to the all construtors.</
description>
</
pointcut>
<
advice type="
begin"
name="
enterFunction">
<
description>
Weave the log message at the beginning of every method.</
description>
<
pointcutRef ref="
allFunctions"
type="
src:function" />
<
codeModifier type="
codeFragment">
<
text>
if( logger.isLoggable( Level.INFO ) ) {</
text>
<
text>
logger.logp( Level.INFO, "${className}", "${functionName}", "Entering");</
text>
<
text>
}</
text>
</
codeModifier>
</
advice>
<
advice type="
end"
name="
endFunction">
<
description>
Weave the log message at the end of every method that
has no return value.</
description>
<
pointcut type="
src:function">
<
and>
<
pointcutRef ref="
allFunctions"
type="
src:function" />
<
not>
<
restriction type="
contain">
<
pointcut type="
src:return" />
</
restriction>
</
not>
</
and>
</
pointcut>
<
codeModifier type="
codeFragment">
<
text>
if( logger.isLoggable( Level.INFO ) ) {</
text>
<
text>
logger.logp( Level.INFO, "${className}", "${functionName}", "Leaving");</
text>
<
text>
}</
text>
</
codeModifier>
</
advice>
<
advice type="
before"
name="
beforeReturn">
<
description>
Weave the log message before return statement of
every method.</
description>
<
pointcut type="
src:return" />
<
codeModifier type="
codeFragment">
<
text>
if( logger.isLoggable( Level.INFO ) ) {</
text>
<
text>
logger.logp( Level.INFO, "${className}", "${functionName}", "Return" );</
text>
<
text>
}</
text>
</
codeModifier>
</
advice>
<
advice type="
add"
name="
addLoggerField">
<
description>
Adds a logger field variable to every class
that contains a construtor or a method.
</
description>
<
pointcut type="
src:class">
<
or>
<
restriction type="
contain">
<
pointcutRef ref="
allFunctions"
type="
src:function" />
</
restriction>
<
restriction type="
contain">
<
pointcutRef ref="
allConstructors"
type="
src:constructor" />
</
restriction>
</
or>
</
pointcut>
<
codeModifier type="
member">
<
text>
private static Logger logger = TraceAspectStd.getInstance().getLogger();</
text>
</
codeModifier>
</
advice>
<
advice type="
begin"
name="
enterConstructor">
<
description>
Weave the log message at the beginning of every construtor.
</
description>
<
pointcutRef ref="
allConstructors"
type="
src:constructor" />
<
codeModifier type="
codeFragment">
<
text>
if( logger.isLoggable( Level.INFO ) ) {</
text>
<
text>
logger.logp( Level.INFO, "${className}", "<init>", "Entering");</
text>
<
text>
}</
text>
</
codeModifier>
</
advice>
<
advice type="
add"
name="
addImports">
<
description>
Add the <
code>
import</
code>
statement for the <
code>
java.util.logging.*</
code>
and <
code>
aspect.*</
code>
packages.
Inclusion of those packages is necessary because the
code that is woven into the target methods
calls the method <
code>
logp()</
code>
of calss <
code>
Logger</
code>
.
</
description>
<
pointcut type="
src:unit">
<
description>
Targets all units that contains
definition of a construtor or a method.</
description>
<
restriction type="
contain">
<
pointcutRef ref="
allFunctions"
type="
src:function" />
</
restriction>
</
pointcut>
<
codeModifier type="
import">
<
text>
import java.util.logging.*;</
text>
<
text>
import aspect.*;</
text>
</
codeModifier>
</
advice>
</
aspect>
v