-
Notifications
You must be signed in to change notification settings - Fork 47
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update the changelog
src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java
Outdated
Show resolved
Hide resolved
src/test/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSortTest.java
Show resolved
Hide resolved
|
Hi, I did some changes in order to update the PR. Also I see that run "verify" fails on my PR, I guess it's about rule specification. Do you know how I could correct this error ? Maybe I need to update rule specification or change the version of rule specification in the pom of my PR ? Error: Tests run: 5, Failures: 0, Errors: 5, Skipped: 0, Time elapsed: 0.264 s <<< FAILURE! -- in org.greencodeinitiative.creedengo.java.JavaRulesDefinitionTest Thank |
Hello @relivio! Yes you will need to use the version of |
Hello, Ok, so I'm waiting for the next release of rules-specification, on my local it's OK :) Olivier. |
...project/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java
Show resolved
Hide resolved
| MethodInvocationTree mit = (MethodInvocationTree) tree; | ||
|
|
||
| if (!isTerminalOperation(mit)) { | ||
| return; // On ignore les appels intermédiaires (filter, sorted, etc.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please put here comments in english ...
|
@utarwyn @relivio |
I do not see it even after 2 weeks, do you know why? |
|
@utarwyn ... no, but I've just check the problem ... as asked for, I go to maven central web site and ... indeed, we have to click on "publish" button to do it effectively. PS : maybe I deleted an option for automatic publish ... I will check it. |
|
@dedece Hello, sorry I have some full weeks of charge at work and at home :), I will push the correction requested this evening. |
| int[] startLines = new int[]{25}; | ||
| int[] endLines = new int[]{25}; | ||
|
|
||
| checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_5MIN); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when I execute the integration test, I have the following error and it's relevant for me. You should modify your startLines, endLines and severity value.
[ERROR] Failures:
[ERROR] GCIRulesIT.testGCI91:559->GCIRulesBase.checkIssuesForFile:84 [Extracted: rule, message, textRange.startLine, textRange.endLine, severity, type, effort]
Expecting ArrayList:
[("creedengo-java:GCI91", "Use 'filter' before 'sorted' for better efficiency.", 11, 14, MAJOR, CODE_SMELL, "5min"),
("creedengo-java:GCI91", "Use 'filter' before 'sorted' for better efficiency.", 16, 20, MAJOR, CODE_SMELL, "5min")]
to contain:
[("creedengo-java:GCI91", "Use 'filter' before 'sorted' for better efficiency.", 25, 25, MINOR, CODE_SMELL, "5min")]
but could not find the following element(s):
[("creedengo-java:GCI91", "Use 'filter' before 'sorted' for better efficiency.", 25, 25, MINOR, CODE_SMELL, "5min")]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the line 25 is the good one and not the bad one.
pom.xml
Outdated
|
|
||
| <!-- Version of creedengo rules specifications implemented by this plugin --> | ||
| <creedengo-rules-specifications.version>2.2.2</creedengo-rules-specifications.version> | ||
| <creedengo-rules-specifications.version>main-SNAPSHOT</creedengo-rules-specifications.version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can now use 2.2.3 version because it has been published a few minutes ago.
@relivio please take into account Copilot review feedbacks. |
|
Hello, All checks have passed, but rule is considered as Major, don't hesitate to tell me if I need to change that. Olivier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a new static analysis rule (GCI91) that detects inefficient stream operations where sorted() is called before filter(), recommending the more efficient order of filtering before sorting to reduce the number of elements that need to be sorted.
- Adds a new rule
UseFilterBeforeSortthat analyzes Java stream method chains - Implements comprehensive test coverage including unit tests and integration tests
- Updates project configuration to include the new rule in the default profile
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java |
Main rule implementation that detects sorted() before filter() patterns |
src/test/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSortTest.java |
Unit test for the new rule |
src/test/files/UseFilterBeforeSort.java |
Test file with compliant and non-compliant code examples |
src/main/java/org/greencodeinitiative/creedengo/java/JavaCheckRegistrar.java |
Registers the new rule with the plugin |
src/main/resources/org/greencodeinitiative/creedengo/java/creedengo_way_profile.json |
Adds GCI91 to the default rule profile |
src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java |
Integration test file with test cases |
src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java |
Integration test validation |
pom.xml |
Updates dependencies and Java version configuration |
CHANGELOG.md |
Documents the new rule addition |
Comments suppressed due to low confidence (2)
src/test/files/UseFilterBeforeSort.java:39
- The comment format is inconsistent. Compliant cases should not include the same message as non-compliant cases. This should be '// Compliant' without the violation message.
list.stream() // Compliant {{Use 'filter' before 'sorted' for better efficiency.}}
src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java:22
- The comment format is inconsistent. Compliant cases should not include the violation message. This should be '// Compliant' without the message.
list.stream() // Compliant {{Use 'filter' before 'sorted' for better efficiency.}}
| return false; | ||
| } | ||
| String methodName = ((MemberSelectExpressionTree) tree.methodSelect()).identifier().name(); | ||
| // Ajouter ici tous les terminaux connus |
Copilot
AI
Aug 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is in French. For consistency with the codebase, it should be in English: '// Add all known terminal operations here'
| // Ajouter ici tous les terminaux connus | |
| // Add all known terminal operations here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please correct this feedback
src/main/java/org/greencodeinitiative/creedengo/java/checks/UseFilterBeforeSort.java
Show resolved
Hide resolved
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.13.0</version> | ||
| <configuration> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this overriding of jdk versions ? it is already at 17...
I've just checked your PR with the deletion of this two overinf lines and UT et IT are OK.
|
|
||
| <!-- Version of creedengo rules specifications implemented by this plugin --> | ||
| <creedengo-rules-specifications.version>2.2.2</creedengo-rules-specifications.version> | ||
| <creedengo-rules-specifications.version>2.2.3</creedengo-rules-specifications.version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use the last one : 2.5.0
| return false; | ||
| } | ||
| String methodName = ((MemberSelectExpressionTree) tree.methodSelect()).identifier().name(); | ||
| // Ajouter ici tous les terminaux connus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please correct this feedback
| int sortedIndex = methodChain.indexOf("sorted"); | ||
| int filterIndex = methodChain.indexOf("filter"); | ||
|
|
||
| if (sortedIndex != -1 && filterIndex != -1 && sortedIndex < filterIndex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure about your algorithms ... I think in streams we can have several select method ... thus you can control only the first one ? or more complex, control all the stream and we want only control th second part ... for example :
stream
. filtrer()
.sorted()
. findAny() /// first part
. sort()
. filter()
. collect() // second part
what is the behaviour for examples with the same complexity (several parts) ?
can you add some such cases in your test resources files to control the behaviour ?
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
No description provided.