diff --git a/CHANGELOG.md b/CHANGELOG.md index d83178ed..2e561182 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - upgrade internal libraries versions - non retro-compatibility upgrades - refacto to have all the test files in the same place (for UT and IT), to avoid maintaining 2 test directories - refacto all test files to add sub-directories for each rule, to be more clear and to be able to add more tests for each rule in the future +- [#3](https://github.com/green-code-initiative/creedengo-java/issues/3) Improvement: pattern declaration not only in a static way - fix integration test system run + fix TI GCI82 ### Deleted diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index 86c5f5fc..18c95403 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -238,8 +238,8 @@ void testGCI2_noIssue() { void testGCI77_invalid() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStatic.java"; - int[] startLines = new int[]{25}; - int[] endLines = new int[]{25}; + int[] startLines = new int[]{25, 26, 27}; + int[] endLines = new int[]{25, 26, 27}; String ruleId = "creedengo-java:GCI77"; String ruleMsg = "Avoid using Pattern.compile() in a non-static context."; @@ -282,6 +282,18 @@ void testGCI77_valid3() { checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_20MIN); } + @Test + void testGCI77_valid4() { + + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/ValidParamRegexPattern.java"; + int[] startLines = new int[]{}; + int[] endLines = new int[]{}; + String ruleId = "creedengo-java:GCI77"; + String ruleMsg = "Avoid using Pattern.compile() in a non-static context."; + + checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_20MIN); + } + @Test void testGCI78() { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStatic.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStatic.java index 76387635..56466331 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStatic.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/AvoidRegexPatternNotStatic.java @@ -23,6 +23,10 @@ public class AvoidRegexPatternNotStatic { public boolean foo() { final Pattern pattern = Pattern.compile("foo"); // Noncompliant {{Avoid using Pattern.compile() in a non-static context.}} - return pattern.matcher("foo").find(); + final Pattern pattern2 = Pattern.compile("foo" + "bar"); // Noncompliant {{Avoid using Pattern.compile() in a non-static context.}} + final Pattern pattern3 = Pattern.compile("foo" + "bar" + "baz"); // Noncompliant {{Avoid using Pattern.compile() in a non-static context.}} + return pattern.matcher("foo").find() + && pattern2.matcher("foo").find() + && pattern3.matcher("foo").find(); } } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/ValidParamRegexPattern.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/ValidParamRegexPattern.java new file mode 100644 index 00000000..0b959161 --- /dev/null +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/GCI77/ValidParamRegexPattern.java @@ -0,0 +1,28 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * 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 org.greencodeinitiative.creedengo.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/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java index beb1cea6..8298115c 100644 --- a/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java +++ b/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java @@ -26,7 +26,9 @@ 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.BinaryExpressionTree; import org.sonar.plugins.java.api.tree.MethodInvocationTree; import org.sonar.plugins.java.api.tree.MethodTree; import org.sonar.plugins.java.api.tree.Tree; @@ -67,12 +69,26 @@ private class AvoidRegexPatternNotStaticVisitor extends BaseTreeVisitor { @Override public void visitMethodInvocation(@Nonnull MethodInvocationTree tree) { - if (PATTERN_COMPILE.matches(tree)) { + Arguments arguments = tree.arguments(); + boolean isArgumentStringLiteral = !arguments.isEmpty() && isStringLiteralOrConcatenation(arguments.get(0)); + if (PATTERN_COMPILE.matches(tree) && isArgumentStringLiteral) { reportIssue(tree, MESSAGE_RULE); } else { super.visitMethodInvocation(tree); } } + private boolean isStringLiteralOrConcatenation(Tree tree) { + if (tree.is(Tree.Kind.STRING_LITERAL)) { + return true; + } + if (tree.is(Tree.Kind.PLUS)) { + BinaryExpressionTree binaryExpr = (BinaryExpressionTree) tree; + return isStringLiteralOrConcatenation(binaryExpr.leftOperand()) + && isStringLiteralOrConcatenation(binaryExpr.rightOperand()); + } + return false; + } + } }