From 3db84785e164272c0fba67b8badc724590b2c08b Mon Sep 17 00:00:00 2001 From: Ashvin Appigadoo Date: Wed, 29 May 2024 15:30:24 +0200 Subject: [PATCH 1/3] Mise en place check pour eviter les eager fetch --- .../java/checks/AvoidEagerFetchCheck.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java b/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java new file mode 100644 index 0000000..1ddb0f6 --- /dev/null +++ b/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java @@ -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 . + */ +package fr.greencodeinitiative.java.checks; + +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 java.util.List; +import java.util.function.Predicate; + +import static java.util.Collections.singletonList; +import static java.util.regex.Pattern.CASE_INSENSITIVE; +import static java.util.regex.Pattern.compile; + +@Rule(key = "GRC2") +public class AvoidEagerFetchCheck extends IssuableSubscriptionVisitor { + + protected static final String MESSAGERULE = "Privilege the use of Lazy Fetch"; + private static final Predicate EAGER_FETCH = + compile("FetchType.EAGER", CASE_INSENSITIVE).asPredicate(); //simple regexp, more precision + + @Override + public List nodesToVisit() { + return singletonList(Kind.ANNOTATION); + } + + @Override + public void visitNode(Tree tree) { + String value = ((LiteralTree) tree).value(); + if (EAGER_FETCH.test(value)) { + reportIssue(tree, MESSAGERULE); + } + } +} From 70985551beaa0be2fa0740e4b24b5a0a4b7afd37 Mon Sep 17 00:00:00 2001 From: Ashvin Appigadoo Date: Wed, 29 May 2024 16:23:30 +0200 Subject: [PATCH 2/3] Add unit tests --- .../java/checks/AvoidEagerFetchCheck.java | 9 +++-- src/test/files/AvoidEagerFetchCheck.java | 31 +++++++++++++++++ .../java/checks/AvoidEagerFetchCheckTest.java | 33 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/test/files/AvoidEagerFetchCheck.java create mode 100644 src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java b/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java index 1ddb0f6..11ecaa3 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java @@ -18,6 +18,8 @@ package fr.greencodeinitiative.java.checks; import org.sonar.check.Rule; +import org.sonar.java.model.expression.AssignmentExpressionTreeImpl; +import org.sonar.java.model.expression.MemberSelectExpressionTreeImpl; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.LiteralTree; import org.sonar.plugins.java.api.tree.Tree; @@ -35,16 +37,17 @@ public class AvoidEagerFetchCheck extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Privilege the use of Lazy Fetch"; private static final Predicate EAGER_FETCH = - compile("FetchType.EAGER", CASE_INSENSITIVE).asPredicate(); //simple regexp, more precision + compile("EAGER", CASE_INSENSITIVE).asPredicate(); //simple regexp, more precision @Override public List nodesToVisit() { - return singletonList(Kind.ANNOTATION); + return singletonList(Kind.ASSIGNMENT); } @Override public void visitNode(Tree tree) { - String value = ((LiteralTree) tree).value(); + Tree treeFirstLevel = ((AssignmentExpressionTreeImpl) tree).getChildren().get(2); + String value = ((MemberSelectExpressionTreeImpl) treeFirstLevel).identifier().toString(); if (EAGER_FETCH.test(value)) { reportIssue(tree, MESSAGERULE); } diff --git a/src/test/files/AvoidEagerFetchCheck.java b/src/test/files/AvoidEagerFetchCheck.java new file mode 100644 index 0000000..b028ad0 --- /dev/null +++ b/src/test/files/AvoidEagerFetchCheck.java @@ -0,0 +1,31 @@ +/* + * 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 . + */ +package fr.greencodeinitiative.java.checks; + +import java.util.regex.Pattern; + +class AvoidEagerFetchCheck { + AvoidEagerFetchCheck(AvoidEagerFetchCheck mc) { + } + + public void UserEntity() { + @ManyToOne(fetch = FetchType.LAZY) // Noncompliant {{Privilege the use of Lazy Fetch}} + String requestNonCompiliant; + } + +} \ No newline at end of file diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java b/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java new file mode 100644 index 0000000..27d01dc --- /dev/null +++ b/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java @@ -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 . + */ +package fr.greencodeinitiative.java.checks; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; + +class AvoidEagerFetchCheckTest { + + @Test + void test() { + CheckVerifier.newVerifier() + .onFile("src/test/files/AvoidEagerFetchCheck.java") + .withCheck(new AvoidEagerFetchCheck()) + .verifyNoIssues(); + } + +} \ No newline at end of file From 0c0e6118941d8e9b36aad9cd281a84735792ac4b Mon Sep 17 00:00:00 2001 From: Ashvin Appigadoo Date: Thu, 30 May 2024 09:40:48 +0200 Subject: [PATCH 3/3] Add unit tests --- .../java/checks/AvoidEagerFetchCheck.java | 14 +++++--- ...oidEagerFetchCheckWithManyAnnotations.java | 32 +++++++++++++++++++ ...a => AvoidEagerFetchCheckWithNoEager.java} | 9 +++--- .../java/checks/AvoidEagerFetchCheckTest.java | 4 +-- .../checks/AvoidEagerFetchCheckTestGood.java | 32 +++++++++++++++++++ 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 src/test/files/AvoidEagerFetchCheckWithManyAnnotations.java rename src/test/files/{AvoidEagerFetchCheck.java => AvoidEagerFetchCheckWithNoEager.java} (80%) create mode 100644 src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTestGood.java diff --git a/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java b/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java index 11ecaa3..af3fb50 100644 --- a/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java +++ b/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java @@ -32,10 +32,12 @@ import static java.util.regex.Pattern.CASE_INSENSITIVE; import static java.util.regex.Pattern.compile; -@Rule(key = "GRC2") +@Rule(key = "EC1111") public class AvoidEagerFetchCheck extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Privilege the use of Lazy Fetch"; + + protected static final int FETCH_TYPE = 2; private static final Predicate EAGER_FETCH = compile("EAGER", CASE_INSENSITIVE).asPredicate(); //simple regexp, more precision @@ -46,10 +48,12 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { - Tree treeFirstLevel = ((AssignmentExpressionTreeImpl) tree).getChildren().get(2); - String value = ((MemberSelectExpressionTreeImpl) treeFirstLevel).identifier().toString(); - if (EAGER_FETCH.test(value)) { - reportIssue(tree, MESSAGERULE); + Tree treeFirstLevel = ((AssignmentExpressionTreeImpl) tree).getChildren().get(FETCH_TYPE); + if (treeFirstLevel instanceof MemberSelectExpressionTreeImpl) { + String value = ((MemberSelectExpressionTreeImpl) treeFirstLevel).identifier().toString(); + if (EAGER_FETCH.test(value)) { + reportIssue(tree, MESSAGERULE); + } } } } diff --git a/src/test/files/AvoidEagerFetchCheckWithManyAnnotations.java b/src/test/files/AvoidEagerFetchCheckWithManyAnnotations.java new file mode 100644 index 0000000..254f573 --- /dev/null +++ b/src/test/files/AvoidEagerFetchCheckWithManyAnnotations.java @@ -0,0 +1,32 @@ +/* + * 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 . + */ +package fr.greencodeinitiative.java.checks; + +import java.util.regex.Pattern; + +class AvoidEagerFetchCheckWithManyAnnotations { + AvoidEagerFetchCheckWithManyAnnotations(AvoidEagerFetchCheckWithManyAnnotations mc) { + } + + public void UserEntity() { + + @ManyToOne(mappedBy = test, fetch = FetchType.EAGER) // Noncompliant {{Privilege the use of Lazy Fetch}} + String commandReference; + } + +} \ No newline at end of file diff --git a/src/test/files/AvoidEagerFetchCheck.java b/src/test/files/AvoidEagerFetchCheckWithNoEager.java similarity index 80% rename from src/test/files/AvoidEagerFetchCheck.java rename to src/test/files/AvoidEagerFetchCheckWithNoEager.java index b028ad0..740abb0 100644 --- a/src/test/files/AvoidEagerFetchCheck.java +++ b/src/test/files/AvoidEagerFetchCheckWithNoEager.java @@ -19,13 +19,14 @@ import java.util.regex.Pattern; -class AvoidEagerFetchCheck { - AvoidEagerFetchCheck(AvoidEagerFetchCheck mc) { +class AvoidEagerFetchCheckWithManyAnnotations { + AvoidEagerFetchCheckWithManyAnnotations(AvoidEagerFetchCheckWithManyAnnotations mc) { } public void UserEntity() { - @ManyToOne(fetch = FetchType.LAZY) // Noncompliant {{Privilege the use of Lazy Fetch}} - String requestNonCompiliant; + + @ManyToOne(mappedBy = test, fetch = FetchType.LAZY) + String commandReference; } } \ No newline at end of file diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java b/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java index 27d01dc..5e2866c 100644 --- a/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java +++ b/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTest.java @@ -25,9 +25,9 @@ class AvoidEagerFetchCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidEagerFetchCheck.java") + .onFile("src/test/files/AvoidEagerFetchCheckWithManyAnnotations.java") .withCheck(new AvoidEagerFetchCheck()) - .verifyNoIssues(); + .verifyIssues(); } } \ No newline at end of file diff --git a/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTestGood.java b/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTestGood.java new file mode 100644 index 0000000..e23191f --- /dev/null +++ b/src/test/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheckTestGood.java @@ -0,0 +1,32 @@ +/* + * 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 . + */ +package fr.greencodeinitiative.java.checks; +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; + +class AvoidEagerFetchCheckTestGood { + + @Test + void test() { + CheckVerifier.newVerifier() + .onFile("src/test/files/AvoidEagerFetchCheckWithNoEager.java") + .withCheck(new AvoidEagerFetchCheck()) + .verifyNoIssues(); + } + +} \ No newline at end of file