-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-5294 Create rule S7185: @eventlistener methods should have …
…one parameter (#4618)
- Loading branch information
1 parent
ec2258b
commit 4c6681e
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
== Why is this an issue? | ||
|
||
Spring provides the `@EventListener` annotation as a simpler alternative to implementing the `ApplicationListener` interface for handling events. The `@EventListener` annotation registers a method as an event handler. | ||
This allows to skip the implementation of the `ApplicationListener` interface, making it easier to handle events. | ||
|
||
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. | ||
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |