From 3e2320081962270b1bd71be144aeb6988f1f54cd Mon Sep 17 00:00:00 2001 From: Denielig Date: Wed, 29 May 2024 15:21:41 +0200 Subject: [PATCH 1/3] Add false positive test case --- src/test/files/ValidParamRegexPattern.java | 27 +++++++++++++++++++ .../AvoidRegexPatternNotStaticTest.java | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/test/files/ValidParamRegexPattern.java diff --git a/src/test/files/ValidParamRegexPattern.java b/src/test/files/ValidParamRegexPattern.java new file mode 100644 index 0000000..3dc91c7 --- /dev/null +++ b/src/test/files/ValidParamRegexPattern.java @@ -0,0 +1,27 @@ +/* + * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) + * + * 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 . + */ +package fr.greencodeinitiative.java.checks; + +import java.util.regex.Pattern; + +public class ValidParamRegexPattern { + + public void epjPatternWithParam(String codeEpj) { + final Pattern pattern = Pattern.compile("\"codeEpj\"\\s*:\\s" + codeEpj + ","); // Noncompliant {{Avoid using Pattern.compile() in a non-static context.}} + } +} diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java b/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java index 1750232..9ae2454 100644 --- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java +++ b/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java @@ -36,7 +36,8 @@ void testHasNoIssues() { .onFiles( "src/test/files/ValidRegexPattern.java", "src/test/files/ValidRegexPattern2.java", - "src/test/files/ValidRegexPattern3.java" + "src/test/files/ValidRegexPattern3.java", + "src/test/files/ValidParamRegexPattern.java" ) .withCheck(new AvoidRegexPatternNotStatic()) .verifyNoIssues(); From 0ce448433abcac5bafd420806b8f2e81803f6dba Mon Sep 17 00:00:00 2001 From: Maxime Daniel Date: Thu, 30 May 2024 09:06:57 +0200 Subject: [PATCH 2/3] fix: :bug: added exception to EC77 for parameterized regex --- .../java/checks/AvoidRegexPatternNotStatic.java | 5 ++++- src/test/files/ValidParamRegexPattern.java | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java b/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java index 2561a6c..8f68d0c 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java @@ -26,6 +26,7 @@ import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; +import org.sonar.plugins.java.api.tree.Arguments; import org.sonar.plugins.java.api.tree.BaseTreeVisitor; import org.sonar.plugins.java.api.tree.MethodInvocationTree; import org.sonar.plugins.java.api.tree.MethodTree; @@ -66,7 +67,9 @@ private class AvoidRegexPatternNotStaticVisitor extends BaseTreeVisitor { @Override public void visitMethodInvocation(@Nonnull MethodInvocationTree tree) { - if (PATTERN_COMPILE.matches(tree)) { + Arguments arguments = tree.arguments(); + boolean isArgumentStringLitteral = !arguments.isEmpty() && arguments.get(0).is(Tree.Kind.STRING_LITERAL); + if (PATTERN_COMPILE.matches(tree) && isArgumentStringLitteral){ reportIssue(tree, MESSAGE_RULE); } else { super.visitMethodInvocation(tree); diff --git a/src/test/files/ValidParamRegexPattern.java b/src/test/files/ValidParamRegexPattern.java index 3dc91c7..db73e09 100644 --- a/src/test/files/ValidParamRegexPattern.java +++ b/src/test/files/ValidParamRegexPattern.java @@ -22,6 +22,7 @@ public class ValidParamRegexPattern { public void epjPatternWithParam(String codeEpj) { - final Pattern pattern = Pattern.compile("\"codeEpj\"\\s*:\\s" + codeEpj + ","); // Noncompliant {{Avoid using Pattern.compile() in a non-static context.}} + 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 } } From 13fa0fa11512101f4a48170b0e38295d368500c5 Mon Sep 17 00:00:00 2001 From: Luc Fouin Date: Thu, 30 May 2024 12:08:03 +0200 Subject: [PATCH 3/3] =?UTF-8?q?docs:=20=F0=9F=93=9D=20Updating=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce62a8..477f410 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [#3](https://github.com/green-code-initiative/ecoCode-java/issues/3) Improvement: pattern declaration not only in a static way + ### Deleted ## [1.6.1] - 2024-05-15