Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ControlFlowPointcut extensibility #27187

Closed
kriegaex opened this issue Jul 19, 2021 · 9 comments
Closed

Improve ControlFlowPointcut extensibility #27187

kriegaex opened this issue Jul 19, 2021 · 9 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@kriegaex
Copy link
Contributor

kriegaex commented Jul 19, 2021

I am starting with an issue instead of a PR, because I want to find out if the maintainers are open to this suggestion and discuss how it best be done.

ControlFlowPointcut does not support method name patterns like

  • test* (AspectJ-like syntax) or
  • test.*, test[1-3] (regex syntax).

While the class is extensible and sports two methods to be overridden for improved filtering and it would be possible to override public boolean matches(Method, Class<?>) or public boolean matches(Method, Class<?>, Object...) in order to add the extra functionality, subclasses do not have access to fields like private Class<?> clazz, private String methodName and private volatile int evaluations. I.e., instead of extending existing functionality users would end up duplicating some things or even copying and extending the whole class like I did when answering StackOverflow #68431056 - please read and feel free to copy the source code.

I see several options here:

  • Extend ControlFlowPointcut directly in order to allow for more flexible method name matching, similar to how I did it. Sub-options:
    • Add a new Pattern field and corresponding constructor, like I did, in order to retain exact String matching mode for best performance.
    • Always interpret the existing String parameter as a regex.
  • Improve ControlFlowPointcut extensibility by making the fields mentioned above protected instead of private.

After we have come to a common understanding how this should best be done, the implementation as such should be pretty trivial. I can then create a PR, but the maintainers could also quickly do it by themselves, depending on their preference.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 19, 2021
@kriegaex
Copy link
Contributor Author

I would be glad to receive some feedback after 3 months. I am not sure what there is to triage in this case.

@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 10, 2021
@snicoll
Copy link
Member

snicoll commented Sep 25, 2023

@kriegaex sorry for the time it took to come to this. As I am sure you are aware, Spring Framework is touching a lot of ground and the team is limited. I don't know why this particular request took a lot of time, but there hasn't been a lot of interest by the community either (votes, comments, etc).

ControlFlowPointcut states in its class javadoc that it is a matcher for "simple cflow-style pointcut". I don't know if what you are suggesting to do stays in that category, but I'd rather provide some more extension rather than implementing these ourselves.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Sep 25, 2023
@kriegaex
Copy link
Contributor Author

kriegaex commented Sep 25, 2023

@snicoll, I am not sure I understand what exactly you are suggesting. If you have seen my code, you know that it would be easy to add more power and flexibility to this pointcut type with almost trivial changes.

If you think, that this makes things too complicated, I am failing to see why. But as maintainer of AspectJ, I can always welcome more Spring AOP users in need of better aspect functionality there, if you prefer.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 25, 2023
@sbrannen sbrannen changed the title Improve ControlFlowPointcut extensibility Improve ControlFlowPointcut extensibility Sep 25, 2023
@sbrannen
Copy link
Member

Hi @kriegaex,

Improve ControlFlowPointcut extensibility by making the fields mentioned above protected instead of private.

Let's go with that option.

Do you want to submit a PR?

If not, we can make the necessary changes.

Cheers,

Sam

@sbrannen sbrannen added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Sep 25, 2023
@sbrannen sbrannen self-assigned this Sep 25, 2023
@sbrannen sbrannen added this to the 6.1.0-RC1 milestone Sep 25, 2023
@kriegaex
Copy link
Contributor Author

kriegaex commented Oct 2, 2023

@sbrannen, assuming that I provide PR A to make the fields protected, would you also accept PR B which adds a subclass extending the simple parent class with more powerful pattern matching functionality? It would be backwards compatible, leaving the original untouched, but also enable Spring AOP users to utilise a more powerful solution out of the box, not everyone having to implement it by themselves. I am assuming, that pattern matching is what most users are looking for, expecting something like it, because other pointcut designators also support it.

What would it take for you to accept that into Spring Core? Or asking the other way around, what would be the problem in accepting it? The prospect to have one more class to maintain? To have to document it? Test coverage? Maybe we can address your concerns for the benefit of the Spring users community.

Just to be clear here: I personally would not benefit from it. I am not even an active Spring user. I am a native AspectJ guy. I simply mean to help to improve Spring AOP, raising the threshold for users for having to switch to full-blown AspectJ just because they need a control flow pointcut with a pattern.

@sbrannen
Copy link
Member

Hi @kriegaex,

@jhoeller and I took a closer look at our options and decided to focus this issue on the basic extensibility needs in ControlFlowPointcut.

In addition, we've created #31435 to provide a built-in mechanism for pattern matching which simultaneously allows for extension -- for example, to use regular expressions instead of PatternMatchUtils.simpleMatch(...).

I'll take care of both of these issues.

In light of that, there's no need for a PR.

Cheers,

Sam

@sbrannen sbrannen modified the milestones: 6.1.0-RC2, 6.1.x Oct 15, 2023
@kriegaex
Copy link
Contributor Author

Thanks, @sbrannen and @jhoeller. I have subscribed to the other issue, too. Looking forward to seeing this implemented.

@sbrannen sbrannen modified the milestones: 6.1.x, 6.1.0-RC2 Oct 25, 2023
@sbrannen
Copy link
Member

sbrannen commented May 19, 2024

In addition, we've created #31435 to provide a built-in mechanism for pattern matching which simultaneously allows for extension -- for example, to use regular expressions instead of PatternMatchUtils.simpleMatch(...).

For anyone interested, this is now quite easy to achieve, as can be seen in the RegExControlFlowPointcut I implemented as part of that closing commit.

private static class RegExControlFlowPointcut extends ControlFlowPointcut {
private final List<Pattern> compiledPatterns;
RegExControlFlowPointcut(Class<?> clazz, String... methodNamePatterns) {
super(clazz, methodNamePatterns);
this.compiledPatterns = super.methodNamePatterns.stream().map(Pattern::compile).toList();
}
@Override
protected boolean isMatch(String methodName, int patternIndex) {
return this.compiledPatterns.get(patternIndex).matcher(methodName).matches();
}
}

I've also posted an answer on that Stack Overflow discussion to point this out.

@kriegaex
Copy link
Contributor Author

@sbrannen, LGTM. Thank you so much for addressing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants