Skip to content

77 java#177

Open
pataluc wants to merge 8 commits into
green-code-initiative:mainfrom
max-208:77-java
Open

77 java#177
pataluc wants to merge 8 commits into
green-code-initiative:mainfrom
max-208:77-java

Conversation

@pataluc
Copy link
Copy Markdown
Contributor

@pataluc pataluc commented May 13, 2026

Solves #3

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.visitMethodInvocation to additionally require the first argument to be a STRING_LITERAL before reporting.
  • Added a new compliant sample file ValidParamRegexPattern.java under 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: isArgumentStringLitteral should be isArgumentStringLiteral (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) reference ValidParamRegexPattern.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 existing verifyNoIssues() 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.checks does not match the file's directory path (.../org/greencodeinitiative/creedengo/java/checks/GCI3/) and is inconsistent with the sibling test files (e.g. AvoidRegexPatternNotStaticValid1.java uses package org.greencodeinitiative.creedengo.java.checks;). This file will not compile under its current location. Please update the package to org.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.

Comment thread CHANGELOG.md Outdated
pataluc and others added 3 commits May 20, 2026 00:57
Improved test on STRING_LITERAL by recursively analyze Binary expression
@rducasse rducasse self-assigned this May 20, 2026
@pataluc pataluc requested a review from rducasse May 20, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🗃️ rule rule improvment or rule development or bug 🚀 enhancement New feature or request

Projects

Status: Ready To Review

Development

Successfully merging this pull request may close these issues.

[GCI77] rule improvment : pattern declaration not only in a static way

5 participants