Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] org.aspectj.weaver.tools.PointcutExpression.couldMatchJoinPointsInType(Class aClass) always returns true in all scenarios

Hi

I was using an AspectJPointcutExpression from Spring AOP and was trying to have the pointcut tell me wether it could potentially match a join point in a specific class. I looked at the source for AspectJPointcutExpression and that seems exactly what it is intended to indicate when its matches(Class targetClass) is invoked  (from ClassFilter interface).

The matches(Class targetClass) in AspectJPointcutExpression, from looking at the source, seems to simply delegate to  org.aspectj.weaver.tools.PointcutExpression.couldMatchJoinPointsInType(Class aClass)

The javadoc for org.aspectj.weaver.tools.PointcutExpression.couldMatchJoinPointsInType(Class aClass) says:


/**
     * Determine whether or not this pointcut could ever match a join point in the given class.
     * @param aClass  the candidate class
     * @return true iff this pointcut <i>may</i> match a join point within(aClass), and false otherwise
     */


and yet it seems to return true no matter what class is passed in! I created a sample scenario to demonstrate the issue with both Spring AOP and directly with AspectJ APIs, it is shown below. It correctly matches the methods, but not the class. I also tried it with the "target" pointcut _expression_ and got the same behavior. Can anyone shed some light on this? Am I just misunderstanding the documentation for org.aspectj.weaver.tools.PointcutExpression.couldMatchJoinPointsInType(Class aClass)? Is there another way to determine whether a join point in a class will ever be matched by a specific org.aspectj.weaver.tools.PointcutExpression? Thanks

With Spring AOP:

-------------------

AspectJExpressionPointcut aspectJPC = new AspectJExpressionPointcut();
        aspectJPC.setExpression("execution(* com.company.aop.test.Target.targetMethod1(..))");
       
        System.out.println(aspectJPC.matches(TargetImpl.class));
        System.out.println(aspectJPC.matches(LoggerImpl.class));
        System.out.println(aspectJPC.getPointcutExpression().couldMatchJoinPointsInType(TargetImpl.class));
        System.out.println(aspectJPC.getPointcutExpression().couldMatchJoinPointsInType(LoggerImpl.class));
       
        System.out.println(aspectJPC.getMethodMatcher().matches(TargetImpl.class.getMethod("targetMethod1"),null));
       
        System.out.println(aspectJPC.getMethodMatcher().matches(TargetImpl.class.getMethod("targetMethod2"),null));
        System.out.println(aspectJPC.getMethodMatcher().matches(LoggerImpl.class.getMethod("logSomething"),null));

Output:

true
true  # expected false here
true
true  # expected false here
true
false
false

-------------------

package com.company.random;

public interface Logger
{
    public void logSomething();
}

------------------

package com.company.aop.test;

public interface Target
{
    public void targetMethod1();
    public void targetMethod2();
}

---------------


With AspectJ:


PointcutParser parser = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution();
       
        PointcutExpression _expression_ = parser.parsePointcutExpression("execution(* com.company.aop.test.Target.targetMethod1(..))",null, new PointcutParameter[]{});
       
       
        System.out.println(_expression_.couldMatchJoinPointsInType(TargetImpl.class));
        System.out.println(_expression_.couldMatchJoinPointsInType(LoggerImpl.class));
       
        System.out.println(_expression_.matchesMethodExecution(TargetImpl.class.getMethod("targetMethod1")).alwaysMatches());
       
        System.out.println(_expression_.matchesMethodExecution(TargetImpl.class.getMethod("targetMethod2")).alwaysMatches());
        System.out.println(_expression_.matchesMethodExecution(LoggerImpl.class.getMethod("logSomething")).alwaysMatches());

Output:

true
true # expected false here
true
false
false



Back to the top