Results 1 to 5 of 5

Thread: aspect parameter validation

  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Default 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. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    302

    Default

    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
    Amila Domingo

  3. #3
    Join Date
    Sep 2009
    Posts
    3

    Default

    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.

  4. #4
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    302

    Default

    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
    Amila Domingo

  5. #5
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    302

    Default

    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) {}
    __________________
    Amila Domingo

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •