Announcement Announcement Module
Collapse
No announcement yet.
aspect parameter validation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • aspect parameter validation

    Hi,
    Im doing a POC using Aspectj.

    class BaseInfo{..}

    class UserInfo extends BaseInfo{..}

    class UserService {

    public void getUser(UserInfo userInfo){..}

    public void deleteUser(String userId){..}

    }

    I've defined an advice, that gets invoked when I pass an UserInfo instance.But when i try to pass the BaseInfo, the advice is not getting invoked.

    Below block executes the afterMethod as expected for getUser.

    &ltaopointcut id="aopafterMethod" expression="execution(* UserService.*(..,UserInfo,..))" />

    &gtaop:after pointcut-ref="aopafterMethod" method="afterMethod" />

    But when i try to give BaseInfo instead of UserInfo, the aspect is not getting triggered.

    Am i missing something?
    Kindly help me on this issue.

  • #2
    As far as i know, inheritance doesn't play a part in AOP (expressions).

    Following will make the method get called regardless of what is passed,

    Code:
    @AfterReturning("execution(* com.UserService.getUser(..))")
    but wouldn't it be cool if inheritance do play a part huh

    Comment


    • #3
      Hi Amila,

      Thanks for the reply.

      I agree tht ur code will invoke irrespective of whatever args we pass.

      But my requirement is, if any args of base type BaseInfo is present, only that method should be invoked.

      Comment


      • #4
        You can try either,

        Code:
        @AfterReturning("execution(* UserService.*(..,UserInfo,..)) || execution(* UserService.*(..,BaseInfo,..))")
        or

        Code:
        @AfterReturning("execution(* UserService.*(..,*Info,..)))")
        But still these are not the exact answer to your problem

        Comment


        • #5
          Code:
          @AfterReturning("execution(* UserService.*(..,UserInfo,..))")
          protected void annotatedMethod() {}
          What above (your expression) really says is,

          Call the annotated method when a method that takes UserInfo as parameter (in class UserService), returns

          NOT

          Call the annotated method when a method has been passed a UserInfo, returns

          So with that conclusion, following code works fine for your problem

          Code:
          @AfterReturning("execution(* UserService.*(..,BaseInfo,..)))")
          and getUser() should take a BaseInfo

          Code:
          public void getUser(BaseInfo info) {}
          __________________

          Comment

          Working...
          X