Announcement Announcement Module
Collapse
No announcement yet.
capturing nested method calls Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • capturing nested method calls

    how do you capture nested call the method signatures defined in an aspect's poincuts?

    i created a simple program to demonstrate my problem. i am new to spring and i know they do dynamic weaving vs static weaving, but i think this should be possible.

    my pointcut and around advice:

    Code:
    @Around("execution(* mpurdy_test.aop.TestBean.*(..))")
    {
       String targetName   = pjp.getTarget().getClass().getName();
       String methodName = pjp.getSignature().getName();
    
       if(this.bLog)
          System.out.println("before executing " + targetName + "." + methodName);
    
       Object ret = pjp.proceed();
    
       //store a hashmap all all methods and its cardinality 
       this.updateMethodStats(targetName + "." + methodName);
    
       return ret;
    
    }
    the interface is:
    Code:
    public interface Test
    {
       public int getX();
       public int getY();
       public void setX(int x);
       public void setY(int y);
    
       public int add();
       public int subtract();
       public int multiply();
       public int divide();
       public int power();
    
    }
    the TestBean class:

    Code:
    public class TestBean implements Test
    {
       private int x;
       private int y;
       
       public TestBean() { ... }
       public TestBean(int x, int y) { this.setX(x); this.setY(y); }
    
       //code cut for brevity
    
       public int add() { return this.x + this.y; }
    
       //code cut for brevity
    
       public void setX(int x) { this.x = x; }
       public void setY(int y)
       {
           //this private method doesnt get captured
           this.doSomethingPrivate();
           this.y = y;
    
       }
       
       //code cut for brevity
    
       private void doSomethingPrivate()
       {
          for(int i = 0; i < 100; i++)
          {
             System.out.println("*** calling doSomethingPrivate - aspect doesnt catch this! ");
             //this call the add doesnt get captured
             this.add();      
          }
       }
    
    
    }
    the app entry point:

    Code:
    public static void main(String[] args)
    {
       ApplicationContext ac = new ClassPath...("applicationContext.xml");
       Test test = (Test)ac.getBean("test");
       TestAspect testAspect = (TestAspect)ac.getBean("testAspect");
    
       System.out.println("test: " + test.toString() );
       System.out.println("test: add( " + test.getX + ", " + test.getY() + ") = " + test.add());
       System.out.println("test: substract(" + ...
       System.out.println("test: multiply(" + ...
       System.out.println("test: divide(" + ...
       System.out.println("test: power(" + ...
    
       if(testAspect != null)
       {
          HashMap<String, Integer> methodStats = testAspect.getMethodStats();
          
          if(!methodStats.isEmpty())
          {
    
              //get the keys
              //print them out to the screen
    
          }
    
    
       }
    
    }

    the output
    Code:
    test: {10, 5 }
    test.add(10, 5)       = 15
    test.subtract(10, 5)  = 5
    test.mult(10, 5)      = 50
    test.divide(10, 5)    = 2
    test.power(10, 5      = 100000
    
    mpurdy_test.aop.TestBean.add         1
    mpurdy_test.aop.TestBean.subtract    1
    mpurdy_test.aop.TestBean.multiply    1
    mpurdy_test.aop.TestBean.divide      1
    mpurdy_test.aop.TestBean.power       1
    mpurdy_test.aop.TestBean.getX        5
    mpurdy_test.aop.TestBean.getY        5

    here is the applicationContext.xml:
    Code:
    <aop:aspectj-autoproxy />
    
    <bean id="test" class="mpurdy_test.aop.TestBean">
       <constructor-arg value="10" />
       <constructor-arg value="5"  />
    </bean>
    
    <bean id="testAspect" class="mpurdy_test.aop.TestAspect">
       <constructor-arg value="false" />
    </bean>
    question:
    the setX and setY should be there with a cardinality of 1
    the add should have a cardinality of 101

    how do you capture nested call the method signatures defined in an aspect's poincuts?
    Last edited by mpurdy1973; Oct 22nd, 2009, 10:21 AM. Reason: forgot the add applicationContext.xml

  • #2
    found a solution

    http://denis-zhdanov.blogspot.com/20...pects-are.html

    you need to do "aspectj weaving" in spring.

    i dont know how to do that yet... but i will do some research...

    i just bought the new AspectJ in Action book 2nd edition (sept 2009) ... a great resource!

    Comment


    • #3
      complete solution using aspectj weaving with spring

      this link answers all my questions - wanted to share it with the community.

      http://denis-zhdanov.blogspot.com/2009/08/weaving-with-aspectj.html

      Comment


      • #4
        You are correct. You need AspectJ for this situation. Spring-based proxy aop will not work on private methods.

        Comment


        • #5
          link was bad...here is the correct one

          Originally posted by mpurdy1973 View Post
          this link answers all my questions - wanted to share it with the community.

          http://denis-zhdanov.blogspot.com/2009/08/weaving-with-aspectj.html

          http://denis-zhdanov.blogspot.com/20...h-aspectj.html

          Comment

          Working...
          X