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..af3fb50 --- /dev/null +++ b/src/main/java/fr/greencodeinitiative/java/checks/AvoidEagerFetchCheck.java @@ -0,0 +1,59 @@ +/* + * 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.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; +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 = "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 + + @Override + public List nodesToVisit() { + return singletonList(Kind.ASSIGNMENT); + } + + @Override + public void visitNode(Tree tree) { + 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/AvoidEagerFetchCheckWithNoEager.java b/src/test/files/AvoidEagerFetchCheckWithNoEager.java new file mode 100644 index 0000000..740abb0 --- /dev/null +++ b/src/test/files/AvoidEagerFetchCheckWithNoEager.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.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 new file mode 100644 index 0000000..5e2866c --- /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/AvoidEagerFetchCheckWithManyAnnotations.java") + .withCheck(new AvoidEagerFetchCheck()) + .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