Skip to content

Commit 4fc4197

Browse files
committed
Add first test case and failing rule
1 parent 6cb451a commit 4fc4197

File tree

4 files changed

+250
-1
lines changed

4 files changed

+250
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import java.util.*;
2424

25-
public class AvoidNPlusOneProblemInJPAEntitiesCheck {
25+
public class AvoidNPlusOneProblemInJPAEntitiesCheckIssue {
2626

2727
@Autowired
2828
private AuthorRepository authorRepository;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.greencodeinitiative.creedengo.java.checks;
2+
3+
import org.sonar.check.Rule;
4+
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
5+
import org.sonar.plugins.java.api.semantic.MethodMatchers;
6+
import org.sonar.plugins.java.api.tree.*;
7+
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
@Rule(key = "GRC3")
13+
@DeprecatedRuleKey(repositoryKey = "ecocode-java", ruleKey = "GRC3")
14+
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRC3")
15+
public class AvoidNPlusOneProblemInJPAEntitiesCheck extends IssuableSubscriptionVisitor {
16+
protected static final String RULE_MESSAGE = "Avoid N+1 with nested JPA Entities";
17+
18+
private static final String BASE_STREAM = "java.util.stream.BaseStream";
19+
private static final String SPRING_REPOSITORY = "org.springframework.data.repository.Repository";
20+
21+
private static final MethodMatchers SPRING_REPOSITORY_METHOD =
22+
MethodMatchers
23+
.create()
24+
.ofSubTypes(SPRING_REPOSITORY)
25+
.anyName()
26+
.withAnyParameters()
27+
.build();
28+
29+
private static final MethodMatchers STREAM_FOREACH_METHOD =
30+
MethodMatchers
31+
.create()
32+
.ofSubTypes(BASE_STREAM)
33+
.names("forEach", "forEachOrdered", "map", "peek")
34+
.withAnyParameters()
35+
.build();
36+
37+
private final AvoidNPlusOneProblemInJPAEntitiesCheck.AvoidSpringRepositoryCallInLoopCheckVisitor visitorInFile = new AvoidNPlusOneProblemInJPAEntitiesCheck.AvoidSpringRepositoryCallInLoopCheckVisitor();
38+
private final AvoidNPlusOneProblemInJPAEntitiesCheck.StreamVisitor streamVisitor = new AvoidNPlusOneProblemInJPAEntitiesCheck.StreamVisitor();
39+
40+
private final AvoidNPlusOneProblemInJPAEntitiesCheck.AncestorMethodVisitor ancestorMethodVisitor = new AvoidNPlusOneProblemInJPAEntitiesCheck.AncestorMethodVisitor();
41+
42+
@Override
43+
public List<Tree.Kind> nodesToVisit() {
44+
return Arrays.asList(
45+
Tree.Kind.FOR_EACH_STATEMENT, // loop
46+
Tree.Kind.FOR_STATEMENT, // loop
47+
Tree.Kind.WHILE_STATEMENT, // loop
48+
Tree.Kind.DO_STATEMENT, // loop
49+
Tree.Kind.METHOD_INVOCATION // stream
50+
);
51+
}
52+
53+
@Override
54+
public void visitNode(Tree tree) {
55+
if (tree.is(Tree.Kind.METHOD_INVOCATION)) { // stream process
56+
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
57+
if (STREAM_FOREACH_METHOD.matches(methodInvocationTree)) {
58+
tree.accept(streamVisitor);
59+
}
60+
} else { // loop process
61+
tree.accept(visitorInFile);
62+
}
63+
}
64+
65+
private class AvoidSpringRepositoryCallInLoopCheckVisitor extends BaseTreeVisitor {
66+
@Override
67+
public void visitMethodInvocation(MethodInvocationTree tree) {
68+
if (SPRING_REPOSITORY_METHOD.matches(tree)) {
69+
reportIssue(tree, RULE_MESSAGE);
70+
} else {
71+
super.visitMethodInvocation(tree);
72+
}
73+
}
74+
75+
}
76+
77+
private class StreamVisitor extends BaseTreeVisitor {
78+
79+
@Override
80+
public void visitLambdaExpression(LambdaExpressionTree tree) {
81+
tree.accept(ancestorMethodVisitor);
82+
}
83+
84+
}
85+
86+
private class AncestorMethodVisitor extends BaseTreeVisitor {
87+
88+
@Override
89+
public void visitMethodInvocation(MethodInvocationTree tree) {
90+
// if the method is a spring repository method, report an issue
91+
if (SPRING_REPOSITORY_METHOD.matches(tree)) {
92+
reportIssue(tree, RULE_MESSAGE);
93+
} else { // else, check if the method is a method invocation and check recursively
94+
if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
95+
MemberSelectExpressionTree memberSelectTree = (MemberSelectExpressionTree) tree.methodSelect();
96+
if ( memberSelectTree.expression().is(Tree.Kind.METHOD_INVOCATION)) {
97+
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) memberSelectTree.expression();
98+
methodInvocationTree.accept(ancestorMethodVisitor);
99+
}
100+
}
101+
}
102+
}
103+
104+
}
105+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.java.checks;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.data.jpa.repository.JpaRepository;
22+
23+
import java.util.*;
24+
25+
public class AvoidNPlusOneProblemInJPAEntitiesCheckIssue {
26+
27+
@Autowired
28+
private AuthorRepository authorRepository;
29+
30+
public List<Author> smellGetAllAuthors() {
31+
List<Author> authors = authorRepository.findAll();
32+
for (Author author : authors) {
33+
List<Book> books = author.getBooks(); // Cela peut déclencher une requête SQL pour chaque auteur
34+
}
35+
return authors;
36+
}
37+
38+
39+
public class Author {
40+
41+
private Long id;
42+
private String name;
43+
44+
private List<Book> books;
45+
46+
47+
public Long getId() {
48+
return id;
49+
}
50+
51+
public void setId(Long id) {
52+
this.id = id;
53+
}
54+
55+
public String getName() {
56+
return name;
57+
}
58+
59+
public void setName(String name) {
60+
this.name = name;
61+
}
62+
63+
public List<Book> getBooks() {
64+
return books;
65+
}
66+
67+
public void setBooks(List<Book> books) {
68+
this.books = books;
69+
}
70+
}
71+
72+
73+
public class Book {
74+
75+
private Long id;
76+
private String title;
77+
78+
private Author author;
79+
80+
public Long getId() {
81+
return id;
82+
}
83+
84+
public void setId(Long id) {
85+
this.id = id;
86+
}
87+
88+
public String getTitle() {
89+
return title;
90+
}
91+
92+
public void setTitle(String title) {
93+
this.title = title;
94+
}
95+
96+
public Author getAuthor() {
97+
return author;
98+
}
99+
100+
public void setAuthor(Author author) {
101+
this.author = author;
102+
}
103+
}
104+
105+
public interface AuthorRepository extends JpaRepository<Author, Long> {
106+
107+
}
108+
109+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.java.checks;
19+
20+
import org.greencodeinitiative.creedengo.java.utils.FilesUtils;
21+
import org.junit.jupiter.api.Test;
22+
import org.sonar.java.checks.verifier.CheckVerifier;
23+
24+
class AvoidNPlusOneProblemInJPAEntitiesCheckTest {
25+
26+
@Test
27+
void test() {
28+
CheckVerifier.newVerifier()
29+
.onFile("src/test/files/AvoidNPlusOneProblemInJPAEntitiesCheckIssue.java")
30+
.withCheck(new AvoidNPlusOneProblemInJPAEntitiesCheck())
31+
.withClassPath(FilesUtils.getClassPath("target/test-jars"))
32+
.verifyIssues();
33+
}
34+
35+
}

0 commit comments

Comments
 (0)