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 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 new file mode 100644 index 0000000..db73e09 --- /dev/null +++ b/src/test/files/ValidParamRegexPattern.java @@ -0,0 +1,28 @@ +/* + * 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 + ","); // Compliant - Pattern is used with a parameter + final Pattern pattern2 = Pattern.compile(codeEpj); // Compliant - Pattern is used with a parameter + } +} 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();