-
-
Notifications
You must be signed in to change notification settings - Fork 50
GCI91 - Create rule 'Use filter before sort' in java context - V.E.R.T. #111
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
Open
relivio
wants to merge
12
commits into
green-code-initiative:main
Choose a base branch
from
relivio:GCI91-VERT
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c872aa3
GCI91 - Create rule 'Use filter before sort' in java context - V.E.R.T.
relivio 0fa8332
Merge branch 'main' into GCI91-VERT
relivio 1cb50f5
Merge branch 'main' into GCI91-VERT
relivio 60dccd6
GCI91 - Modification of Changelog file and add a new test to verify a…
relivio f93b677
Merge remote-tracking branch 'origin/GCI91-VERT' into GCI91-VERT
relivio e669c5b
Merge branch 'main' into GCI91-VERT
dedece35 2f4bf3f
GCI91 - Put comments in english and add the third test in integration…
relivio 52d6d3b
Merge branch 'main' into GCI91-VERT
dedece35 bc4386d
GCI91 - Correction from Copilot code review (Comment in french, not a…
relivio 04e3c8c
Merge remote-tracking branch 'origin/GCI91-VERT' into GCI91-VERT
relivio fdca436
GCI91 - Correction integration test
relivio a70224e
GCI91 - Use MAJOR severity - To check
relivio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...ject/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java
This file contains hidden or 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,21 @@ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| class UseFilterBeforeSort { | ||
| UseFilterBeforeSort() { | ||
| } | ||
|
|
||
| public void manipulateStream(final List<String> list) { | ||
| list.stream() // Noncompliant {{Use 'filter' before 'sorted' for better efficiency.}} | ||
| .sorted() | ||
| .filter(s -> s.startsWith("A")) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| list.stream() // Compliant {{Use 'filter' before 'sorted' for better efficiency.}} | ||
| .filter(s -> s.startsWith("A")) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java
This file contains hidden or 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,43 @@ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.MethodInvocationTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| @Rule(key = "GCI91") | ||
| public class UseFilterBeforeSort extends IssuableSubscriptionVisitor { | ||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return Arrays.asList(Tree.Kind.METHOD_INVOCATION); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| MethodInvocationTree methodInvocation = (MethodInvocationTree) tree; | ||
|
|
||
| // On vérifie si c’est un appel à "sorted" | ||
| if (methodInvocation.methodSelect() instanceof MemberSelectExpressionTree) { | ||
| MemberSelectExpressionTree sortedSelect = (MemberSelectExpressionTree) methodInvocation.methodSelect(); | ||
| String sortedMethod = sortedSelect.identifier().name(); | ||
|
|
||
| if ("sorted".equals(sortedMethod)) { | ||
| Tree methodCall = methodInvocation.parent(); | ||
|
dedece35 marked this conversation as resolved.
Outdated
|
||
| if (methodCall instanceof MemberSelectExpressionTree) { | ||
| MemberSelectExpressionTree filterCall = (MemberSelectExpressionTree) methodCall; | ||
| String filterMethod = filterCall.identifier().name(); | ||
|
|
||
| if ("filter".equals(filterMethod)) { | ||
| // Mauvais ordre détecté : sorted().filter() au lieu de filter().sorted() | ||
| reportIssue(methodInvocation, "Use 'filter' before 'sorted' for better efficiency."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| "GCI78", | ||
| "GCI79", | ||
| "GCI82", | ||
| "GCI91", | ||
| "GCI94" | ||
| ] | ||
| } | ||
This file contains hidden or 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,35 @@ | ||
| /* | ||
| * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| class UseFilterBeforeSort { | ||
| UseFilterBeforeSort() { | ||
| } | ||
|
|
||
| public void manipulateStream(List<String> list) { | ||
| list.stream() // Noncompliant {{Use 'filter' before 'sorted' for better efficiency.}} | ||
| .sorted() | ||
| .filter(s -> s.startsWith("A")) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| list.stream() // Compliant {{Use 'filter' before 'sorted' for better efficiency.}} | ||
| .filter(s -> s.startsWith("A")) | ||
| .sorted() | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/test/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSortTest.java
This file contains hidden or 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,14 @@ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
|
||
| public class UseFilterBeforeSortTest { | ||
| @Test | ||
| void testHasIssues() { | ||
|
dedece35 marked this conversation as resolved.
|
||
| CheckVerifier.newVerifier() | ||
| .onFile("src/test/files/UseFilterBeforeSort.java") | ||
| .withCheck(new UseFilterBeforeSort()) | ||
| .verifyIssues(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.