From 8dc9d67943138e848f18c99d577d585b1ee8324b Mon Sep 17 00:00:00 2001 From: mohamed qelai Date: Wed, 5 Apr 2023 16:04:20 +0200 Subject: [PATCH 1/9] first steps --- .../java/checks/FetchTypeLazyCheck.java | 32 +++++++++++++++++++ .../src/test/files/FetchTypeLazyCheck.java | 19 +++++++++++ .../java/checks/FetchTypeLazyCheckTest.java | 16 ++++++++++ 3 files changed, 67 insertions(+) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java create mode 100644 java-plugin/src/test/files/FetchTypeLazyCheck.java create mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java new file mode 100644 index 000000000..cc8ae4039 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java @@ -0,0 +1,32 @@ +package fr.greencodeinitiative.java.checks; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.tree.Tree; + +import java.lang.reflect.AnnotatedType; +import java.util.List; + +import static java.util.Collections.singletonList; + +@Rule( + key = "CRJVM205", + name = "Developpement", + description = AvoidFullSQLRequest.MESSAGERULE, + priority = Priority.MINOR, + tags = {"bug"}) +public class FetchTypeLazyCheck extends IssuableSubscriptionVisitor { + protected static final String MESSAGERULE = "Use lazy fetch type instead of egger "; + + @Override + public List nodesToVisit() { + return singletonList(Tree.Kind.ANNOTATION); + } + + @Override + public void visitNode(Tree tree) { + AnnotatedType ann = tree.kind().getDeclaringClass().getAnnotatedSuperclass(); + System.out.println(ann); + } +} diff --git a/java-plugin/src/test/files/FetchTypeLazyCheck.java b/java-plugin/src/test/files/FetchTypeLazyCheck.java new file mode 100644 index 000000000..d03f0c73e --- /dev/null +++ b/java-plugin/src/test/files/FetchTypeLazyCheck.java @@ -0,0 +1,19 @@ +package fr.greencodeinitiative.java.checks; + +import java.util.regex.Pattern; +@Entity +@Table (name = "ECO_CODE") +class FetchTypeLazyCheck { + FetchTypeLazyCheck(FetchTypeLazyCheck mc) { + } + + @Id + @GeneratedValue + @Column(name="ORDER_ID") + private Long orderId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="USER_ID") + private UserLazy user; + +} \ No newline at end of file diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java new file mode 100644 index 000000000..bc1ea731a --- /dev/null +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java @@ -0,0 +1,16 @@ +package fr.greencodeinitiative.java.checks; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; + +public class FetchTypeLazyCheckTest { + + @Test + void test() { + CheckVerifier.newVerifier() + .onFile("src/test/files/FetchTypeLazyCheck.java") + .withCheck(new FetchTypeLazyCheck()) + .verifyIssues(); + } + +} From ac91aaa624c23151de9ccc9abb071ae158ca86c0 Mon Sep 17 00:00:00 2001 From: Younes DJOUAMBI Date: Wed, 5 Apr 2023 17:25:46 +0200 Subject: [PATCH 2/9] implementation rule --- .../java/checks/FetchTypeLazyCheck.java | 34 ++++++++++++++++--- .../src/test/files/FetchTypeLazyCheck.java | 4 +-- .../java/checks/FetchTypeLazyCheckTest.java | 2 +- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java index cc8ae4039..f71797f1b 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java @@ -2,8 +2,13 @@ import org.sonar.check.Priority; import org.sonar.check.Rule; +import org.sonar.java.ast.parser.ArgumentListTreeImpl; +import org.sonar.java.model.declaration.AnnotationTreeImpl; +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.Tree; +import org.sonar.plugins.java.api.tree.TypeTree; import java.lang.reflect.AnnotatedType; import java.util.List; @@ -15,18 +20,37 @@ name = "Developpement", description = AvoidFullSQLRequest.MESSAGERULE, priority = Priority.MINOR, - tags = {"bug"}) + tags = { "bug" } ) public class FetchTypeLazyCheck extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Use lazy fetch type instead of egger "; + public static final String MANY_TO_ONE = "ManyToOne"; + public static final String LAZY = "LAZY"; @Override public List nodesToVisit() { - return singletonList(Tree.Kind.ANNOTATION); + return singletonList( Tree.Kind.ANNOTATION ); } @Override - public void visitNode(Tree tree) { - AnnotatedType ann = tree.kind().getDeclaringClass().getAnnotatedSuperclass(); - System.out.println(ann); + public void visitNode( Tree tree ) { + TypeTree typeTree = (( AnnotationTreeImpl ) tree).annotationType(); + if ( MANY_TO_ONE.equals( typeTree.symbolType().name() ) ) { + List annotationListTree = (( AnnotationTreeImpl ) tree).children(); + if ( !annotationListTree.isEmpty() && annotationListTree.size() > 1 ) { + ArgumentListTreeImpl argumentListTree = ( ArgumentListTreeImpl ) annotationListTree.get( 2 ); + List argumentListTreeChildren = argumentListTree.getChildren(); + if ( !argumentListTreeChildren.isEmpty() && argumentListTreeChildren.size() > 1 ) { + AssignmentExpressionTreeImpl assignmentExpressionTree = ( AssignmentExpressionTreeImpl ) argumentListTreeChildren.get( 1 ); + List assignmentExpressionTreeChildren = assignmentExpressionTree.getChildren(); + if ( !assignmentExpressionTreeChildren.isEmpty() && assignmentExpressionTreeChildren.size() > 1 ) { + MemberSelectExpressionTreeImpl memberSelectExpressionTree = ( MemberSelectExpressionTreeImpl ) assignmentExpressionTreeChildren.get( 2 ); + if ( ! LAZY.equals( memberSelectExpressionTree.identifier().name() ) ) { + reportIssue( tree, MESSAGERULE ); + } + } + } + } + } + } } diff --git a/java-plugin/src/test/files/FetchTypeLazyCheck.java b/java-plugin/src/test/files/FetchTypeLazyCheck.java index d03f0c73e..00c31c6b7 100644 --- a/java-plugin/src/test/files/FetchTypeLazyCheck.java +++ b/java-plugin/src/test/files/FetchTypeLazyCheck.java @@ -15,5 +15,5 @@ class FetchTypeLazyCheck { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="USER_ID") private UserLazy user; - -} \ No newline at end of file + +} diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java index bc1ea731a..42421d6e1 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java @@ -10,7 +10,7 @@ void test() { CheckVerifier.newVerifier() .onFile("src/test/files/FetchTypeLazyCheck.java") .withCheck(new FetchTypeLazyCheck()) - .verifyIssues(); + .verifyNoIssues(); } } From 3facbee366e5d626c9bdfc64778ba51ff691aaca Mon Sep 17 00:00:00 2001 From: Younes DJOUAMBI Date: Wed, 5 Apr 2023 17:46:48 +0200 Subject: [PATCH 3/9] clean code --- .../java/checks/FetchTypeLazyCheck.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java index f71797f1b..b8dc24465 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java @@ -10,7 +10,6 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.TypeTree; -import java.lang.reflect.AnnotatedType; import java.util.List; import static java.util.Collections.singletonList; @@ -22,9 +21,9 @@ priority = Priority.MINOR, tags = { "bug" } ) public class FetchTypeLazyCheck extends IssuableSubscriptionVisitor { + public static final String MANY_TO_ONE = "ManyToOne"; + public static final String LAZY = "LAZY"; protected static final String MESSAGERULE = "Use lazy fetch type instead of egger "; - public static final String MANY_TO_ONE = "ManyToOne"; - public static final String LAZY = "LAZY"; @Override public List nodesToVisit() { @@ -34,7 +33,8 @@ public List nodesToVisit() { @Override public void visitNode( Tree tree ) { TypeTree typeTree = (( AnnotationTreeImpl ) tree).annotationType(); - if ( MANY_TO_ONE.equals( typeTree.symbolType().name() ) ) { + if ( MANY_TO_ONE.equals( typeTree.symbolType() + .name() ) ) { List annotationListTree = (( AnnotationTreeImpl ) tree).children(); if ( !annotationListTree.isEmpty() && annotationListTree.size() > 1 ) { ArgumentListTreeImpl argumentListTree = ( ArgumentListTreeImpl ) annotationListTree.get( 2 ); @@ -43,8 +43,10 @@ public void visitNode( Tree tree ) { AssignmentExpressionTreeImpl assignmentExpressionTree = ( AssignmentExpressionTreeImpl ) argumentListTreeChildren.get( 1 ); List assignmentExpressionTreeChildren = assignmentExpressionTree.getChildren(); if ( !assignmentExpressionTreeChildren.isEmpty() && assignmentExpressionTreeChildren.size() > 1 ) { - MemberSelectExpressionTreeImpl memberSelectExpressionTree = ( MemberSelectExpressionTreeImpl ) assignmentExpressionTreeChildren.get( 2 ); - if ( ! LAZY.equals( memberSelectExpressionTree.identifier().name() ) ) { + MemberSelectExpressionTreeImpl memberSelectExpressionTree = ( MemberSelectExpressionTreeImpl ) assignmentExpressionTreeChildren.get( + 2 ); + if ( !LAZY.equals( memberSelectExpressionTree.identifier() + .name() ) ) { reportIssue( tree, MESSAGERULE ); } } From b1a534b4f9ce41c5bf61ec7590deca7f127e54f8 Mon Sep 17 00:00:00 2001 From: Younes DJOUAMBI Date: Wed, 5 Apr 2023 18:16:53 +0200 Subject: [PATCH 4/9] code refacto --- .../java/checks/FetchTypeLazyCheck.java | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java index b8dc24465..18bb8b407 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java @@ -30,29 +30,43 @@ public List nodesToVisit() { return singletonList( Tree.Kind.ANNOTATION ); } + @Override public void visitNode( Tree tree ) { TypeTree typeTree = (( AnnotationTreeImpl ) tree).annotationType(); - if ( MANY_TO_ONE.equals( typeTree.symbolType() - .name() ) ) { - List annotationListTree = (( AnnotationTreeImpl ) tree).children(); - if ( !annotationListTree.isEmpty() && annotationListTree.size() > 1 ) { - ArgumentListTreeImpl argumentListTree = ( ArgumentListTreeImpl ) annotationListTree.get( 2 ); - List argumentListTreeChildren = argumentListTree.getChildren(); - if ( !argumentListTreeChildren.isEmpty() && argumentListTreeChildren.size() > 1 ) { - AssignmentExpressionTreeImpl assignmentExpressionTree = ( AssignmentExpressionTreeImpl ) argumentListTreeChildren.get( 1 ); - List assignmentExpressionTreeChildren = assignmentExpressionTree.getChildren(); - if ( !assignmentExpressionTreeChildren.isEmpty() && assignmentExpressionTreeChildren.size() > 1 ) { - MemberSelectExpressionTreeImpl memberSelectExpressionTree = ( MemberSelectExpressionTreeImpl ) assignmentExpressionTreeChildren.get( - 2 ); - if ( !LAZY.equals( memberSelectExpressionTree.identifier() - .name() ) ) { - reportIssue( tree, MESSAGERULE ); - } - } - } - } + if ( MANY_TO_ONE.equals( typeTree.symbolType().name() ) ) { + visitAnnotations( tree ); + } + } + + private void visitAnnotations( final Tree tree ) { + List annotationListTree = (( AnnotationTreeImpl ) tree).children(); + if ( !annotationListTree.isEmpty() && annotationListTree.size() > 1 ) { + ArgumentListTreeImpl argumentListTree = ( ArgumentListTreeImpl ) annotationListTree.get( 2 ); + visitArguments( argumentListTree, tree ); + } + } + + private void visitArguments( final ArgumentListTreeImpl argumentListTree, final Tree tree ) { + List argumentListTreeChildren = argumentListTree.getChildren(); + if ( !argumentListTreeChildren.isEmpty() && argumentListTreeChildren.size() > 1 ) { + AssignmentExpressionTreeImpl assignmentExpressionTree = ( AssignmentExpressionTreeImpl ) argumentListTreeChildren.get( 1 ); + visitAssignements( assignmentExpressionTree, tree ); + } + } + + private void visitAssignements( final AssignmentExpressionTreeImpl assignmentExpressionTree, final Tree tree ) { + List assignmentExpressionTreeChildren = assignmentExpressionTree.getChildren(); + if ( !assignmentExpressionTreeChildren.isEmpty() && assignmentExpressionTreeChildren.size() > 1 ) { + MemberSelectExpressionTreeImpl memberSelectExpressionTree = ( MemberSelectExpressionTreeImpl ) assignmentExpressionTreeChildren.get( 2 ); + checkFetchTypeManyToOne( tree, memberSelectExpressionTree ); } + } + private void checkFetchTypeManyToOne( final Tree tree, final MemberSelectExpressionTreeImpl memberSelectExpressionTree ) { + if ( !LAZY.equals( memberSelectExpressionTree.identifier() + .name() ) ) { + reportIssue( tree, MESSAGERULE ); + } } } From 87b7a54bc2e2caeb21c7d6a07583432f24b0b7ae Mon Sep 17 00:00:00 2001 From: Younes DJOUAMBI Date: Thu, 6 Apr 2023 10:02:04 +0200 Subject: [PATCH 5/9] doc html --- .../l10n/java/rules/java/CRJVM205.html | 24 +++++++++++++++++++ .../l10n/java/rules/java/CRJVM205.json | 15 ++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html create mode 100644 java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html new file mode 100644 index 000000000..f8801ef21 --- /dev/null +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html @@ -0,0 +1,24 @@ +

