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

SONARJAVA-5294 Create rule S7185: @eventlistener methods should have one parameter #4618

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions rules/S7185/java/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "@EventListener methods should have one parameter at most",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"spring"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7185",
"sqKey": "S7185",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "HIGH"
},
"attribute": "LOGICAL"
}
}
37 changes: 37 additions & 0 deletions rules/S7185/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
== Why is this an issue?

Spring provides the `@EventListener` annotation to make a method handle some specific types of events.
This allows to skip the implementation of the `ApplicationListener` interface, making it easier to handle events.
However, the `@EventListener` annotation can only be used on methods that have at most one parameter, which should be the specific event that we want to handle.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a line break and remove the however.

Suggested change
However, the `@EventListener` annotation can only be used on methods that have at most one parameter, which should be the specific event that we want to handle.
The `@EventListener` annotation can only be used on methods that have at most one parameter, which should be the specific event that we want to handle.

For multiple types of events, we can use the `classes` argument of the annotation, and provide a list of classes of events that we want to handle.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For multiple types of events, we can use the `classes` argument of the annotation, and provide a list of classes of events that we want to handle.
To listen to several types of events, use the `classes` argument of the `@EventListener` annotation.


This rule raises an issue on all methods annotated with `@EventListener` that have more than one parameter.

== How to fix it

=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
@EventListener
void handleEvent(CustomerEvent customerEvent, ExceptionalEvent exceptionalEvent) { // Non compliant, this will cause a runtime error
//... some event handling
}
----

==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
@EventListener(classes = {CustomerEvent.class, ExceptionalEvent.class})
void handleEvent(ApplicationEvent event) { // Only one parameter, of the super type `ApplicationEvent`
//... some event handling
}
----

== Resources
=== Documentation

Spring API - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/event/EventListener.html[@EventListener]
2 changes: 2 additions & 0 deletions rules/S7185/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}