Announcement Announcement Module
Collapse
No announcement yet.
Reusing method matching logic? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reusing method matching logic?

    Hi friends,

    I need to find Methods in a Class based on a String representation, and realized the AspectJ pointcut syntax would be perfect for my need. For example:

    Code:
    execution(@com.example.Blah * *(..))
    execution(* set*(..))
    My usage is not related to AOP, I simply need to find Methods in a given Class.

    I was wondering if there was an easy way to reuse Spring/AspectJ's matching logic in my own class?

    For example, I would like to do something like:

    Code:
    Method[] methods = myUtility.getMethods(SomeClass.class, "* set*(..)")
    I looked around a bit in the Spring source, but I wasn't sure where to start.
    Any ideas/suggestions? I'm hoping there is an easily reusable class in Spring that I could use directly.

    Thanks,
    G

  • #2
    BTW, I found the solution just using AspectJ code:

    Code:
    import org.aspectj.weaver.tools.PointcutExpression;
    import org.aspectj.weaver.tools.PointcutParser;
    import org.aspectj.weaver.tools.ShadowMatch;
    
    public void testParse() {
        PointcutParser parser = 
            PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution();
        PointcutExpression expression =
            parser.parsePointcutExpression("execution(void set*(..))");
        for (Method method : Whatever.class.getMethods()) {
            ShadowMatch matcher = expression.matchesMethodExecution(method);
            if (matcher.alwaysMatches()) {
                // do whatever
            }
        }
    }

    Comment

    Working...
    X