try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources.

+

Noncompliant Code Example

+
+    private static void printFileJava7() throws IOException {
+        FileInputStream input = new FileInputStream("file.txt");
+        int data = input.read();
+        while(data != -1){
+            System.out.print((char) data);
+            data = input.read();
+        }
+    }
+
+

Compliant Solution

+
+    private static void printFileJava7() throws IOException {
+        try(FileInputStream input = new FileInputStream("file.txt")) {
+            int data = input.read();
+            while(data != -1){
+                System.out.print((char) data);
+                data = input.read();
+            }
+        }
+    }
+
diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json new file mode 100644 index 000000000..9d74b48fe --- /dev/null +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json @@ -0,0 +1,15 @@ +{ + "title": "Free resources", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "15min" + }, + "tags": [ + "eco-design", + "bug", + "ecocode" + ], + "defaultSeverity": "Minor" +} From 1e51c17f33c6cadc9bcbb33c6d9e35ef594d143b Mon Sep 17 00:00:00 2001 From: Younes DJOUAMBI Date: Thu, 6 Apr 2023 10:02:12 +0200 Subject: [PATCH 6/9] doc html --- .../java/checks/FetchTypeLazyCheck.java | 2 +- .../l10n/java/rules/java/CRJVM205.html | 44 +++++++++++-------- .../l10n/java/rules/java/CRJVM205.json | 8 ++-- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java index 18bb8b407..1a8a4a1ba 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java @@ -23,7 +23,7 @@ public class FetchTypeLazyCheck extends IssuableSubscriptionVisitor { public static final String MANY_TO_ONE = "ManyToOne"; public static final String LAZY = "LAZY"; - protected static final String MESSAGERULE = "Use lazy fetch type instead of egger "; + protected static final String MESSAGERULE = "Use lazy fetch type instead of egger"; @Override public List nodesToVisit() { diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html index f8801ef21..a9bd32368 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html @@ -1,24 +1,32 @@ -

try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources.

+

Use lazy fetch type instead of egger.

Noncompliant Code Example

-    private static void printFileJava7() throws IOException {
-        FileInputStream input = new FileInputStream("file.txt");
-        int data = input.read();
-        while(data != -1){
-            System.out.print((char) data);
-            data = input.read();
-        }
-    }
+  class FetchTypeLazyCheck {
+
+    @ManyToOne(fetch = FetchType.EAGER)
+    @JoinColumn(name="USER_ID")
+    private UserLazy user;
+
+   }
 

Compliant Solution

-    private static void printFileJava7() throws IOException {
-        try(FileInputStream input = new FileInputStream("file.txt")) {
-            int data = input.read();
-            while(data != -1){
-                System.out.print((char) data);
-                data = input.read();
-            }
-        }
-    }
+  class FetchTypeLazyCheck {
+
+        @ManyToOne(fetch = FetchType.LAZY)
+        @JoinColumn(name="USER_ID")
+        private UserLazy user;
+
+   }
+
+    OR
+
+    class FetchTypeLazyCheck {
+
+        @ManyToOne
+        @JoinColumn(name="USER_ID")
+        private UserLazy user;
+
+   }
+
 
diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json index 9d74b48fe..67910bbe4 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json @@ -1,14 +1,16 @@ { - "title": "Free resources", + "title": "Use lazy fetch type instead of egger", "type": "CODE_SMELL", "status": "ready", "remediation": { "func": "Constant\/Issue", - "constantCost": "15min" + "constantCost": "20min" }, "tags": [ "eco-design", - "bug", + "performance", + "orm", + "jpa", "ecocode" ], "defaultSeverity": "Minor" From b99273b46c31c4af4b90a35580111bc0b0a2ed06 Mon Sep 17 00:00:00 2001 From: mohamed qelai Date: Thu, 6 Apr 2023 12:15:26 +0200 Subject: [PATCH 7/9] improve test file --- .../src/test/files/FetchTypeLazyCheckBad.java | 19 +++++++++++++++++++ ...Check.java => FetchTypeLazyCheckGood.java} | 0 .../java/checks/FetchTypeLazyCheckTest.java | 13 +++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 java-plugin/src/test/files/FetchTypeLazyCheckBad.java rename java-plugin/src/test/files/{FetchTypeLazyCheck.java => FetchTypeLazyCheckGood.java} (100%) diff --git a/java-plugin/src/test/files/FetchTypeLazyCheckBad.java b/java-plugin/src/test/files/FetchTypeLazyCheckBad.java new file mode 100644 index 000000000..b64cc7662 --- /dev/null +++ b/java-plugin/src/test/files/FetchTypeLazyCheckBad.java @@ -0,0 +1,19 @@ +package fr.greencodeinitiative.java.checks; + +import java.util.regex.Pattern; +@Entity +@Table (name = "ECO_CODE") +class FetchTypeLazyCheck { + FetchTypeLazyCheck(FetchTypeLazyCheck mc) { + } + + @Id + @GeneratedValue + @Column(name="ORDER_ID") + private Long orderId; + + @ManyToOne(fetch = FetchType.EAGER) // Noncompliant {{Use lazy fetch type instead of egger}} + @JoinColumn(name="USER_ID") + private UserLazy user; + +} diff --git a/java-plugin/src/test/files/FetchTypeLazyCheck.java b/java-plugin/src/test/files/FetchTypeLazyCheckGood.java similarity index 100% rename from java-plugin/src/test/files/FetchTypeLazyCheck.java rename to java-plugin/src/test/files/FetchTypeLazyCheckGood.java diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java index 42421d6e1..76e18e29f 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java @@ -6,11 +6,20 @@ public class FetchTypeLazyCheckTest { @Test - void test() { + void testNoIssues() { CheckVerifier.newVerifier() - .onFile("src/test/files/FetchTypeLazyCheck.java") + .onFile("src/test/files/FetchTypeLazyCheckGood.java") .withCheck(new FetchTypeLazyCheck()) .verifyNoIssues(); } + + @Test + void testIssues() { + CheckVerifier.newVerifier() + .onFile("src/test/files/FetchTypeLazyCheckBad.java") + .withCheck(new FetchTypeLazyCheck()) + .verifyIssues(); + } + } From 19f43fea5d4ce1ff45624c51c0cf5f081df4bae1 Mon Sep 17 00:00:00 2001 From: mohamed qelai Date: Thu, 6 Apr 2023 13:51:54 +0200 Subject: [PATCH 8/9] modify class visibility --- RULES.md | 3 ++- .../java/checks/FetchTypeLazyCheckTest.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/RULES.md b/RULES.md index d3add4cb8..3d20d562a 100644 --- a/RULES.md +++ b/RULES.md @@ -14,7 +14,7 @@ Some are applicable for different technologies. | | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | | | Non-grouped similar CSS declarations | When multiple Document Object Model (DOM) elements have common CSS properties, declare them together in the same style sheet. This method reduces the weight of CSS. |[cnumr best practices (3rd edition) BP_025](https://github.com/cnumr/best-practices/blob/main/chapters/BP_025_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | | | CSS shorthand notations not used | Reduces the weight of the style sheet. | [cnumr best practices (3rd edition) BP_026](https://github.com/cnumr/best-practices/blob/main/chapters/BP_026_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| | CSS print not included | This style sheet reduces the number of pages printed. | [cnumr best practices (3rd edition) BP_027](https://github.com/cnumr/best-practices/blob/main/chapters/BP_027_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| | CSS[INSTALL.md](INSTALL.md) print not included | This style sheet reduces the number of pages printed. | [cnumr best practices (3rd edition) BP_027](https://github.com/cnumr/best-practices/blob/main/chapters/BP_027_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | | | Non-standard fonts used | Prefer standard fonts, as they are already present on the user's computer, so they do not need to download them. This saves bandwidth, while speeding up the display of the site. | [cnumr best practices (3rd edition) BP_029](https://github.com/cnumr/best-practices/blob/main/chapters/BP_029_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | | | Non-outsourced CSS and Javascript | If you include CSS or JavaScript code in the body of the HTML file, while the HTML file is used by several pages (or even the entire site), this code must be transferred for each page requested by the user, which increases the volume of data transmitted. | [cnumr best practices (3rd edition) BP_032](https://github.com/cnumr/best-practices/blob/main/chapters/BP_032_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | | | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | @@ -47,3 +47,4 @@ Some are applicable for different technologies. | EC28 | Optimize read file exceptions | | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | | EC5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | | EC27 | Usage of system.arraycopy to copy arrays | Programs spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | +|CRJVM205| Use lazy fetch type instead of egger \ No newline at end of file diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java index 76e18e29f..ce4e32e5a 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheckTest.java @@ -3,7 +3,7 @@ import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; -public class FetchTypeLazyCheckTest { +class FetchTypeLazyCheckTest { @Test void testNoIssues() { From d7b8d0e4939a2667d043f3e27fa9a7bb9213e9b9 Mon Sep 17 00:00:00 2001 From: mohamed qelai Date: Thu, 6 Apr 2023 14:40:35 +0200 Subject: [PATCH 9/9] add Hibernate relationships --- .../java/checks/FetchTypeLazyCheck.java | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java index 1a8a4a1ba..2a9131a84 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FetchTypeLazyCheck.java @@ -19,54 +19,55 @@ name = "Developpement", description = AvoidFullSQLRequest.MESSAGERULE, priority = Priority.MINOR, - tags = { "bug" } ) + tags = {"bug"}) public class FetchTypeLazyCheck extends IssuableSubscriptionVisitor { - public static final String MANY_TO_ONE = "ManyToOne"; - public static final String LAZY = "LAZY"; + public static final String MANY_TO_ONE = "ManyToOne"; + public static final String LAZY = "LAZY"; + public static final String ONE_TO_MANY = "OneToMany"; protected static final String MESSAGERULE = "Use lazy fetch type instead of egger"; @Override public List nodesToVisit() { - return singletonList( Tree.Kind.ANNOTATION ); + return singletonList(Tree.Kind.ANNOTATION); } @Override - public void visitNode( Tree tree ) { - TypeTree typeTree = (( AnnotationTreeImpl ) tree).annotationType(); - if ( MANY_TO_ONE.equals( typeTree.symbolType().name() ) ) { - visitAnnotations( tree ); + public void visitNode(Tree tree) { + TypeTree typeTree = ((AnnotationTreeImpl) tree).annotationType(); + if (MANY_TO_ONE.equals(typeTree.symbolType().name()) || ONE_TO_MANY.equals(typeTree.symbolType().name()) || "ManyToMany".equals(typeTree.symbolType().name())) { + visitAnnotations(tree); } } - private void visitAnnotations( final Tree tree ) { - List annotationListTree = (( AnnotationTreeImpl ) tree).children(); - if ( !annotationListTree.isEmpty() && annotationListTree.size() > 1 ) { - ArgumentListTreeImpl argumentListTree = ( ArgumentListTreeImpl ) annotationListTree.get( 2 ); - visitArguments( argumentListTree, tree ); + private void visitAnnotations(final Tree tree) { + List annotationListTree = ((AnnotationTreeImpl) tree).children(); + if (!annotationListTree.isEmpty() && annotationListTree.size() > 1) { + ArgumentListTreeImpl argumentListTree = (ArgumentListTreeImpl) annotationListTree.get(2); + visitArguments(argumentListTree, tree); } } - private void visitArguments( final ArgumentListTreeImpl argumentListTree, final Tree tree ) { + private void visitArguments(final ArgumentListTreeImpl argumentListTree, final Tree tree) { List argumentListTreeChildren = argumentListTree.getChildren(); - if ( !argumentListTreeChildren.isEmpty() && argumentListTreeChildren.size() > 1 ) { - AssignmentExpressionTreeImpl assignmentExpressionTree = ( AssignmentExpressionTreeImpl ) argumentListTreeChildren.get( 1 ); - visitAssignements( assignmentExpressionTree, tree ); + if (!argumentListTreeChildren.isEmpty() && argumentListTreeChildren.size() > 1) { + AssignmentExpressionTreeImpl assignmentExpressionTree = (AssignmentExpressionTreeImpl) argumentListTreeChildren.get(1); + visitAssignements(assignmentExpressionTree, tree); } } - private void visitAssignements( final AssignmentExpressionTreeImpl assignmentExpressionTree, final Tree tree ) { + private void visitAssignements(final AssignmentExpressionTreeImpl assignmentExpressionTree, final Tree tree) { List assignmentExpressionTreeChildren = assignmentExpressionTree.getChildren(); - if ( !assignmentExpressionTreeChildren.isEmpty() && assignmentExpressionTreeChildren.size() > 1 ) { - MemberSelectExpressionTreeImpl memberSelectExpressionTree = ( MemberSelectExpressionTreeImpl ) assignmentExpressionTreeChildren.get( 2 ); - checkFetchTypeManyToOne( tree, memberSelectExpressionTree ); + if (!assignmentExpressionTreeChildren.isEmpty() && assignmentExpressionTreeChildren.size() > 1) { + MemberSelectExpressionTreeImpl memberSelectExpressionTree = (MemberSelectExpressionTreeImpl) assignmentExpressionTreeChildren.get(2); + checkFetchTypeManyToOne(tree, memberSelectExpressionTree); } } - private void checkFetchTypeManyToOne( final Tree tree, final MemberSelectExpressionTreeImpl memberSelectExpressionTree ) { - if ( !LAZY.equals( memberSelectExpressionTree.identifier() - .name() ) ) { - reportIssue( tree, MESSAGERULE ); + private void checkFetchTypeManyToOne(final Tree tree, final MemberSelectExpressionTreeImpl memberSelectExpressionTree) { + if (!LAZY.equals(memberSelectExpressionTree.identifier() + .name())) { + reportIssue(tree, MESSAGERULE); } } }