Testing a navigation

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Here is a two-file application called HelloWorld - with an Aspect which prints "Tracing" to show that the aspect has been applied. Load into Eclipse with the AJDT and you can see the aspect getting applied. For this example the AjLib jar is extracted into the project also.

HellowWorld.java:

package com.AjlibTest;
public class HelloWorld
{
    public static void main(String[] args)
    {
        DummyObject dummyObject = new DummyObject();
        dummyObject.someMethod(5);
    }
}

DummyObject.java:

package com.AjlibTest;
public class DummyObject
{
    public int someMethod(int i)
    {
        return 7;
    }
}

TestTraceAspect.aj:
package com.AjlibTest;
import org.codehaus.ajlib.util.tracing.ExecutionTracing;
public aspect TestTraceAspect extends ExecutionTracing

Unknown macro: {{color}

    public pointcut scope() : within(DummyObject);
    before(): scope()
    {
        System.out.println("Tracing");
    }
}

With AjLib extracted along with the src into a single eclipse project for the purposes of this example, the resulting directory strructure looks like this:
org\codehaus\ajlib\util\tracing\AbstractTracing.aj
org\codehaus\ajlib\util\tracing\BasicTraceFormatter.aj
org\codehaus\ajlib\util\tracing\CustomTraceable.aj
org\codehaus\ajlib\util\tracing\ExecutionTracing.aj
org\codehaus\ajlib\util\tracing\ExtensibleTracing.aj
org\codehaus\ajlib\util\tracing\GuardTracing.aj
org\codehaus\ajlib\util\tracing\Log4jExecutionTracing.aj
org\codehaus\ajlib\util\tracing\Log4jTracer.aj
org\codehaus\ajlib\util\tracing\NotTraced.aj
org\codehaus\ajlib\util\tracing\PerThreadTracer.aj
org\codehaus\ajlib\util\tracing\PrintStreamTracer.aj
org\codehaus\ajlib\util\tracing\StandardPointcuts.aj
org\codehaus\ajlib\util\tracing\TraceFormatter.aj
org\codehaus\ajlib\util\tracing\Tracer.aj
org\codehaus\ajlib\util\tracing\XMLTraceFormatter.aj
com\AjlibTest\TestTraceAspect.aj
com\AjlibTest\DummyObject.java
com\AjlibTest\HelloWorld.java
This is the output produced to the console from running this example:
Tracing
Tracing
Tracing
Tracing
trace enter: com.AjlibTest.DummyObject() , this: com.AjlibTest.DummyObject@7a84e4
trace exit: com.AjlibTest.DummyObject()
Tracing
trace enter: int com.AjlibTest.DummyObject.someMethod(int) , this: com.AjlibTest.DummyObject@7a84e4, args: arg 0 = 5
trace exit: int com.AjlibTest.DummyObject.someMethod(int), return = 7

  • No labels