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

Rule EC24 #54

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [#308](https://github.com/green-code-initiative/ecoCode/issues/308) Implementing EC24 in Java

### Changed

### Deleted
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<google.re2j>1.7</google.re2j>

<!-- temporary version waiting for real automatic release in ecocode repository -->
<ecocode-rules-specifications.version>1.5.1</ecocode-rules-specifications.version>
<ecocode-rules-specifications.version>1.5.5-SNAPSHOT</ecocode-rules-specifications.version>

</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
import fr.greencodeinitiative.java.checks.IncrementCheck;
import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
import fr.greencodeinitiative.java.checks.LimitDbQueryResults;
import fr.greencodeinitiative.java.checks.NoFunctionCallWhenDeclaringForLoop;
import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions;
import org.sonar.plugins.java.api.CheckRegistrar;
Expand Down Expand Up @@ -62,7 +63,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
InitializeBufferWithAppropriateSize.class,
AvoidSetConstantInBatchUpdate.class,
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class
AvoidMultipleIfElseStatement.class,
LimitDbQueryResults.class
);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.Collections;
import java.util.List;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.LiteralTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;

import com.google.re2j.Pattern;

@Rule(key = "EC24")
public class LimitDbQueryResults extends IssuableSubscriptionVisitor {

protected static final String MESSAGERULE = "Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)";

private static final Pattern PATTERN = Pattern.compile("(LIMIT|TOP|ROW_NUMBER|FETCH FIRST|WHERE)", Pattern.CASE_INSENSITIVE);

@Override
public List<Kind> nodesToVisit() {
return Collections.singletonList(Tree.Kind.STRING_LITERAL);
}

@Override
public void visitNode(Tree tree) {
String value = ((LiteralTree) tree).value().toUpperCase();
if (value.contains("SELECT") && value.contains("FROM") && !PATTERN.matcher(value).find()) {
reportIssue(tree, MESSAGERULE);
}
}


}
39 changes: 39 additions & 0 deletions src/test/files/LimitDbQueryResults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

class LimitDbQueryResults {
LimitDbQueryResults(LimitDbQueryResults mc) {
}

public void literalSQLrequest() {
dummyCall("SELECT id, name, email FROM customers LIMIT 10;");
dummyCall("SELECT TOP 5 * FROM products;");
dummyCall("SELECT id, name, email FROM customers WHERE id = 1;");
dummyCall("SELECT * FROM orders FETCH FIRST 20 ROWS ONLY;");
dummyCall("WITH numbered_customers AS (SELECT *, ROW_NUMBER() OVER (ORDER BY customer_id) AS row_num FROM customers) SELECT * FROM numbered_customers WHERE row_num <= 50;");

dummyCall("SELECT * FROM bikes;"); // Noncompliant {{Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)}}
dummyCall("SELECT id FROM customers;"); // Noncompliant {{Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)}}
}

private String dummyCall(String request) {
return request;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void checkNumberRules() {
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);

assertThat(context.checkClasses()).hasSize(15);
assertThat(context.checkClasses()).hasSize(16);
assertThat(context.testCheckClasses()).isEmpty();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void init() {
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
repository = context.repository(rulesDefinition.repositoryKey());
rulesSize = 15;
rulesSize = 16;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

class LimitDbQueryResultsTest {

@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/LimitDbQueryResults.java")
.withCheck(new LimitDbQueryResults())
.verifyIssues();
}

}
Loading