2 Replies Latest reply on Sep 19, 2011 11:49 PM by flavia.rainone

    Constructor Advice

    codefox

      I've been writing AOP against actions in my ESB pipeline (as we all now know ) and while writing pointcuts against the process methods works fine, I have a need to have advice around the constructors of the actions so that I can populate some metadata.  The problem is that none of the pointcuts I've written seem to do anything.  I've tried various things since it seemed that the ESB might be instantiating the actions via reflection but that didn't seem to work either.  When I wrote advice against the class ActionProcessingPipeline just to see if it would grab any constructor, I got no output from my constructor advice.  Here is my current code:

       

       

      jboss-aop.xml:

      {code:xml}<pointcut expr="execution(public com.amentra.*->new(..))" name="NewInstancePC"/>

       

      <aspect name="ConstructorAdvice" class="com.amentra.aop.advice.ConstructorAdvice" scope="PER_VM"/>

       

      <bind pointcut="NewInstancePC"> 

              <before aspect="ConstructorAdvice" name="interceptConstructor"/>

      </bind>{code}

       

      I've also tried doing this via reflection though and extended the appropriate class in my ConstructorAdvice and that also didn't result in anything.  The reflection I tried this way:

       

      jboss-aop.xml:

       

      {code:xml}<pointcut expr="call(* java.lang.reflect.Constructor->newInstance())" name="NewInstancePC"/>

      <pointcut expr="call(* java.lang.Class->newInstance())" name="ClassNewInstancePC"/>

       

      <aspect name="ConstructorAdvice" class="com.amentra.aop.advice.ConstructorAdvice" scope="PER_VM"/>

       

      <bind pointcut="NewInstancePC OR ClassNewInstancePC">

              <before aspect="ConstructorAdvice" name="interceptConstructor"/>

      </bind>{code}

       

      ConstructorAdvice.java:

       

      {code}

      package com.amentra.aop.advice;

       

      import java.lang.reflect.Constructor;

       

      import org.apache.log4j.Logger;

      import org.jboss.aop.joinpoint.Invocation;

      import org.jboss.aop.reflection.ReflectionAspect;

       

      public class ConstructorAdvice extends ReflectionAspect {

       

          Logger LOG = Logger.getLogger(this.getClass());

       

          @Override

          protected Object interceptConstructor(Invocation invocation, Constructor<?> constructor, Object[] args) throws Throwable

          {

              LOG.info("****************CONSTRUCTOR ADVICE****************");

       

              return super.interceptConstructor(invocation, constructor, args);

          }

      }{code}

       

      Thanks for any help on getting AOP to recognize my constructors.

        • 1. Re: Constructor Advice
          codefox

          The markup seems to have eaten some of my quotes in my pointcut expressions.  The quotes are in the actual code so the incorrectly formatted xml is all correct in Eclipse

          • 2. Re: Constructor Advice
            flavia.rainone

            I would first try to use an Interceptor in your case. If the interceptor works, then the problem is somewhere in the advice signature, and we can do further investigation. If the interceptor does not work, the problem is with the pointcut.