From bbe380da282677a878fd6e0d796707193d0c0965 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sun, 10 Dec 2023 22:59:20 +0100 Subject: [PATCH 1/4] Deprecation of rule EC63 for java --- CHANGELOG.md | 2 ++ RULES.md | 9 +++++---- .../java/checks/AvoidConcatenateStringsInLoop.java | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82656859a..71727e60c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deleted +- [#243](https://github.com/green-code-initiative/ecoCode/pull/243) Deprecate rule EC63 for Java because not applicable since JDK8 + ## [1.4.2] - 2023-12-05 ### Added diff --git a/RULES.md b/RULES.md index b67636ff4..0eb2676bc 100644 --- a/RULES.md +++ b/RULES.md @@ -60,10 +60,11 @@ Some are applicable for different technologies. This table lists rules proposed by the community but deprecated in ecoCode plugins with the justification. These rules will be completely deleted in next releases and moved to bottom deleted rules array. -| Rule key | Language | Name | Description | Invalidation | -|----------|---------------------|-------------------------------------|----------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------| -| EC34 | Java / Python / PHP | Using try...catch...finally calls | Implementation is too simple (only detection of presence of "try" statement) AND replaced by `EC35` rule | Github discussion with measures : [general/java](https://github.com/green-code-initiative/ecoCode/pull/128) / [python](https://github.com/green-code-initiative/ecoCode-python/pull/6) / [php](https://github.com/green-code-initiative/ecoCode-php/pull/10)| -| EC4 | Java | Avoid using global variables | Global variables do not exist in Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/233) | +| Rule key | Language | Name | Description | Invalidation | +|----------|---------------------|------------------------------------|----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------| +| EC34 | Java / Python / PHP | Using try...catch...finally calls | Implementation is too simple (only detection of presence of "try" statement) AND replaced by `EC35` rule | Github discussion with measures : [general/java](https://github.com/green-code-initiative/ecoCode/pull/128) / [python](https://github.com/green-code-initiative/ecoCode-python/pull/6) / [php](https://github.com/green-code-initiative/ecoCode-php/pull/10)| +| EC4 | Java | Avoid using global variables | Global variables do not exist in Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/233) | +| EC75 | Java | Concatenate Strings in loop | Optimizations on Java concatenation Strings are useless since JDK8 | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/246) | ## Refused / Deleted rules diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java index 99b732c50..491f41277 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java @@ -30,6 +30,10 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; +/** + * @deprecated because not useless since JDK 8 + */ +@Deprecated(forRemoval = true) @Rule(key = "EC75") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S75") public class AvoidConcatenateStringsInLoop extends IssuableSubscriptionVisitor { From a171062df590f3b7a8bef1d5df398ef6303b558d Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sun, 10 Dec 2023 23:00:41 +0100 Subject: [PATCH 2/4] Deprecation of rule EC63 for java - update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71727e60c..c4dfa9a76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deleted -- [#243](https://github.com/green-code-initiative/ecoCode/pull/243) Deprecate rule EC63 for Java because not applicable since JDK8 +- [#259](https://github.com/green-code-initiative/ecoCode/pull/259) Deprecate rule EC63 for Java because not applicable since JDK8 ## [1.4.2] - 2023-12-05 From 201b2a8f2e1b3d214d28ee99df04b3f422db293f Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sun, 10 Dec 2023 23:01:54 +0100 Subject: [PATCH 3/4] Deprecation of rule EC63 for java - BIS --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4dfa9a76..4966eb63d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deleted -- [#259](https://github.com/green-code-initiative/ecoCode/pull/259) Deprecate rule EC63 for Java because not applicable since JDK8 +- [#259](https://github.com/green-code-initiative/ecoCode/pull/259) Deprecate rule EC75 for Java because not applicable since JDK8 ## [1.4.2] - 2023-12-05 From c0ef68b88ac29c940b615e1dda1ee68154d707b4 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sun, 10 Dec 2023 23:26:03 +0100 Subject: [PATCH 4/4] Deprecation of rule EC75 for java - review corrections --- .../src/main/rules/EC75/java/EC75.asciidoc | 18 ++++++++++++------ .../src/main/rules/EC75/java/EC75.json | 3 +++ .../AvoidConcatenateStringsInLoopTest.java | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC75/java/EC75.json diff --git a/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc b/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc index 6cbc5ed2a..a0b7dad94 100644 --- a/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc @@ -1,10 +1,15 @@ +*This rule is deprecated because not applicable to Java since JDK8, and will be removed soon.* + +== Why is this an issue? + Don't concatenate Strings in loop. Use `StringBuilder instead. Strings are immutable so each time you concatenate a String, a new String is created. This is a waste of memory and CPU. -## Noncompliant Code Example +== Noncompliant Code Example -```java +[source,java] +---- public String concatenateStrings(String[] strings) { String result = ""; @@ -22,11 +27,12 @@ public String concatenateStrings2() { } return result; } -``` +---- -## Compliant Solution +== Compliant Solution -```java +[source,java] +---- public String concatenateStrings(String[] strings) { StringBuilder result = new StringBuilder(); @@ -35,4 +41,4 @@ public String concatenateStrings(String[] strings) { } return result.toString(); } -``` +---- diff --git a/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.json b/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.json new file mode 100644 index 000000000..0b963543b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.json @@ -0,0 +1,3 @@ +{ + "status": "deprecated" +} diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java index 00027474c..d3ad82f7d 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; +@Deprecated public class AvoidConcatenateStringsInLoopTest { @Test