Announcement Announcement Module
Collapse
No announcement yet.
AOP @Before/@After oldValue/newValue Changes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP @Before/@After oldValue/newValue Changes

    Hello,

    Please consider the following code:

    private Object oldVal = null;
    private Object newVal = null;
    private Object retVal = null;
    private Object target = null;
    private String getMethodName = null;
    private String setMethodName = null;
    private MethodSignature methodSig = null;
    private Method getMethod = null;

    @Before("execution(* com.hmi.configgui..*.set*(..))")
    @Order(1)
    public void beforeSetMethod(JoinPoint jp) throws Throwable {
    methodSig = (MethodSignature) jp.getSignature();
    target = jp.getTarget();

    try {
    getMethodName = methodSig.getMethod().getName();
    setMethodName = "s".concat(setMethodName.substring(1));
    getMethod = target.getClass().getDeclaredMethod(getMethodName, (Class<?>[])null);
    oldVal = getMethod.invoke(target, (Object[]) null);
    } catch(NoSuchMethodException e) {
    System.out.println(e);
    } catch(NullPointerException e) {
    System.out.println(e);
    }
    }


    @After("execution(* com.hmi.configgui..*.set*(..))")
    @Order(2)
    public void afterSetMethod(JoinPoint jp) throws Throwable {
    methodSig = (MethodSignature) jp.getSignature();
    target = jp.getTarget();

    try {
    setMethodName = methodSig.getMethod().getName();
    getMethodName = "g".concat(setMethodName.substring(1));
    getMethod = target.getClass().getDeclaredMethod(getMethodName, (Class<?>[])null);
    newVal = getMethod.invoke(target, (Object[]) null);
    } catch(NoSuchMethodException e) {
    System.out.println(e);
    } catch(NullPointerException e) {
    System.out.println(e);
    }

    if(((oldVal==null && newVal!=null) || !oldVal.equals(newVal))
    && configurationManager != null) {
    configurationManager.setModifedConfigurations(true );
    }
    }

    I am writing these methods to capture the oldValue and newValue to determine if there are any configuration changes. I make use the of AOP @Before and @After annotations.

    I am getting a few NullPointerExceptions.
    Any suggestions ?

    Paolo

  • #2
    Please use [ CODE] tags when posting code / xml / stacktraces.

    Code:
    setMethodName = "s".concat(setMethodName.substring(1));
    This obviously generates NPE as setMethodName is always null at this point. I guess you intended to substring getMethodName instead.

    But there is a bigger mistake here. Aspects beans should always be scoped to singletons (default) and as such they shouldn't contain state. Your aspect class is stateful, this is wrong.

    Comment


    • #3
      Yes, the NPE is a problem.
      I tried to address that by adding a catch block for the NPE.
      Can you elaborate with what you mean by: "Aspects beans should always be scoped to singletons (default) and as such they shouldn't contain state. Your aspect class is stateful, this is wrong." ?

      Maybe provide some code example/modification ?

      Paolo

      Comment


      • #4
        From the code you posted I suppose you use Spring AOP with annotation support (in case you're actually using AspectJ ltw or ctw then you should have mentioned that), so you have an @Aspect class which is also defined as a Spring bean one way or another. Unless you manually changed the scope of that bean, that bean is a singleton. As every OO programmer should know, singletons must not contain state. Your @Aspect annotated bean contains state (it has 8 class level variables), this has to be changed (btw it's easy to change that: use method level variables instead).
        On top of that I would use a single @Around advice instead of 1 @Before and 1 @After with same exact pointcut.
        For what concerns the NPE what you answered makes me think you didn't understand what I said. Wrapping an exception inside a try catch block doesn't mean resolving it. If you don't change that "s" into a "g" you'll always end up in that catch block.

        Comment


        • #5
          I have re-written the above as follows:

          @Around("execution(* com.hmi.configgui..*.set*(..))")
          public Object aroundSetMethod(ProceedingJoinPoint pjp) throws Throwable {
          MethodSignature methodSig = (MethodSignature) pjp.getSignature();
          Object target = pjp.getTarget();
          Object newValue = pjp.getArgs()[0];
          Object retVal = null;
          String setMethodName = methodSig.getMethod().getName();
          String getMethodName = "g".concat(setMethodName.substring(1));
          retVal = pjp.proceed();
          Object oldValue=null;

          try {
          Method getMethod = target.getClass().getDeclaredMethod(getMethodName, (Class<?>[])null);
          oldValue = getMethod.invoke(target, (Object[]) null);
          } catch(NoSuchMethodException e) {
          // swallow the error. This error is associated with descriptor class and can be ignored.
          System.out.println(e);
          return retVal;
          } catch(NullPointerException e) {
          //NetworkConnectionList - NetworkMasterList: NullPointerException
          System.out.println(e);
          } catch(Exception e) {
          System.out.println(e);
          }

          if(((oldValue==null && newValue!=null) || !oldValue.equals(newValue))
          && configurationManager != null) {
          configurationManager.setModifedConfigurations(true );
          }

          return retVal;
          }

          Your impressions ...........

          Paolo

          Comment


          • #6
            This won't work, at least not as you intend to. pjp.proceed() actually executes the advised method. So you should get and save the old value BEFORE you call pjp.proceed(), then you should get and save the new value, and then compare the two for equality and do your stuff if they are not equal. After that you should return the object you obtained from the pjp.proceed() call.

            Comment

            Working...
            X