Results 1 to 7 of 7

Thread: Annotation problem

  1. #1
    Join Date
    Apr 2008
    Posts
    11

    Default Annotation problem

    Hi.
    I got problem with annotation in pointcut. I want to run my aspect only when methods which are invoked are annotated with my annotation.

    xml
    Code:
    <aop:aspect id="dataWarehouseAspect" ref="dataWarehouseAspectBean">
           <aop:around method="processMessage" pointcut="com.mikel.aspect.DataWarehouseAspect.dataWarehouseMethods()"/>
    </aop:aspect>
    I got more aspect which are ordered.

    Now the pointcuts in the class:
    Code:
        @Pointcut("@annotation(com.mikel.aspect.DataWarehouseMessage)")// the pointcut expression
        private void dataWarehouseMessage() {}// the pointcut signature  
        
        @Pointcut("execution(* com.mikel.server.kernel.service.*.*(..))")// the pointcut expression
        private void allServiceMethods() {}// the pointcut signature
        
        @Pointcut("execution(* com.mikel.server.kernel.CallmanServer.*(..))")// the pointcut expression
        private void allServerMethods() {}// the pointcut signature
        
        @Pointcut("((allServiceMethods() || allServerMethods()) && dataWarehouseMessage())")// the pointcut expression
        public void dataWarehouseMethods() {}// the pointcut signature
    Those pointcut are defined in DataWarehouseAspect. When annotaed methos are invoked, no action in processMessage method in this aspect

    What im doing wrong ? I tried almost everythink.

  2. #2
    Join Date
    Apr 2008
    Posts
    11

    Default

    When i use @Transactional annotation everythink is ok. Im using
    Code:
     <aop:advisor order="2" advice-ref="txAdvice" pointcut="com.mikel.kernel.aspect.TransactionAspect.transactionalMethods()"/>
    Is this advisor have something in common with matching annotations ?

  3. #3
    Join Date
    Apr 2008
    Posts
    11

    Default

    Quote Originally Posted by mikel1982 View Post
    When i use @Transactional annotation everythink is ok. Im using
    Code:
     <aop:advisor order="2" advice-ref="txAdvice" pointcut="com.mikel.kernel.aspect.TransactionAspect.transactionalMethods()"/>
    Is this advisor have something in common with matching annotations ?
    Ok problem solved, i have my annotation infront of interface method not in implementation.

  4. #4
    Join Date
    Apr 2008
    Posts
    11

    Post

    I have another problem connected with this case. Im inside the aspect and i need to get annotation from method which has been invoked.

    I get the method by this code:

    Code:
        protected Method getMethod(ProceedingJoinPoint call)
        {
            Signature sign = call.getSignature();
            if(sign instanceof MethodSignature)
            {
                return ((MethodSignature)sign).getMethod();
            }
        }
    But method its not method from implementation class but from interface. And in the interface there is no annotation. Somebody know how to get correct method ? or this annotation in different way ?

  5. #5
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    I assume the code below is your advice.
    There is a getAnotation*() methods on Method type and you need to get a reference t it first.
    Here is the modified code.
    Code:
    protected Method getMethod(ProceedingJoinPoint call) {
           MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
           Method method = mi.getMethod();
           System.out.println(method.getAnnotations()[0]);
    }
    
    Here is my sample output:
    
         @beanpointcut.MyAnnotation()

  6. #6
    Join Date
    Apr 2008
    Posts
    11

    Default

    But this method is also from interface. I need method from the target.

  7. #7
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    All you need to do is access the Method definition from the target object (the one that is being advised)
    Just interrogate your MethodInvocation a little further:
    Code:
    MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
    Method iMethod = mi.getMethod();
    
    System.out.println("Method name: " + iMethod.getName());
    Annotation[] annotations = iMethod.getAnnotations();
    for (int i = 0; i < annotations.length; i++) {
    	System.out.println("IAnnotation: " + annotations[i]);
    }
    		
    Object obj = mi.getThis();
    try {
    	Method cMethod = obj.getClass().getMethod(iMethod.getName(), iMethod.getParameterTypes());
    	Annotation[] cAnnotations = cMethod.getAnnotations();
    	for (int i = 0; i < cAnnotations.length; i++) {
    		System.out.println("CAnnotation: " + cAnnotations[i]);
    	}
    } catch (Exception e) {
    	e.printStackTrace();
    }
    And the output on my end is:

    Method name: foo
    IAnnotation: @test.MyOtherAnnotation()
    CAnnotation: @test.MyAnnotation()
    executing foo()

Posting Permissions

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