John Lam 1997 — 2026

AOP demo code

I promised Chris Sells that I would publish the sample code and output of my demos at VS Live!.

Here’s the Main() method of the test application:

`static int Main(string[] args) { Console.WriteLine( “Hello, World!” );

XmlDocument doc = new XmlDocument(); doc.Load( @”c:\work\codexp\weavecompiler\wc\weavemanifest.xml” );

Math math = new Math(); Console.WriteLine( Math.GetString( “Bar” ) ); Console.WriteLine( “The answer is: ” + math.Add( 3, 4 ) ); Console.WriteLine( “Another call: ” + math.Add( 1000, 2000 ) ); Console.WriteLine( “Yet another call: ” + math.Multiply( 3, 3, 3 ) );

return 0; }`

Here’s the user-defined type that was weaved in the first part of the demonstration:

public class Math { public static string GetString( string before ) { return String.Format( "Before: {0}", before ); } public int Add( int x, int y ) { return x + y; } public int Multiply( int x, int y, int z ) { return x * y; } }

The first demonstration that I showed at VS Live! weaved all of the methods of the Math class shown above. The aspect was defined in a separate assembly, and contains a single piece of advice: the Interceptor() method shown below. This method was weaved as before advice on an execution join-point (in plain English: the Interceptor method is called before any of the method bodies of the Math class methods are executed).

`public class TraceAspect { private static TraceAspect m_this;

static TraceAspect() { m_this = new TraceAspect(); } public static TraceAspect AspectOf() { return m_this; } public void Interceptor( CallContext context ) { string result = String.Format( “Intercepted: {0}( “, context.MethodName ); if( context.Parameters != null ) { for( int i = 0; i < context.Parameters.Length; i++ ) { if( i != context.Parameters.Length - 1 ) result += String.Format( “{0}, “, context.Parameters[ i ] ); else result += String.Format( “{0} )”, context.Parameters[ i ] ); } } else result += “)”; Console.WriteLine( result ); } }`

The output that was generated by the first demonstration is:

Hello, World! Intercepted: GetString( Bar ) Before: Bar Intercepted: Add( 3, 4 ) The answer is: 7 Intercepted: Add( 1000, 2000 ) Another call: 3000 Intercepted: Multiply( 3, 3, 3 ) Yet another call: 9

The second demonstration showed weaving of methods that were defined in system libraries. I weaved all of the public methods of System.Xml.XmlDocument as well as System.Xml.XmlTextReader. The weave was defined in an external XML configuration file, which was fed through a weave compiler that generated a binary file that was used by the runtime aspect weaver to determine which methods to weave. Here is the weave definition file:

<?xml version="1.0" encoding="utf-8" ?> <weave> <pointcut target="public.*System.Xml.XmlDocument.*\(" adviceType="before" advice="public void CallTrace.Trace\( CodeXP.CallContext \)" /> <pointcut target="public.*System.Xml.XmlTextReader.*\(" adviceType="before" advice="public void CallTrace.Trace\( CodeXP.CallContext \)" /> </weave>

The output of the second demonstration is:

Hello, World! Intercepted: Load( c:\work\codexp\weavecompiler\wc\weavemanifest.xml ) Intercepted: get_NameTable( ) Intercepted: get_NameTable( ) Intercepted: get_BaseURI( ) Intercepted: get_NameTable( ) Intercepted: get_NameTable( ) Intercepted: get_NameTable( ) Intercepted: Load( System.Xml.XmlValidatingReader ) ...

Folks who are really interested in the entire output that was generated by the program can look here.

This represents the current state of the AOP engine. It can weave before advice into arbitrary methods in both user and system library code. Note that the syntax of the weave definition file is very much in flux right now. I might turn it into custom attributes that are embedded into the source code of the aspects.

Next up (after an extensive cleanup of the code) is after, around and exception advice weaving at execution join-points. Then comes the more difficult case: weaving advice into call join-points.

In case you are wondering where all of this terminology comes from, I’m lifting it from AspectJ ;)

3 archived reader comments

Alex (2004-06-10)

Actually your regular syntax for the aspect along an XML DD for the activation of it looks much more like AspectWerkz (full java/XML AOP)http://aspectwerkz.codehaus.org http://docs.codehaus.org/display/AW/Hello+World

**** (2004-12-01)

You can also check some helpful info about…

**** (2004-12-01)

You can also check the sites in the field of…