Version 2

    Debugging AOP applications in Eclipse.

     

    This basically applies to debugging the aop tests in Eclipse, but the principles will be the same for any aop application.

     

    First of all set up your Eclipse workspace for jboss as shown in HowToDevelopJBossWithEclipse. Then open the "aop" project. We will use the "basic" test as the example. Its sources are under aop/src/test/org/jboss/test/aop/basic and its jboss-aop.xml is at aop/src/resources/test/basic/jboss-aop.xml

     

    If you look at the aop/build.xml file you'll find that when running the build script it compiles the test classes to aop/output/tests.classes. The rest of this example assumes that your jboss checkout is under C:\jboss-head, so you will probably have to modify the paths given.

     

    A number of different weaving options are supported by jboss aop, only the "optimized" option, which is the default in JBoss AOP 1.x will be discussed here.

     

    Debugging aopc compilation

     

    First of all we need to make sure that we have the class files for the test compiled. In a console window:

     

    C:\jboss-head\aop>build
    

    To build the aop library classes

    C:\jboss-head\aop>build clean-tests compile-test-classes
    

    To clean out the tests.classes folder and recompile the tests. If you have gone through the debugging of compilation once you will need to execute this step before attempting to debug again.

     

     

    Now that everything is in place we can setup the debugger in Eclipse.

     

    • Select Run/Debug... from Eclipse's menu

    • In the resulting dialog go to "Java Application" in the tree view, and press the "New" button

    • Give the new debug configuration the name "AOP Compiler", and select "aop" as the project and "org.jboss.aop.standalone.Compiler" as the Main class.

    • Go to the "Arguments" tab and fill in the following for

      • Program arguments: -aoppath C:\jboss-head\aop\src\resources\test\basic\jboss-aop.xml C:\jboss-head\aop\output\tests.classes\org\jboss\test\aop\basic (This tells the compiler where to find the jboss-aop.xml file and the files to be transformed)

      • VM arguments: -Djboss.aop.optimized=true This is the default weaving mode for AOP 1.x

    • Go to the "Classpath" tab and highlight "User Entries", it should contain the "aop" project.

      • Click "Advanced...", select "Add Folders" and click OK. In the resulting dialog, expand "aop" and select "docs" and "output/tests.classes". Press OK.

      • Use the "Up" button to make sure that "tests.classes" comes before "aop"

    • Press the "Apply button"

     

    You can now use the "AOP Compiler" debug configuration to step through and see how JBoss AOP does its weaving. If you don't know where to start the obvious place to put a breakpoint will be in the main method of org.jboss.aop.standalone.Compiler

     

     

    Debugging AOP at runtime

     

    This should give you an idea of what JBoss AOP does at runtime.

     

    Compile time woven classes

     

    This assumes that your classes have been woven at compile time, either by having gone through the debugging of the compiler in the previous section, or by running the tests from the command line. To run the tests from the command line:

    C:\jboss-head\aop>build clean-tests compile-test-classes precompiled-tests
    

    This will compile the classes, aopc them, and run the tests. The classes will have been instrumented and you can now debug them.

     

    First of all it is a good idea to get a vague idea of how the hooks for AOP is built in. Use any decompiler (I use Cavaj). If you look at any of the instrumented classes under output/tests.classes you'll see that it has an introduced field that interacts with the AOP library

     

        private static final ClassAdvisor aop$classAdvisor$aop = AspectManager.instance().getAdvisor(Class.forName("org.jboss.test.aop.basic.POJO"));
    

     

    The AspectManager is the central place for configuration of bindings an pointcuts, and the ClassAdvisor, which is created when the class is first loaded contains the interceptor bindings etc. for each of the joinpoints in the chain. The ClassAdvisor also populated the new aop$XXXInfo fields in the class, which contain the advisor chains.

     

    For advised methods, you should find that where you previously had one methods you now have two. One is a copy of the original method (with a new name) and the original method contains hooks for obtaining the interceptors. If there are some interceptors we create an invocation with the applicable interceptors and invoke the chain. Otherwise, we simply call the copy of the method (which contains the original code)

     

    To be able to debug an AOP application, we just need to tell JBoss AOP where to find the jboss-aop.xml file. To set this up in Eclipse's debugger:

     

    • Select Run/Debug...} from Eclipse's menu

    • In the resulting dialog go to "Java Application" in the tree view, and press the "New" button

    • Give the new debug configuration the name "Basic CompileTime", and select "aop" as the project and "org.jboss.test.aop.basic.AOPTester" as the Main class.

    • Go to the "Arguments" tab and fill in the following for

      • VM arguments: -Djboss.aop.path=C:\jboss-head\aop\src\resources\test\implementz\jboss-aop.xml

    • Go to the "Classpath" tab and highlight "User Entries", it should contain the "aop" project.

      • Click "Advanced...", select "Add Folders" and click OK. In the resulting dialog, expand "aop" and select "docs" and "output/tests.classes". Press OK.

      • Use the "Up" button to make sure that "tests.classes" comes before "aop"

    • Press the "Apply button"

     

    You can now use the "Basic CompileTime" debug configuration to step through and see how JBoss AOP creates the interceptor chains etc.. Obvious places to put breakpoints if you're just starting out would be in:

    -org.jboss.aop.AspectManager.instance()

    -org.jboss.aop.AspectManager.getAdvisor(java.lang.String)

     

    Note that it is not possible to view the source for all the generated code when you step through the code!