Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

77 java #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/test/files/ValidParamRegexPattern.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down