Announcement Announcement Module
Collapse
No announcement yet.
@Pointcut execution with Map parameter fails, use wildcard instead? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Pointcut execution with Map parameter fails, use wildcard instead?

    I'm using 2.5rc1 with LTW.

    The simple test class:
    Code:
    package foo;
    
    import java.util.Map;
    
    public class Service
    {
        public void methodWithString(String string) {
            System.out.println("methodWithString=" + string);
        }
        public void methodWithMap(Map map) {
            System.out.println("methodWithMap=" + map);
        }
    }
    The Aspect looks as follows:

    Code:
    package foo;
    
    import java.util.Map;
    import org.aspectj.lang.annotation.*;
    
    @Aspect
    public class MapAspect {
    
        @Pointcut("execution(* *.methodWithString(String)) && args(string)")
        private void stringPointcut(String string) {}
        
        @Before("stringPointcut(string)")
        public void logString(String string) {
            System.out.println("-> String=" + string);
        }
    
        @Pointcut("execution(* *.methodWithMap(Map)) && args(map)")
        private void mapPointcut(Map map) {}
        
        @Before("mapPointcut(map)")
        public void logMap(Map map) {
            System.out.println("-> Map=" + map);
        }
    
        @Pointcut("execution(* *.methodWithMap(*)) && args(map)")
        private void mapWildcardPointcut(Map map) {}
        
        @Before("mapWildcardPointcut(map)")
        public void logMapWildcard(Map map) {
            System.out.println("-> MapWildcard=" + map);
        }
    }
    The output is:
    Code:
    -> String=TEST-STRING
    methodWithString=TEST-STRING
    -> MapWildcard={KEY=VALUE}
    methodWithMap={KEY=VALUE}
    The color already indicates that the String and wildcard Pointcut work fine.
    But why does the Pointcut with the exact parameter type of that method fail?

    META-INF/aop.xml:
    Code:
    <!DOCTYPE aspectj PUBLIC
            "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
    <aspectj>
        <weaver options="-verbose">
            <include within="foo.*"/>
            <dump within="foo.*"/>
        </weaver>
        <aspects>
            <aspect name="foo.MapAspect"/>
        </aspects>
      </aspectj>
    application-context.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <bean id="service" class="foo.Service"/>
    
        <context:load-time-weaver/>
    
    </beans>

  • #2
    Type must be fully qualified

    The type must be fully qualified.
    Use java.util.Map instead of Map.

    But why isn't it possible to use the type of the parameter of the corresponding Pointcut method?

    Comment


    • #3
      As you observed, when used as a type name in pointcut expressions, you must specify the fully-qualified type name. The reason is that the runtime evaluation of the pointcut expression doesn't have access to the import statements (they aren't retained in byte code). However, when you use an identifier in a pointcut expression whose type is specified as a method parameter, its full type is retained in byte code, obviating the need to do anything special.

      -Ramnivas

      Comment


      • #4
        So the rule of thumb is:

        The type in a pointcut expression must be fully-qualified if it's an imported type, else not (e.g. Object, String, Number, etc.).

        Comment

        Working...
        X