77 java#177
Conversation
There was a problem hiding this comment.
Pull request overview
Refines the GCI77 (AvoidRegexPatternNotStatic) rule so that Pattern.compile(...) calls whose first argument is not a string literal are no longer reported, addressing the case where a regex built from concatenation cannot be promoted to a static constant.
Changes:
- Updated
AvoidRegexPatternNotStatic.visitMethodInvocationto additionally require the first argument to be aSTRING_LITERALbefore reporting. - Added a new compliant sample file
ValidParamRegexPattern.javaunder the IT test project. - Added a CHANGELOG entry referencing issue #3.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java |
Adds a literal-argument guard around the issue report. |
src/it/test-projects/.../GCI3/ValidParamRegexPattern.java |
New compliant sample showing Pattern.compile with a parameter / concatenation. |
CHANGELOG.md |
Notes the GCI77 improvement and links to issue #3. |
Comments suppressed due to low confidence (4)
src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java:73
- Typo:
isArgumentStringLitteralshould beisArgumentStringLiteral(single 't'). Please rename the variable.
boolean isArgumentStringLitteral = !arguments.isEmpty() && arguments.get(0).is(Tree.Kind.STRING_LITERAL);
if (PATTERN_COMPILE.matches(tree) && isArgumentStringLitteral){
src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI3/ValidParamRegexPattern.java:28
- The new test file is not wired into any test. Neither the unit test (
src/test/java/.../GCI77/AvoidRegexPatternNotStaticTest.java) nor the integration test (src/it/java/.../GCIRulesIT.java) referenceValidParamRegexPattern.java. As a result, the new behavior (Pattern.compile called with a non-string-literal argument is now compliant) is not actually exercised by the test suite. Please add this file (or an equivalent case) to the existingverifyNoIssues()invocation and add a corresponding integration test, otherwise the regression risk for this change is not covered.
public class ValidParamRegexPattern {
public void epjPatternWithParam(String codeEpj) {
final Pattern pattern = Pattern.compile("\"codeEpj\"\\s*:\\s" + codeEpj + ","); // Compliant - Pattern is used with a parameter
final Pattern pattern2 = Pattern.compile(codeEpj); // Compliant - Pattern is used with a parameter
}
}
src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI3/ValidParamRegexPattern.java:18
- The package declaration
fr.greencodeinitiative.java.checksdoes not match the file's directory path (.../org/greencodeinitiative/creedengo/java/checks/GCI3/) and is inconsistent with the sibling test files (e.g.AvoidRegexPatternNotStaticValid1.javausespackage org.greencodeinitiative.creedengo.java.checks;). This file will not compile under its current location. Please update the package toorg.greencodeinitiative.creedengo.java.checks.
package fr.greencodeinitiative.java.checks;
src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI3/ValidParamRegexPattern.java:3
- The license header still references the old project name and URL ("ecoCode … https://www.ecocode.io") and the year 2023, whereas all sibling files in this directory use "creedengo … https://green-code-initiative.org/" and "Copyright © 2024". Please align this header with the project convention.
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Improved test on STRING_LITERAL by recursively analyze Binary expression
Solves #3