From 844ca7e8eb43111503682a08bf44c724b9813415 Mon Sep 17 00:00:00 2001 From: leonardo-pilastri-sonarsource Date: Mon, 27 Jan 2025 10:51:58 +0000 Subject: [PATCH 1/4] Create rule S7185 --- rules/S7185/java/metadata.json | 25 +++++++++++++++++++ rules/S7185/java/rule.adoc | 44 ++++++++++++++++++++++++++++++++++ rules/S7185/metadata.json | 2 ++ 3 files changed, 71 insertions(+) create mode 100644 rules/S7185/java/metadata.json create mode 100644 rules/S7185/java/rule.adoc create mode 100644 rules/S7185/metadata.json diff --git a/rules/S7185/java/metadata.json b/rules/S7185/java/metadata.json new file mode 100644 index 00000000000..bbecacbb422 --- /dev/null +++ b/rules/S7185/java/metadata.json @@ -0,0 +1,25 @@ +{ + "title": "FIXME", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7185", + "sqKey": "S7185", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S7185/java/rule.adoc b/rules/S7185/java/rule.adoc new file mode 100644 index 00000000000..4172043c9d3 --- /dev/null +++ b/rules/S7185/java/rule.adoc @@ -0,0 +1,44 @@ +FIXME: add a description + +// If you want to factorize the description uncomment the following line and create the file. +//include::../description.adoc[] + +== Why is this an issue? + +FIXME: remove the unused optional headers (that are commented out) + +//=== What is the potential impact? + +== How to fix it +//== How to fix it in FRAMEWORK NAME + +=== Code examples + +==== Noncompliant code example + +[source,java,diff-id=1,diff-type=noncompliant] +---- +FIXME +---- + +==== Compliant solution + +[source,java,diff-id=1,diff-type=compliant] +---- +FIXME +---- + +//=== How does this work? + +//=== Pitfalls + +//=== Going the extra mile + + +//== Resources +//=== Documentation +//=== Articles & blog posts +//=== Conference presentations +//=== Standards +//=== External coding guidelines +//=== Benchmarks diff --git a/rules/S7185/metadata.json b/rules/S7185/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S7185/metadata.json @@ -0,0 +1,2 @@ +{ +} From 686690bcce224dcd053ee2f973c644d1ed68f26e Mon Sep 17 00:00:00 2001 From: Leonardo Pilastri Date: Mon, 27 Jan 2025 15:03:07 +0100 Subject: [PATCH 2/4] SONARJAVA-5294 Create rule S7185: @EventListener methods should have one parameter --- rules/S7185/java/metadata.json | 11 +++++----- rules/S7185/java/rule.adoc | 39 ++++++++++++++-------------------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/rules/S7185/java/metadata.json b/rules/S7185/java/metadata.json index bbecacbb422..15f02153bbb 100644 --- a/rules/S7185/java/metadata.json +++ b/rules/S7185/java/metadata.json @@ -1,12 +1,13 @@ { - "title": "FIXME", - "type": "CODE_SMELL", + "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", @@ -16,10 +17,8 @@ "quickfix": "unknown", "code": { "impacts": { - "MAINTAINABILITY": "HIGH", - "RELIABILITY": "MEDIUM", - "SECURITY": "LOW" + "RELIABILITY": "HIGH" }, - "attribute": "CONVENTIONAL" + "attribute": "LOGICAL" } } diff --git a/rules/S7185/java/rule.adoc b/rules/S7185/java/rule.adoc index 4172043c9d3..13dee6e31de 100644 --- a/rules/S7185/java/rule.adoc +++ b/rules/S7185/java/rule.adoc @@ -1,16 +1,13 @@ -FIXME: add a description - -// If you want to factorize the description uncomment the following line and create the file. -//include::../description.adoc[] - == Why is this an issue? -FIXME: remove the unused optional headers (that are commented out) +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. +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. -//=== What is the potential impact? +This rule raises an issue on all methods annotated with `@EventListener` that have more than one parameter. == How to fix it -//== How to fix it in FRAMEWORK NAME === Code examples @@ -18,27 +15,23 @@ FIXME: remove the unused optional headers (that are commented out) [source,java,diff-id=1,diff-type=noncompliant] ---- -FIXME +@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] ---- -FIXME +@EventListener(classes = {CustomerEvent.class, ExceptionalEvent.class}) +void handleEvent(ApplicationEvent event) { // Only one parameter, of the super type `ApplicationEvent` + //... some event handling +} ---- -//=== How does this work? - -//=== Pitfalls - -//=== Going the extra mile - +== Resources +=== Documentation -//== Resources -//=== Documentation -//=== Articles & blog posts -//=== Conference presentations -//=== Standards -//=== External coding guidelines -//=== Benchmarks +Spring API - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/event/EventListener.html[@EventListener] \ No newline at end of file From a392c5a34178557764c5d8d3ec3b090c05c49d7c Mon Sep 17 00:00:00 2001 From: Leonardo Pilastri Date: Tue, 28 Jan 2025 09:54:09 +0100 Subject: [PATCH 3/4] Review changes --- rules/S7185/java/rule.adoc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rules/S7185/java/rule.adoc b/rules/S7185/java/rule.adoc index 13dee6e31de..3e2950c33ea 100644 --- a/rules/S7185/java/rule.adoc +++ b/rules/S7185/java/rule.adoc @@ -2,8 +2,9 @@ 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. -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. + +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. @@ -34,4 +35,4 @@ void handleEvent(ApplicationEvent event) { // Only one parameter, of the super t == Resources === Documentation -Spring API - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/event/EventListener.html[@EventListener] \ No newline at end of file +Spring API - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/event/EventListener.html[@EventListener] From 9cf744d25b72a742b5589c1d481be4fb70f0643d Mon Sep 17 00:00:00 2001 From: leonardo-pilastri-sonarsource <115481625+leonardo-pilastri-sonarsource@users.noreply.github.com> Date: Tue, 28 Jan 2025 10:59:25 +0100 Subject: [PATCH 4/4] Update rules/S7185/java/rule.adoc Co-authored-by: erwan-serandour --- rules/S7185/java/rule.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/S7185/java/rule.adoc b/rules/S7185/java/rule.adoc index 3e2950c33ea..7f613e94bf3 100644 --- a/rules/S7185/java/rule.adoc +++ b/rules/S7185/java/rule.adoc @@ -1,6 +1,6 @@ == Why is this an issue? -Spring provides the `@EventListener` annotation to make a method handle some specific types of events. +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.