Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

Expand Down Expand Up @@ -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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Comment thread
pataluc marked this conversation as resolved.
* 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 <http://www.gnu.org/licenses/>.
*/
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
}