Announcement Announcement Module
Collapse
No announcement yet.
aop:before reference to target Method, simply syntax Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • aop:before reference to target Method, simply syntax

    Hello,
    My requirements are quite simple,
    From an aop:before advice, I need to access the intercepted Method object, fill a map with its parameter Names -> parameter values and invoke a non intercepted method on the target (intercepted) object instance.
    I'm using Paranamer to retrieve parameter names with compile time generation, so that's a no issue. but, naturally I need a reference to the intercepted method "java.lang.reflect.Method".
    Browsing the syntax of JoinPoint, ProceedingJoinPoint (in aop:around) and even MethodInvocationProceedingJoinPoint has no "clear" reference.

    appreciate any pointers.
    best regards,
    Last edited by maahelal; Jan 27th, 2010, 09:25 AM. Reason: marking as solved

  • #2
    Just one question... Why?!

    Why call the method yourself? It is already called for you by the aop framework...

    All the information needed is in the JoinPoint and using reflection you could obtain the Method from the target class and use it to invoke it... But I still don't really see why on earth you would want to do that, it basically means invoke the method twice...

    Comment


    • #3
      Simple enough explanation

      Welcome Marten,
      I thought that my question was clearer than it turned out to be.
      I appreciate you taking the time to reply, but I think a clearer description would help solve the mystery.

      I've already said that in the intercepting implementation I'll be calling another "NON intercepted" method on the target object. so as you can see I already cater for your point and wouldn't be calling the same method twice.

      While my final solution varied from my question a bit, the reason is quite simple.
      I was developing a generic business rules executor, that advices all the business level methods, extracts their parameter data (name-> value) pairs using paranamer; hence I need a reference to the java.lang.reflect.Method instance, prepare a simplistic execution context and invoke a "Non advised" generic business rule execution method. the only variation I did from my initial description is to extract the generic business execution logic to the advice itself. While it's cleaner this way, I think, it's by no means a replacement for needing to get a reference to the target(advised, intercepted) method.

      I already solved my problem and I ended up using the CGLIB based Class proxy-ing feature.

      Here's what my current Spring configuration look like
      Code:
              <!-- Generic Business Rule Executor -->
      	<bean id="businessRuler" class="[base package].service.aop.BusinessRuleExecutionAdvice" >
      		<!-- initialization of business rule base goes here -->
      	</bean>
              <!-- interception configuration using CGLib based proxying, BeanNameAutoProxyCreartor was used for simplicity and brevity -->
      	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
                      <!-- had to turn this on so as paranamer gets the parameter info properly -->
      		<property name="proxyTargetClass" value="true"></property>
      	  <property name="beanNames">
      		  <value>
      		  	personService,..... (list of all advisable service references goes here)
      		  </value>
      	  </property>
      	  <property name="interceptorNames">
      	    <list>
      	      <value>businessRuler</value>
      	    </list>
      	  </property>
      	</bean>
      And it works like a charm, did the demo, people LOVED it...

      It's no longer urgent for my particular case, but I think it's still of value to future thread readers to add explanation on how to actually get java.lang.reflect.Method from a JoinPoint reference, If you have the time of course.
      Best regards,
      Mohamed...

      Comment


      • #4
        Well you want to call another method so what is the point of getting the method from the joinpoint, that will give you the method which is intercepted.

        But the joinpoint has a Signature, which in case of a method interception can be cast to a MethodSignature which has the actual method.

        Code:
        Signature sig = jp.getSignature();
        if (sig instanceof MethodSignature) {
          Method m = ((MethodSignature) sig).getMethod();
        }

        Comment


        • #5
          Thanks

          Well, thanks for the snippet. mystery solved .
          regarding your question

          so what is the point of getting the method from the joinpoint, that will give you the method which is intercepted.
          I think it's only logical that I would need the intercepted method to construct the business rule execution context!

          Parameter names -> parameter values make up the necessary context a given business rule needs to validate a given business case.

          I do feel funny about saying the exact same statement for the third time, am I saying something fundamentally incorrect and totally overseeing it time after time ?

          I really appreciate your input,
          and thanks again..
          Mohamed

          Comment

          Working...
          X