From 8969840330e2d491eec12c39db7c83d3c169fd2e Mon Sep 17 00:00:00 2001 From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com> Date: Wed, 5 Apr 2023 12:22:10 +0200 Subject: [PATCH 01/31] Create ForceLazyFetchTypeUse.java --- .../src/test/files/ForceLazyFetchTypeUse.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUse.java diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java b/java-plugin/src/test/files/ForceLazyFetchTypeUse.java new file mode 100644 index 000000000..3feface0d --- /dev/null +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUse.java @@ -0,0 +1,21 @@ +@Entity +@Table(name = "purchaseOrder") +public class Order implements Serializable { + + @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} + private Set items = new HashSet(); + + ... + +} + +@Entity +@Table(name = "purchaseOrder") +public class Order implements Serializable { + + @OneToMany(mappedBy = "order", fetch = FetchType.LAZY) // Ignored => compliant + private Set items = new HashSet(); + + ... + +} From bda60fe66ec335d34e18ec987931fdc492ee3374 Mon Sep 17 00:00:00 2001 From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com> Date: Wed, 5 Apr 2023 13:26:22 +0200 Subject: [PATCH 02/31] Creation of html and json files --- .../l10n/java/rules/java/CRJVM205.html | 30 +++++++++++++++++++ .../l10n/java/rules/java/CRJVM205.json | 16 ++++++++++ 2 files changed, 46 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..a90529a49 --- /dev/null +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html @@ -0,0 +1,30 @@ +

Consider forcing the use of LAZY's FetchType on collections in JPA type entities. To reduce RAM load.

+ +

Non-compliant Code Example

+
+    @Entity
+    @Table(name = "purchaseOrder")
+    public class Order implements Serializable {
+       
+      @OneToMany(mappedBy = "order", fetch = FetchType.EAGER)
+      private Set items = new HashSet();
+       
+      ...
+       
+    }
+
+
+

Compliant Code Example

+
+    @Entity
+    @Table(name = "purchaseOrder")
+    public class Order implements Serializable {
+       
+      @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
+      private Set items = new HashSet();
+       
+      ...
+       
+    }
+
+
\ No newline at end of file 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..df71a8247 --- /dev/null +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json @@ -0,0 +1,16 @@ +{ + "title": "Force the use of FetchType LAZY on collections in Entity JPA", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "bug", + "ecocode" + ], + "defaultSeverity": "Minor" + } \ No newline at end of file From 36cef9166e0a125fbb77625d4109a2d60417fd55 Mon Sep 17 00:00:00 2001 From: WalidKrifi Date: Wed, 5 Apr 2023 13:27:17 +0200 Subject: [PATCH 03/31] WKR-Add Rule structure dev --- .../ForceUsingLazyFetchTypeInJPAEntity.java | 50 +++++++++++++++++++ .../src/test/files/ForceLazyFetchTypeUse.java | 27 +++++----- ...orceUsingLazyFetchTypeInJPAEntityTest.java | 26 ++++++++++ 3 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java create mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java new file mode 100644 index 000000000..9304e09f0 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -0,0 +1,50 @@ +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.semantic.MethodMatchers; +import org.sonar.plugins.java.api.tree.*; +import org.sonar.plugins.java.api.tree.Tree.Kind; + +import java.sql.PreparedStatement; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import static fr.greencodeinitiative.java.checks.ConstOrLiteralDeclare.isLiteral; +import static java.util.Arrays.asList; +import static org.sonar.plugins.java.api.semantic.Type.Primitives.INT; +import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT; +import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION; + +@Rule(key = "CRJVM205", name = "Developpement", + description = ForceUsingLazyFetchTypeInJPAEntity.MESSAGERULE, + priority = Priority.MINOR, + tags = {"bug"}) + +public class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisitor { + + protected static final String MESSAGERULE = "Force the use of LAZY FetchType"; + + @Override + public List nodesToVisit() { + return Arrays.asList(Kind.VARIABLE); + } + + @Override + public void visitNode(Tree tree) { + + if (tree.is(Kind.VARIABLE)) { + VariableTree variableTree = (VariableTree) tree; + List listOfAnnotations = variableTree.type().annotations(); + for(AnnotationTree annotationTree : listOfAnnotations){ + + System.out.print(annotationTree.toString()); + } + reportIssue(tree, MESSAGERULE); + } + } + + +} diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java b/java-plugin/src/test/files/ForceLazyFetchTypeUse.java index 3feface0d..786845e2c 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUse.java @@ -1,21 +1,20 @@ +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + @Entity @Table(name = "purchaseOrder") -public class Order implements Serializable { +public class ForceLazyFetchTypeUse implements Serializable { @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} private Set items = new HashSet(); - - ... - -} -@Entity -@Table(name = "purchaseOrder") -public class Order implements Serializable { - - @OneToMany(mappedBy = "order", fetch = FetchType.LAZY) // Ignored => compliant - private Set items = new HashSet(); - - ... - } diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java new file mode 100644 index 000000000..35f471d55 --- /dev/null +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java @@ -0,0 +1,26 @@ +package fr.greencodeinitiative.java.checks; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; + +public class ForceUsingLazyFetchTypeInJPAEntityTest { + + @Test + void checkNonCompliantTests() { + CheckVerifier.newVerifier() + .onFile("src/test/files/ForceLazyFetchTypeUse.java") + .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) + .verifyIssues(); + } + + @Test + void checkCompliantTests() { + /* CheckVerifier.newVerifier() + .onFile("src/test/files/GoodWayConcatenateStringsLoop.java") + .withCheck(new AvoidConcatenateStringsInLoop()) + .verifyNoIssues(); + + */ + } + +} From 0bdc73db80910cdc1016119cc60a385573ffe75c Mon Sep 17 00:00:00 2001 From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com> Date: Wed, 5 Apr 2023 13:39:03 +0200 Subject: [PATCH 04/31] update CRJVM250.html --- .../fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a90529a49..9ab41bc22 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,4 +1,4 @@ -

Consider forcing the use of LAZY's FetchType on collections in JPA type entities. To reduce RAM load.

+

Consider using LAZY mode on your FetchType for collections in JPA type entities. This will reduce the amount of data loaded into memory.

Non-compliant Code Example


From 2ed35144d46508bf7918e2ba39d51057312315de Mon Sep 17 00:00:00 2001
From: WalidKrifi 
Date: Wed, 5 Apr 2023 13:51:33 +0200
Subject: [PATCH 05/31] WKR-Add access to annotation

---
 .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java     | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
index 9304e09f0..b839132ea 100644
--- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
+++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
@@ -37,10 +37,10 @@ public void visitNode(Tree tree) {
 
         if (tree.is(Kind.VARIABLE)) {
             VariableTree variableTree = (VariableTree) tree;
-            List listOfAnnotations = variableTree.type().annotations();
-            for(AnnotationTree annotationTree : listOfAnnotations){
+            List annotations = variableTree.modifiers().annotations();
 
-                System.out.print(annotationTree.toString());
+            for(AnnotationTree annotationTree : annotations){
+                System.out.print(annotationTree.annotationType());
             }
             reportIssue(tree, MESSAGERULE);
         }

From 1d68d3f8b89e219b222a8dd5177d591f66749069 Mon Sep 17 00:00:00 2001
From: WalidKrifi 
Date: Wed, 5 Apr 2023 14:07:40 +0200
Subject: [PATCH 06/31] WKR-Add trying access to arguments

---
 .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java     | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
index b839132ea..6bb48a649 100644
--- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
+++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
@@ -41,6 +41,12 @@ public void visitNode(Tree tree) {
 
             for(AnnotationTree annotationTree : annotations){
                 System.out.print(annotationTree.annotationType());
+
+                if("OneToMany".equals(annotationTree.annotationType())||"ManyToOne".equals(annotationTree.annotationType())){
+
+                    Arguments arguments = annotationTree.arguments();
+
+                }
             }
             reportIssue(tree, MESSAGERULE);
         }

From 7409bc6fdeb19b0c7871f1d50af9d842b3bf25ff Mon Sep 17 00:00:00 2001
From: WalidKrifi 
Date: Wed, 5 Apr 2023 13:27:17 +0200
Subject: [PATCH 07/31] WKR-Add Rule structure dev

---
 .../ForceUsingLazyFetchTypeInJPAEntity.java   | 50 +++++++++++++++++++
 .../src/test/files/ForceLazyFetchTypeUse.java | 27 +++++-----
 ...orceUsingLazyFetchTypeInJPAEntityTest.java | 26 ++++++++++
 3 files changed, 89 insertions(+), 14 deletions(-)
 create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
 create mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java

diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
new file mode 100644
index 000000000..9304e09f0
--- /dev/null
+++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
@@ -0,0 +1,50 @@
+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.semantic.MethodMatchers;
+import org.sonar.plugins.java.api.tree.*;
+import org.sonar.plugins.java.api.tree.Tree.Kind;
+
+import java.sql.PreparedStatement;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Stream;
+
+import static fr.greencodeinitiative.java.checks.ConstOrLiteralDeclare.isLiteral;
+import static java.util.Arrays.asList;
+import static org.sonar.plugins.java.api.semantic.Type.Primitives.INT;
+import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT;
+import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION;
+
+@Rule(key = "CRJVM205", name = "Developpement",
+        description = ForceUsingLazyFetchTypeInJPAEntity.MESSAGERULE,
+        priority = Priority.MINOR,
+        tags = {"bug"})
+
+public class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisitor {
+
+    protected static final String MESSAGERULE = "Force the use of LAZY FetchType";
+
+    @Override
+    public List nodesToVisit() {
+        return Arrays.asList(Kind.VARIABLE);
+    }
+
+    @Override
+    public void visitNode(Tree tree) {
+
+        if (tree.is(Kind.VARIABLE)) {
+            VariableTree variableTree = (VariableTree) tree;
+            List listOfAnnotations = variableTree.type().annotations();
+            for(AnnotationTree annotationTree : listOfAnnotations){
+
+                System.out.print(annotationTree.toString());
+            }
+            reportIssue(tree, MESSAGERULE);
+        }
+    }
+
+
+}
diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java b/java-plugin/src/test/files/ForceLazyFetchTypeUse.java
index 3feface0d..786845e2c 100644
--- a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java
+++ b/java-plugin/src/test/files/ForceLazyFetchTypeUse.java
@@ -1,21 +1,20 @@
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.Transient;
+
 @Entity
 @Table(name = "purchaseOrder")
-public class Order implements Serializable {
+public class ForceLazyFetchTypeUse implements Serializable {
    
   @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}}
   private Set items = new HashSet();
-   
-  ...
-   
-}
 
-@Entity
-@Table(name = "purchaseOrder")
-public class Order implements Serializable {
-   
-  @OneToMany(mappedBy = "order", fetch = FetchType.LAZY) // Ignored => compliant
-  private Set items = new HashSet();
-   
-  ...
-   
 }
diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
new file mode 100644
index 000000000..35f471d55
--- /dev/null
+++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
@@ -0,0 +1,26 @@
+package fr.greencodeinitiative.java.checks;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.java.checks.verifier.CheckVerifier;
+
+public class ForceUsingLazyFetchTypeInJPAEntityTest {
+
+    @Test
+    void checkNonCompliantTests() {
+        CheckVerifier.newVerifier()
+                .onFile("src/test/files/ForceLazyFetchTypeUse.java")
+                .withCheck(new ForceUsingLazyFetchTypeInJPAEntity())
+                .verifyIssues();
+    }
+
+    @Test
+    void checkCompliantTests() {
+       /* CheckVerifier.newVerifier()
+                .onFile("src/test/files/GoodWayConcatenateStringsLoop.java")
+                .withCheck(new AvoidConcatenateStringsInLoop())
+                .verifyNoIssues();
+
+        */
+    }
+
+}

From 2341685b6907b0268b7f916dc4e3ac8473116123 Mon Sep 17 00:00:00 2001
From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com>
Date: Wed, 5 Apr 2023 14:31:57 +0200
Subject: [PATCH 08/31] Update tests classes

---
 .../files/ForceLazyFetchTypeUseAllInOne.java  | 26 +++++++++++++++++++
 .../files/ForceLazyFetchTypeUseCompliant.java | 20 ++++++++++++++
 .../files/ForceLazyFetchTypeUseFalseTest.java | 20 ++++++++++++++
 ...=> ForceLazyFetchTypeUseNonCompliant.java} |  2 +-
 ...orceUsingLazyFetchTypeInJPAEntityTest.java | 24 +++++++++++++----
 5 files changed, 86 insertions(+), 6 deletions(-)
 create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java
 create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java
 create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java
 rename java-plugin/src/test/files/{ForceLazyFetchTypeUse.java => ForceLazyFetchTypeUseNonCompliant.java} (89%)

diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java
new file mode 100644
index 000000000..70f138071
--- /dev/null
+++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java
@@ -0,0 +1,26 @@
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.Transient;
+
+@Entity
+@Table(name = "purchaseOrder")
+public class ForceLazyFetchTypeUseAllInOne implements Serializable {
+   
+  @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}}
+  private Set items = new HashSet();
+
+  @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
+  private Set items = new HashSet();
+
+  @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false)
+  private Set items = new HashSet();
+
+}
diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java
new file mode 100644
index 000000000..5a0e78f9d
--- /dev/null
+++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java
@@ -0,0 +1,20 @@
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.Transient;
+
+@Entity
+@Table(name = "purchaseOrder")
+public class ForceLazyFetchTypeUseCompliant implements Serializable {
+   
+  @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
+  private Set items = new HashSet();
+
+}
diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java
new file mode 100644
index 000000000..6d3761af7
--- /dev/null
+++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java
@@ -0,0 +1,20 @@
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.Transient;
+
+@Entity
+@Table(name = "purchaseOrder")
+public class ForceLazyFetchTypeUseFalseTest implements Serializable {
+  
+  @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false)
+  private Set items = new HashSet();
+
+}
diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseNonCompliant.java
similarity index 89%
rename from java-plugin/src/test/files/ForceLazyFetchTypeUse.java
rename to java-plugin/src/test/files/ForceLazyFetchTypeUseNonCompliant.java
index 786845e2c..acf11d368 100644
--- a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java
+++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseNonCompliant.java
@@ -12,7 +12,7 @@
 
 @Entity
 @Table(name = "purchaseOrder")
-public class ForceLazyFetchTypeUse implements Serializable {
+public class ForceLazyFetchTypeUseNonCompliant implements Serializable {
    
   @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}}
   private Set items = new HashSet();
diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
index 35f471d55..c6a5ccbfc 100644
--- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
+++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
@@ -8,19 +8,33 @@ public class ForceUsingLazyFetchTypeInJPAEntityTest {
     @Test
     void checkNonCompliantTests() {
         CheckVerifier.newVerifier()
-                .onFile("src/test/files/ForceLazyFetchTypeUse.java")
+                .onFile("src/test/files/ForceLazyFetchTypeUseNonCompliant.java")
                 .withCheck(new ForceUsingLazyFetchTypeInJPAEntity())
                 .verifyIssues();
     }
 
     @Test
     void checkCompliantTests() {
-       /* CheckVerifier.newVerifier()
-                .onFile("src/test/files/GoodWayConcatenateStringsLoop.java")
-                .withCheck(new AvoidConcatenateStringsInLoop())
+        CheckVerifier.newVerifier()
+                .onFile("src/test/files/ForceLazyFetchTypeUseCompliant.java")
+                .withCheck(new ForceUsingLazyFetchTypeInJPAEntity())
                 .verifyNoIssues();
 
-        */
     }
 
+    @Test
+    void checkPositiveFalseTests() {
+        CheckVerifier.newVerifier()
+                .onFile("src/test/files/ForceLazyFetchTypeUseFalseTest.java")
+                .withCheck(new ForceUsingLazyFetchTypeInJPAEntity())
+                .verifyNoIssues();
+    }
+
+    @Test
+    void checkPositiveFalseTests() {
+        CheckVerifier.newVerifier()
+                .onFile("src/test/files/ForceLazyFetchTypeUseAllInOne.java")
+                .withCheck(new ForceUsingLazyFetchTypeInJPAEntity())
+                .verifyNoIssues();
+    }
 }

From 580dc34ca922b197bd9d073d510acc3e11dc8ded Mon Sep 17 00:00:00 2001
From: WalidKrifi 
Date: Wed, 5 Apr 2023 13:51:33 +0200
Subject: [PATCH 09/31] WKR-Add access to annotation

---
 .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java     | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
index 9304e09f0..b839132ea 100644
--- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
+++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
@@ -37,10 +37,10 @@ public void visitNode(Tree tree) {
 
         if (tree.is(Kind.VARIABLE)) {
             VariableTree variableTree = (VariableTree) tree;
-            List listOfAnnotations = variableTree.type().annotations();
-            for(AnnotationTree annotationTree : listOfAnnotations){
+            List annotations = variableTree.modifiers().annotations();
 
-                System.out.print(annotationTree.toString());
+            for(AnnotationTree annotationTree : annotations){
+                System.out.print(annotationTree.annotationType());
             }
             reportIssue(tree, MESSAGERULE);
         }

From d64c35bc783b2efcbbe7922aad59da70cd0036e5 Mon Sep 17 00:00:00 2001
From: WalidKrifi 
Date: Wed, 5 Apr 2023 14:42:03 +0200
Subject: [PATCH 10/31] WKR-Add trying access to fetch

---
 .../ForceUsingLazyFetchTypeInJPAEntity.java       | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
index 6bb48a649..7137635e4 100644
--- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
+++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
@@ -10,6 +10,7 @@
 import java.sql.PreparedStatement;
 import java.util.Arrays;
 import java.util.List;
+import java.util.ListIterator;
 import java.util.stream.Stream;
 
 import static fr.greencodeinitiative.java.checks.ConstOrLiteralDeclare.isLiteral;
@@ -42,9 +43,21 @@ public void visitNode(Tree tree) {
             for(AnnotationTree annotationTree : annotations){
                 System.out.print(annotationTree.annotationType());
 
-                if("OneToMany".equals(annotationTree.annotationType())||"ManyToOne".equals(annotationTree.annotationType())){
+                if("OneToMany".equals(annotationTree.annotationType().toString())||"ManyToOne".equals(annotationTree.annotationType())){
 
                     Arguments arguments = annotationTree.arguments();
+                    for (ListIterator it = arguments.listIterator(); it.hasNext(); ) {
+                        ExpressionTree argument = it.next();
+                        AssignmentExpressionTree assignementExpression = (AssignmentExpressionTree)argument;
+                        String attribute = assignementExpression.variable().toString();
+                        if("fetch".equals(attribute)){
+                            //--attribut
+                            assignementExpression.variable();
+                            //--value
+                            assignementExpression.expression();
+                        }
+                        System.out.print(attribute);
+                    }
 
                 }
             }

From 50fe8049caa9dd31b28a98cc4730642ffc063402 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20LE=20SAUSSE?=
 
Date: Wed, 5 Apr 2023 14:44:34 +0200
Subject: [PATCH 11/31] Modify method name avoid compilation error

---
 .../java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java     | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
index c6a5ccbfc..4d81d3a90 100644
--- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
+++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java
@@ -31,7 +31,7 @@ void checkPositiveFalseTests() {
     }
 
     @Test
-    void checkPositiveFalseTests() {
+    void checkAllTests() {
         CheckVerifier.newVerifier()
                 .onFile("src/test/files/ForceLazyFetchTypeUseAllInOne.java")
                 .withCheck(new ForceUsingLazyFetchTypeInJPAEntity())

From 6619a8fbc38a01d824c20f659e33d907f7679734 Mon Sep 17 00:00:00 2001
From: WalidKrifi 
Date: Wed, 5 Apr 2023 14:55:53 +0200
Subject: [PATCH 12/31] WKR- first version

---
 .../ForceUsingLazyFetchTypeInJPAEntity.java     | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
index 7137635e4..5870151f0 100644
--- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
+++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java
@@ -49,19 +49,22 @@ public void visitNode(Tree tree) {
                     for (ListIterator it = arguments.listIterator(); it.hasNext(); ) {
                         ExpressionTree argument = it.next();
                         AssignmentExpressionTree assignementExpression = (AssignmentExpressionTree)argument;
-                        String attribute = assignementExpression.variable().toString();
-                        if("fetch".equals(attribute)){
+
+                        IdentifierTree variable = (IdentifierTree) assignementExpression.variable();
+                        if("fetch".equals(variable.name())){
                             //--attribut
-                            assignementExpression.variable();
-                            //--value
-                            assignementExpression.expression();
+                            String fetchValue = ((MemberSelectExpressionTree)assignementExpression.expression()).identifier().name();
+                            System.out.print(fetchValue);
+                            if("EAGER".equals(fetchValue)){
+                                reportIssue(tree, MESSAGERULE);
+                            }
                         }
-                        System.out.print(attribute);
+
                     }
 
                 }
             }
-            reportIssue(tree, MESSAGERULE);
+
         }
     }
 

From ef11e3350dac7f8da332133a20474c2665acdaef Mon Sep 17 00:00:00 2001
From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com>
Date: Wed, 5 Apr 2023 15:05:17 +0200
Subject: [PATCH 13/31] Merge team build

---
 .../l10n/java/rules/java/CRJVM205.html        | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html

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..9ab41bc22
--- /dev/null
+++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html
@@ -0,0 +1,30 @@
+

Consider using LAZY mode on your FetchType for collections in JPA type entities. This will reduce the amount of data loaded into memory.

+ +

Non-compliant Code Example

+
+    @Entity
+    @Table(name = "purchaseOrder")
+    public class Order implements Serializable {
+       
+      @OneToMany(mappedBy = "order", fetch = FetchType.EAGER)
+      private Set items = new HashSet();
+       
+      ...
+       
+    }
+
+
+

Compliant Code Example

+
+    @Entity
+    @Table(name = "purchaseOrder")
+    public class Order implements Serializable {
+       
+      @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
+      private Set items = new HashSet();
+       
+      ...
+       
+    }
+
+
\ No newline at end of file From 6e9fee776291664528d9cfb0aeec493963854ed4 Mon Sep 17 00:00:00 2001 From: WalidKrifi Date: Wed, 5 Apr 2023 15:13:02 +0200 Subject: [PATCH 14/31] WKR- second version --- .../ForceUsingLazyFetchTypeInJPAEntity.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 5870151f0..59cd95066 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -3,21 +3,12 @@ import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; -import org.sonar.plugins.java.api.semantic.MethodMatchers; import org.sonar.plugins.java.api.tree.*; import org.sonar.plugins.java.api.tree.Tree.Kind; -import java.sql.PreparedStatement; import java.util.Arrays; import java.util.List; import java.util.ListIterator; -import java.util.stream.Stream; - -import static fr.greencodeinitiative.java.checks.ConstOrLiteralDeclare.isLiteral; -import static java.util.Arrays.asList; -import static org.sonar.plugins.java.api.semantic.Type.Primitives.INT; -import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT; -import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION; @Rule(key = "CRJVM205", name = "Developpement", description = ForceUsingLazyFetchTypeInJPAEntity.MESSAGERULE, @@ -39,20 +30,22 @@ public void visitNode(Tree tree) { if (tree.is(Kind.VARIABLE)) { VariableTree variableTree = (VariableTree) tree; List annotations = variableTree.modifiers().annotations(); - + //-- get all annotations on the attribut for(AnnotationTree annotationTree : annotations){ - System.out.print(annotationTree.annotationType()); - - if("OneToMany".equals(annotationTree.annotationType().toString())||"ManyToOne".equals(annotationTree.annotationType())){ + //--access only to One + ; + if("OneToMany".equals(annotationTree.annotationType().symbolType().name())||"ManyToOne".equals(annotationTree.annotationType().symbolType().name())){ Arguments arguments = annotationTree.arguments(); for (ListIterator it = arguments.listIterator(); it.hasNext(); ) { + ExpressionTree argument = it.next(); + AssignmentExpressionTree assignementExpression = (AssignmentExpressionTree)argument; IdentifierTree variable = (IdentifierTree) assignementExpression.variable(); + if("fetch".equals(variable.name())){ - //--attribut String fetchValue = ((MemberSelectExpressionTree)assignementExpression.expression()).identifier().name(); System.out.print(fetchValue); if("EAGER".equals(fetchValue)){ From 41882779ca07937aa81ecdca39e68cfb91702854 Mon Sep 17 00:00:00 2001 From: WalidKrifi Date: Wed, 5 Apr 2023 15:15:08 +0200 Subject: [PATCH 15/31] WKR- second version --- .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 59cd95066..e94de1ff9 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -37,6 +37,7 @@ public void visitNode(Tree tree) { if("OneToMany".equals(annotationTree.annotationType().symbolType().name())||"ManyToOne".equals(annotationTree.annotationType().symbolType().name())){ Arguments arguments = annotationTree.arguments(); + for (ListIterator it = arguments.listIterator(); it.hasNext(); ) { ExpressionTree argument = it.next(); @@ -47,7 +48,6 @@ public void visitNode(Tree tree) { if("fetch".equals(variable.name())){ String fetchValue = ((MemberSelectExpressionTree)assignementExpression.expression()).identifier().name(); - System.out.print(fetchValue); if("EAGER".equals(fetchValue)){ reportIssue(tree, MESSAGERULE); } From c2983a4cedfe8426cfcc3196b53fc2c66a81f6e6 Mon Sep 17 00:00:00 2001 From: WalidKrifi Date: Wed, 5 Apr 2023 15:23:35 +0200 Subject: [PATCH 16/31] WKR- second version --- .../checks/ForceUsingLazyFetchTypeInJPAEntity.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index e94de1ff9..5fdbc59c3 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -18,7 +18,10 @@ public class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Force the use of LAZY FetchType"; - + private static final String EAGER_KEYWORD = "EAGER"; + private static final String FETCH_KEYWORD = "fetch"; + private static final String ONE_TO_MANY = "OneToMany"; + private static final String MANY_TO_ONE = "ManyToOne"; @Override public List nodesToVisit() { return Arrays.asList(Kind.VARIABLE); @@ -33,8 +36,8 @@ public void visitNode(Tree tree) { //-- get all annotations on the attribut for(AnnotationTree annotationTree : annotations){ //--access only to One - ; - if("OneToMany".equals(annotationTree.annotationType().symbolType().name())||"ManyToOne".equals(annotationTree.annotationType().symbolType().name())){ + + if(ONE_TO_MANY.equals(annotationTree.annotationType().symbolType().name())||MANY_TO_ONE.equals(annotationTree.annotationType().symbolType().name())){ Arguments arguments = annotationTree.arguments(); @@ -46,9 +49,9 @@ public void visitNode(Tree tree) { IdentifierTree variable = (IdentifierTree) assignementExpression.variable(); - if("fetch".equals(variable.name())){ + if(FETCH_KEYWORD.equals(variable.name())){ String fetchValue = ((MemberSelectExpressionTree)assignementExpression.expression()).identifier().name(); - if("EAGER".equals(fetchValue)){ + if(EAGER_KEYWORD.equals(fetchValue)){ reportIssue(tree, MESSAGERULE); } } From 58055ab889ad916f71e361804bc8d150a440eca5 Mon Sep 17 00:00:00 2001 From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com> Date: Wed, 5 Apr 2023 15:41:48 +0200 Subject: [PATCH 17/31] Correct merge --- .../src/test/files/ForceLazyFetchTypeUse.java | 20 ------------------- ...orceUsingLazyFetchTypeInJPAEntityTest.java | 2 +- 2 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUse.java diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java b/java-plugin/src/test/files/ForceLazyFetchTypeUse.java deleted file mode 100644 index 786845e2c..000000000 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUse.java +++ /dev/null @@ -1,20 +0,0 @@ -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; -import javax.persistence.Transient; - -@Entity -@Table(name = "purchaseOrder") -public class ForceLazyFetchTypeUse implements Serializable { - - @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} - private Set items = new HashSet(); - -} diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java index 4d81d3a90..d46413a99 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java @@ -35,6 +35,6 @@ void checkAllTests() { CheckVerifier.newVerifier() .onFile("src/test/files/ForceLazyFetchTypeUseAllInOne.java") .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) - .verifyNoIssues(); + .verifyIssues(); } } From 7fa808cd423258b344ec0661c2cac954d32cbcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20LE=20SAUSSE?= Date: Wed, 5 Apr 2023 15:50:49 +0200 Subject: [PATCH 18/31] Modify table names and column name --- .../src/test/files/ForceLazyFetchTypeUseAllInOne.java | 8 ++++---- .../src/test/files/ForceLazyFetchTypeUseCompliant.java | 4 ++-- .../src/test/files/ForceLazyFetchTypeUseFalseTest.java | 4 ++-- .../src/test/files/ForceLazyFetchTypeUseNonCompliant.java | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java index 70f138071..bacb397ec 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseAllInOne.java @@ -11,16 +11,16 @@ import javax.persistence.Transient; @Entity -@Table(name = "purchaseOrder") +@Table(name = "myTable") public class ForceLazyFetchTypeUseAllInOne implements Serializable { - @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} + @OneToMany(mappedBy = "myOrders", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} private Set items = new HashSet(); - @OneToMany(mappedBy = "order", fetch = FetchType.LAZY) + @OneToMany(mappedBy = "myOrders", fetch = FetchType.LAZY) private Set items = new HashSet(); - @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false) + @Column(name = "ORDER", length = 50, nullable = false, unique = false) private Set items = new HashSet(); } diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java index 5a0e78f9d..eace2fa77 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseCompliant.java @@ -11,10 +11,10 @@ import javax.persistence.Transient; @Entity -@Table(name = "purchaseOrder") +@Table(name = "myTable") public class ForceLazyFetchTypeUseCompliant implements Serializable { - @OneToMany(mappedBy = "order", fetch = FetchType.LAZY) + @OneToMany(mappedBy = "myOrders", fetch = FetchType.LAZY) private Set items = new HashSet(); } diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java index 6d3761af7..855fa5b67 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java @@ -11,10 +11,10 @@ import javax.persistence.Transient; @Entity -@Table(name = "purchaseOrder") +@Table(name = "myTable") public class ForceLazyFetchTypeUseFalseTest implements Serializable { - @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false) + @Column(name = "ORDER", length = 50, nullable = false, unique = false) private Set items = new HashSet(); } diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseNonCompliant.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseNonCompliant.java index acf11d368..6cb7aa618 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseNonCompliant.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseNonCompliant.java @@ -11,10 +11,10 @@ import javax.persistence.Transient; @Entity -@Table(name = "purchaseOrder") +@Table(name = "myTable") public class ForceLazyFetchTypeUseNonCompliant implements Serializable { - @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} + @OneToMany(mappedBy = "myOrders", fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} private Set items = new HashSet(); } From eeaaadc0a1a750104eb8b0974e48ecc909fc74a3 Mon Sep 17 00:00:00 2001 From: WalidKrifi Date: Wed, 5 Apr 2023 16:55:45 +0200 Subject: [PATCH 19/31] WKR- second version --- .../greencodeinitiative/java/RulesList.java | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java index d6a5cdbf4..c48923e49 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java @@ -24,25 +24,7 @@ import java.util.Collections; import java.util.List; -import fr.greencodeinitiative.java.checks.ArrayCopyCheck; -import fr.greencodeinitiative.java.checks.AvoidConcatenateStringsInLoop; -import fr.greencodeinitiative.java.checks.AvoidFullSQLRequest; -import fr.greencodeinitiative.java.checks.AvoidGettingSizeCollectionInLoop; -import fr.greencodeinitiative.java.checks.AvoidMultipleIfElseStatement; -import fr.greencodeinitiative.java.checks.AvoidRegexPatternNotStatic; -import fr.greencodeinitiative.java.checks.AvoidSQLRequestInLoop; -import fr.greencodeinitiative.java.checks.AvoidSetConstantInBatchUpdate; -import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopCheck; -import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries; -import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections; -import fr.greencodeinitiative.java.checks.AvoidUsingGlobalVariablesCheck; -import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface; -import fr.greencodeinitiative.java.checks.IncrementCheck; -import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize; -import fr.greencodeinitiative.java.checks.NoFunctionCallWhenDeclaringForLoop; -import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions; -import fr.greencodeinitiative.java.checks.UnnecessarilyAssignValuesToVariables; -import fr.greencodeinitiative.java.checks.UseCorrectForLoop; +import fr.greencodeinitiative.java.checks.*; import org.sonar.plugins.java.api.JavaCheck; public final class RulesList { @@ -77,7 +59,8 @@ public static List> getJavaChecks() { AvoidUsingGlobalVariablesCheck.class, AvoidSetConstantInBatchUpdate.class, FreeResourcesOfAutoCloseableInterface.class, - AvoidMultipleIfElseStatement.class + AvoidMultipleIfElseStatement.class, + ForceUsingLazyFetchTypeInJPAEntity.class )); } From bb43a164fe8b17d3d12baa5d77d5d4df9597cf4e Mon Sep 17 00:00:00 2001 From: adrienpelfresne Date: Wed, 5 Apr 2023 17:09:15 +0200 Subject: [PATCH 20/31] for testing --- .../test/ForceLazyFetchTypeUseAllInOne.java | 18 ++++++++++++++++++ .../test/ForceLazyFetchTypeUseCompliant.java | 16 ++++++++++++++++ .../test/ForceLazyFetchTypeUseFalseTest.java | 13 +++++++++++++ .../ForceLazyFetchTypeUseNonCompliant.java | 15 +++++++++++++++ java-plugin/src/test/files/test/LazyItem.java | 13 +++++++++++++ java-plugin/src/test/files/test/Order.java | 17 +++++++++++++++++ 6 files changed, 92 insertions(+) create mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java create mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java create mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java create mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java create mode 100644 java-plugin/src/test/files/test/LazyItem.java create mode 100644 java-plugin/src/test/files/test/Order.java diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java new file mode 100644 index 000000000..263194984 --- /dev/null +++ b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java @@ -0,0 +1,18 @@ +package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; + +import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "ALL_IN_ONE") +public class ForceLazyFetchTypeUseAllInOne extends LazyItem { + @OneToMany(mappedBy = "item", fetch = FetchType.EAGER) + private Set ordersEager = new HashSet<>(); + + @OneToMany(mappedBy = "item", fetch = FetchType.LAZY) + private Set ordersLazy = new HashSet<>(); + + @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false) + private String studentNames; +} diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java new file mode 100644 index 000000000..0b8b670b0 --- /dev/null +++ b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java @@ -0,0 +1,16 @@ +package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; + + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "COMPLIANT") +public class ForceLazyFetchTypeUseCompliant extends LazyItem { + @OneToMany(mappedBy = "item", fetch = FetchType.LAZY) + private Set orders = new HashSet<>(); +} diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java new file mode 100644 index 000000000..d77534c0d --- /dev/null +++ b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java @@ -0,0 +1,13 @@ +package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +@Entity +@Table(name = "FALSE_TEST") +public class ForceLazyFetchTypeUseFalseTest extends LazyItem { + @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false) + private String studentNames; +} + diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java new file mode 100644 index 000000000..2e5ca6f13 --- /dev/null +++ b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java @@ -0,0 +1,15 @@ +package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "NON_COMPLIANT") +public class ForceLazyFetchTypeUseNonCompliant extends LazyItem { + @OneToMany(mappedBy = "item", fetch = FetchType.EAGER) + private Set orders = new HashSet<>(); +} diff --git a/java-plugin/src/test/files/test/LazyItem.java b/java-plugin/src/test/files/test/LazyItem.java new file mode 100644 index 000000000..81df42c9a --- /dev/null +++ b/java-plugin/src/test/files/test/LazyItem.java @@ -0,0 +1,13 @@ +package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@Table(name = "LazyItem") +public class LazyItem implements Serializable { + @Id + @GeneratedValue + @Column(name = "id") + private Long id; +} diff --git a/java-plugin/src/test/files/test/Order.java b/java-plugin/src/test/files/test/Order.java new file mode 100644 index 000000000..430ca00b4 --- /dev/null +++ b/java-plugin/src/test/files/test/Order.java @@ -0,0 +1,17 @@ +package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@Table(name = "ORDER") +public class Order implements Serializable { + @Id + @GeneratedValue + @Column(name = "ORDER_ID") + private Long orderId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "id") + private LazyItem item; +} From 071d082b071e50c2f88c234dd59c879ac9d69bc1 Mon Sep 17 00:00:00 2001 From: adrienpelfresne Date: Wed, 5 Apr 2023 17:22:58 +0200 Subject: [PATCH 21/31] delete files --- .../test/ForceLazyFetchTypeUseAllInOne.java | 18 ------------------ .../test/ForceLazyFetchTypeUseCompliant.java | 16 ---------------- .../test/ForceLazyFetchTypeUseFalseTest.java | 13 ------------- .../ForceLazyFetchTypeUseNonCompliant.java | 15 --------------- java-plugin/src/test/files/test/LazyItem.java | 13 ------------- java-plugin/src/test/files/test/Order.java | 17 ----------------- 6 files changed, 92 deletions(-) delete mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java delete mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java delete mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java delete mode 100644 java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java delete mode 100644 java-plugin/src/test/files/test/LazyItem.java delete mode 100644 java-plugin/src/test/files/test/Order.java diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java deleted file mode 100644 index 263194984..000000000 --- a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseAllInOne.java +++ /dev/null @@ -1,18 +0,0 @@ -package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; - -import javax.persistence.*; -import java.util.HashSet; -import java.util.Set; - -@Entity -@Table(name = "ALL_IN_ONE") -public class ForceLazyFetchTypeUseAllInOne extends LazyItem { - @OneToMany(mappedBy = "item", fetch = FetchType.EAGER) - private Set ordersEager = new HashSet<>(); - - @OneToMany(mappedBy = "item", fetch = FetchType.LAZY) - private Set ordersLazy = new HashSet<>(); - - @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false) - private String studentNames; -} diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java deleted file mode 100644 index 0b8b670b0..000000000 --- a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseCompliant.java +++ /dev/null @@ -1,16 +0,0 @@ -package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; - - -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.OneToMany; -import javax.persistence.Table; -import java.util.HashSet; -import java.util.Set; - -@Entity -@Table(name = "COMPLIANT") -public class ForceLazyFetchTypeUseCompliant extends LazyItem { - @OneToMany(mappedBy = "item", fetch = FetchType.LAZY) - private Set orders = new HashSet<>(); -} diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java deleted file mode 100644 index d77534c0d..000000000 --- a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseFalseTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Table; - -@Entity -@Table(name = "FALSE_TEST") -public class ForceLazyFetchTypeUseFalseTest extends LazyItem { - @Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false) - private String studentNames; -} - diff --git a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java b/java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java deleted file mode 100644 index 2e5ca6f13..000000000 --- a/java-plugin/src/test/files/test/ForceLazyFetchTypeUseNonCompliant.java +++ /dev/null @@ -1,15 +0,0 @@ -package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; - -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.OneToMany; -import javax.persistence.Table; -import java.util.HashSet; -import java.util.Set; - -@Entity -@Table(name = "NON_COMPLIANT") -public class ForceLazyFetchTypeUseNonCompliant extends LazyItem { - @OneToMany(mappedBy = "item", fetch = FetchType.EAGER) - private Set orders = new HashSet<>(); -} diff --git a/java-plugin/src/test/files/test/LazyItem.java b/java-plugin/src/test/files/test/LazyItem.java deleted file mode 100644 index 81df42c9a..000000000 --- a/java-plugin/src/test/files/test/LazyItem.java +++ /dev/null @@ -1,13 +0,0 @@ -package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; - -import javax.persistence.*; -import java.io.Serializable; - -@Entity -@Table(name = "LazyItem") -public class LazyItem implements Serializable { - @Id - @GeneratedValue - @Column(name = "id") - private Long id; -} diff --git a/java-plugin/src/test/files/test/Order.java b/java-plugin/src/test/files/test/Order.java deleted file mode 100644 index 430ca00b4..000000000 --- a/java-plugin/src/test/files/test/Order.java +++ /dev/null @@ -1,17 +0,0 @@ -package fr.greencodeinitiative.java.checks.forcelazyfetchtypeuse; - -import javax.persistence.*; -import java.io.Serializable; - -@Entity -@Table(name = "ORDER") -public class Order implements Serializable { - @Id - @GeneratedValue - @Column(name = "ORDER_ID") - private Long orderId; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "id") - private LazyItem item; -} From 34be768a84fd39d866fd2aed352c296d9f0d48ab Mon Sep 17 00:00:00 2001 From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com> Date: Wed, 5 Apr 2023 17:34:26 +0200 Subject: [PATCH 22/31] Change rule number to EC80 --- .../l10n/java/rules/java/{CRJVM205.html => EC80.html} | 0 .../l10n/java/rules/java/{CRJVM205.json => EC80.json} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/{CRJVM205.html => EC80.html} (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/{CRJVM205.json => EC80.json} (100%) 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/EC80.html similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC80.html 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/EC80.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC80.json From 2bf892118cb214aa8eec75667fa42dda84df6ca4 Mon Sep 17 00:00:00 2001 From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com> Date: Wed, 5 Apr 2023 17:36:04 +0200 Subject: [PATCH 23/31] Change rule number for EC80 --- .../l10n/java/rules/java/{CRJVM205.html => EC80.html} | 0 .../l10n/java/rules/java/{CRJVM205.json => EC80.json} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/{CRJVM205.html => EC80.html} (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/{CRJVM205.json => EC80.json} (100%) 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/EC80.html similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC80.html 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/EC80.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM205.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC80.json From 2c8e50dd9fbd18eb4ac13a9a8408345b53b9e864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20LE=20SAUSSE?= Date: Wed, 5 Apr 2023 17:50:35 +0200 Subject: [PATCH 24/31] Add new test file and test --- ...ForceLazyFetchTypeUseDefaultManyToOne.java | 20 ++++++++++++ .../ForceLazyFetchTypeUseDefaultOneToOne.java | 20 ++++++++++++ .../files/ForceLazyFetchTypeUseManyToOne.java | 20 ++++++++++++ .../files/ForceLazyFetchTypeUseOneToOne.java | 20 ++++++++++++ ...orceUsingLazyFetchTypeInJPAEntityTest.java | 32 +++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java create mode 100644 java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java new file mode 100644 index 000000000..40280e997 --- /dev/null +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java @@ -0,0 +1,20 @@ +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + +@Entity +@Table(name = "myTable") +public class ForceLazyFetchTypeUseFalseTest implements Serializable { + + @ManyToOne // Noncompliant {{Force the use of LAZY FetchType}} + private Order order; + +} diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java new file mode 100644 index 000000000..176830e18 --- /dev/null +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java @@ -0,0 +1,20 @@ +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + +@Entity +@Table(name = "myTable") +public class ForceLazyFetchTypeUseFalseTest implements Serializable { + + @OneToOne // Noncompliant {{Force the use of LAZY FetchType}} + private OrderInformation orderInformations; + +} diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java new file mode 100644 index 000000000..9ec018e05 --- /dev/null +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java @@ -0,0 +1,20 @@ +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + +@Entity +@Table(name = "myTable") +public class ForceLazyFetchTypeUseFalseTest implements Serializable { + + @ManyToOne(fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} + private Order order; + +} diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java new file mode 100644 index 000000000..c3957a8bf --- /dev/null +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java @@ -0,0 +1,20 @@ +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; + +@Entity +@Table(name = "myTable") +public class ForceLazyFetchTypeUseFalseTest implements Serializable { + + @OneToOne(fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} + private OrderInformation orderInformations; + +} diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java index d46413a99..6447e5b4c 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java @@ -37,4 +37,36 @@ void checkAllTests() { .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) .verifyIssues(); } + @Test + void checkDefaultOneToOneTests() { + CheckVerifier.newVerifier() + .onFile("src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java") + .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) + .verifyIssues(); + } + @Test + void checkDefaultManyToOneTests() { + CheckVerifier.newVerifier() + .onFile("src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java") + .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) + .verifyIssues(); + } + + @Test + void checkManyToOneTests() { + CheckVerifier.newVerifier() + .onFile("src/test/files/ForceLazyFetchTypeUseManyToOne.java") + .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) + .verifyIssues(); + } + + @Test + void checkOneToOneTests() { + CheckVerifier.newVerifier() + .onFile("src/test/files/ForceLazyFetchTypeUseOneToOne.java") + .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) + .verifyIssues(); + + } + } From 8f978ea63d4a7d27236046c252ac1558ce15a37b Mon Sep 17 00:00:00 2001 From: WalidKrifi Date: Wed, 5 Apr 2023 17:52:32 +0200 Subject: [PATCH 25/31] WKR- Add check of default values --- .../ForceUsingLazyFetchTypeInJPAEntity.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 5fdbc59c3..075260f61 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -10,7 +10,7 @@ import java.util.List; import java.util.ListIterator; -@Rule(key = "CRJVM205", name = "Developpement", +@Rule(key = "EC80", name = "Developpement", description = ForceUsingLazyFetchTypeInJPAEntity.MESSAGERULE, priority = Priority.MINOR, tags = {"bug"}) @@ -22,6 +22,10 @@ public class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisi private static final String FETCH_KEYWORD = "fetch"; private static final String ONE_TO_MANY = "OneToMany"; private static final String MANY_TO_ONE = "ManyToOne"; + + private static final String ONE_TO_ONE = "OneToOne"; + + private static final String MANY_TO_MANY = "ManyToMany"; @Override public List nodesToVisit() { return Arrays.asList(Kind.VARIABLE); @@ -37,9 +41,10 @@ public void visitNode(Tree tree) { for(AnnotationTree annotationTree : annotations){ //--access only to One - if(ONE_TO_MANY.equals(annotationTree.annotationType().symbolType().name())||MANY_TO_ONE.equals(annotationTree.annotationType().symbolType().name())){ + if(ONE_TO_MANY.equals(annotationTree.annotationType().symbolType().name()) || MANY_TO_ONE.equals(annotationTree.annotationType().symbolType().name()) || ONE_TO_ONE.equals(annotationTree.annotationType().symbolType().name())|| MANY_TO_MANY.equals(annotationTree.annotationType().symbolType().name())) { Arguments arguments = annotationTree.arguments(); + boolean fectchFound = false; for (ListIterator it = arguments.listIterator(); it.hasNext(); ) { @@ -51,18 +56,19 @@ public void visitNode(Tree tree) { if(FETCH_KEYWORD.equals(variable.name())){ String fetchValue = ((MemberSelectExpressionTree)assignementExpression.expression()).identifier().name(); + fectchFound = true; if(EAGER_KEYWORD.equals(fetchValue)){ reportIssue(tree, MESSAGERULE); } } } - + //-The default case of the ManyToOne and the OneToOne + if(!fectchFound && (MANY_TO_ONE.equals(annotationTree.annotationType().symbolType().name()) || ONE_TO_ONE.equals(annotationTree.annotationType().symbolType().name()))){ + reportIssue(tree, MESSAGERULE); + } } } - } } - - } From bdcf8eaa7e0ea7a52f8cd883e6378081c8b2c8fb Mon Sep 17 00:00:00 2001 From: "Antoine.Meheut" <34522927+AntoineMeheut@users.noreply.github.com> Date: Wed, 5 Apr 2023 21:22:35 +0200 Subject: [PATCH 26/31] Update JavaCheckRegistrarTest.java --- .../fr/greencodeinitiative/java/JavaCheckRegistrarTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java index 3225b1a56..fe7f03adf 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java @@ -32,7 +32,7 @@ void checkNumberRules() { final JavaCheckRegistrar registrar = new JavaCheckRegistrar(); registrar.register(context); - assertThat(context.checkClasses()).hasSize(19); + assertThat(context.checkClasses()).hasSize(20); assertThat(context.testCheckClasses()).isEmpty(); } } From 55677d7b41d484af83ac6c3ad259e97be0035de1 Mon Sep 17 00:00:00 2001 From: adrienpelfresne Date: Thu, 6 Apr 2023 10:28:52 +0200 Subject: [PATCH 27/31] [CRJVM205] refactoring code --- .../ForceUsingLazyFetchTypeInJPAEntity.java | 106 +++++++++++------- 1 file changed, 65 insertions(+), 41 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 075260f61..7746a9f42 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -6,69 +6,93 @@ import org.sonar.plugins.java.api.tree.*; import org.sonar.plugins.java.api.tree.Tree.Kind; -import java.util.Arrays; import java.util.List; -import java.util.ListIterator; @Rule(key = "EC80", name = "Developpement", description = ForceUsingLazyFetchTypeInJPAEntity.MESSAGERULE, priority = Priority.MINOR, tags = {"bug"}) - -public class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisitor { +class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Force the use of LAZY FetchType"; private static final String EAGER_KEYWORD = "EAGER"; private static final String FETCH_KEYWORD = "fetch"; - private static final String ONE_TO_MANY = "OneToMany"; - private static final String MANY_TO_ONE = "ManyToOne"; + private static final String ONE_TO_MANY = "OneToMany"; + private static final String MANY_TO_ONE = "ManyToOne"; private static final String ONE_TO_ONE = "OneToOne"; private static final String MANY_TO_MANY = "ManyToMany"; + @Override public List nodesToVisit() { - return Arrays.asList(Kind.VARIABLE); + return List.of(Kind.VARIABLE); } @Override public void visitNode(Tree tree) { + if (!tree.is(Kind.VARIABLE)) { + return; + } + VariableTree variableTree = (VariableTree) tree; + List annotations = variableTree.modifiers().annotations(); + // get all annotations on the attribute + for (AnnotationTree annotationTree : annotations) { + if (!needToCheckExplicitAnnotation(annotationTree.annotationType().symbolType().name())) { + // no Explicit annotation (@OneToMany, @ManyToOne, @ManyToMany, @OneToOne) was found, + // and we don't need to investigate further + continue; + } + Arguments arguments = annotationTree.arguments(); + performsCheck(arguments, annotationTree, tree); + } + } - if (tree.is(Kind.VARIABLE)) { - VariableTree variableTree = (VariableTree) tree; - List annotations = variableTree.modifiers().annotations(); - //-- get all annotations on the attribut - for(AnnotationTree annotationTree : annotations){ - //--access only to One - - if(ONE_TO_MANY.equals(annotationTree.annotationType().symbolType().name()) || MANY_TO_ONE.equals(annotationTree.annotationType().symbolType().name()) || ONE_TO_ONE.equals(annotationTree.annotationType().symbolType().name())|| MANY_TO_MANY.equals(annotationTree.annotationType().symbolType().name())) { - - Arguments arguments = annotationTree.arguments(); - boolean fectchFound = false; - - for (ListIterator it = arguments.listIterator(); it.hasNext(); ) { - - ExpressionTree argument = it.next(); - - AssignmentExpressionTree assignementExpression = (AssignmentExpressionTree)argument; - - IdentifierTree variable = (IdentifierTree) assignementExpression.variable(); - - if(FETCH_KEYWORD.equals(variable.name())){ - String fetchValue = ((MemberSelectExpressionTree)assignementExpression.expression()).identifier().name(); - fectchFound = true; - if(EAGER_KEYWORD.equals(fetchValue)){ - reportIssue(tree, MESSAGERULE); - } - } - - } - //-The default case of the ManyToOne and the OneToOne - if(!fectchFound && (MANY_TO_ONE.equals(annotationTree.annotationType().symbolType().name()) || ONE_TO_ONE.equals(annotationTree.annotationType().symbolType().name()))){ - reportIssue(tree, MESSAGERULE); - } - } + /** + * perform the check for two case : + * fetch keyword is found -> parse the annotation argument and search for eager keyword + * fetch keyword was not found -> we use the default value of fetch type by join type : + * OneToMany: Lazy + * ManyToOne: Eager + * ManyToMany: Lazy + * OneToOne: Eager + * reporting the issues if necessary + */ + private void performsCheck(Arguments arguments, AnnotationTree annotationTree, Tree tree) { + boolean fetchFound = false; + for (ExpressionTree argument : arguments) { + AssignmentExpressionTree assignmentExpression = (AssignmentExpressionTree) argument; + IdentifierTree variable = (IdentifierTree) assignmentExpression.variable(); + + if (!FETCH_KEYWORD.equals(variable.name())) { + // no need to continue checking this argument + continue; + } + String fetchValue = ((MemberSelectExpressionTree) assignmentExpression.expression()).identifier().name(); + fetchFound = true; + if (EAGER_KEYWORD.equals(fetchValue)) { + reportIssue(tree, MESSAGERULE); } } + //- The default case of the ManyToOne and the OneToOne + // the fetch keyword is not explicit + if (fetchFound) { + return; + } + String symbolType = annotationTree.annotationType().symbolType().name(); + if ((MANY_TO_ONE.equals(symbolType) || ONE_TO_ONE.equals(symbolType))) { + reportIssue(tree, MESSAGERULE); + } + } + + /** + * @param symbolType the annotation type name : @OneToMany, @ManyToOne... + * @return true if searched annotation was found and checks need to be performed + */ + private boolean needToCheckExplicitAnnotation(String symbolType) { + return ONE_TO_MANY.equals(symbolType) + || MANY_TO_ONE.equals(symbolType) + || ONE_TO_ONE.equals(symbolType) + || MANY_TO_MANY.equals(symbolType); } } From 94fde4dd3b419656591bb14696fc6d7ddd1cad4c Mon Sep 17 00:00:00 2001 From: adrienpelfresne Date: Thu, 6 Apr 2023 11:21:12 +0200 Subject: [PATCH 28/31] [CRJVM205] added public keyword to compile again! --- .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 7746a9f42..4ea77a857 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -12,7 +12,7 @@ description = ForceUsingLazyFetchTypeInJPAEntity.MESSAGERULE, priority = Priority.MINOR, tags = {"bug"}) -class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisitor { +public class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Force the use of LAZY FetchType"; private static final String EAGER_KEYWORD = "EAGER"; From 5dd3483fb13417218e3dbe27fe0b66cbdc9add6e Mon Sep 17 00:00:00 2001 From: adrienpelfresne Date: Thu, 6 Apr 2023 11:24:00 +0200 Subject: [PATCH 29/31] [CRJVM205] code clean up --- .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 4ea77a857..187065b35 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -19,9 +19,7 @@ public class ForceUsingLazyFetchTypeInJPAEntity extends IssuableSubscriptionVisi private static final String FETCH_KEYWORD = "fetch"; private static final String ONE_TO_MANY = "OneToMany"; private static final String MANY_TO_ONE = "ManyToOne"; - private static final String ONE_TO_ONE = "OneToOne"; - private static final String MANY_TO_MANY = "ManyToMany"; @Override From 3785981374bb2f5568042281b0cdd44655038e66 Mon Sep 17 00:00:00 2001 From: adrienpelfresne Date: Fri, 7 Apr 2023 12:18:41 +0200 Subject: [PATCH 30/31] [CRJVM205] changed from pull request review --- .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java | 3 --- .../test/files/ForceLazyFetchTypeUseDefaultManyToOne.java | 6 +++--- .../test/files/ForceLazyFetchTypeUseDefaultOneToOne.java | 6 +++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 187065b35..80369abf5 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -29,9 +29,6 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { - if (!tree.is(Kind.VARIABLE)) { - return; - } VariableTree variableTree = (VariableTree) tree; List annotations = variableTree.modifiers().annotations(); // get all annotations on the attribute diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java index 40280e997..27f22ccb0 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultManyToOne.java @@ -12,9 +12,9 @@ @Entity @Table(name = "myTable") -public class ForceLazyFetchTypeUseFalseTest implements Serializable { +public class ForceLazyFetchTypeUseFalse implements Serializable { - @ManyToOne // Noncompliant {{Force the use of LAZY FetchType}} - private Order order; + @ManyToOne // Noncompliant {{Force the use of LAZY FetchType}} + private Order order; } diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java index 176830e18..1c3cbe436 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseDefaultOneToOne.java @@ -12,9 +12,9 @@ @Entity @Table(name = "myTable") -public class ForceLazyFetchTypeUseFalseTest implements Serializable { +public class ForceLazyFetchTypeUseFalse implements Serializable { - @OneToOne // Noncompliant {{Force the use of LAZY FetchType}} - private OrderInformation orderInformations; + @OneToOne // Noncompliant {{Force the use of LAZY FetchType}} + private OrderInformation orderInformations; } From beee006575577d21194adf282c648ee95d6a7e32 Mon Sep 17 00:00:00 2001 From: adrienpelfresne Date: Wed, 17 May 2023 08:37:20 +0200 Subject: [PATCH 31/31] [CRJVM205] code review fix --- .../java/checks/ForceUsingLazyFetchTypeInJPAEntity.java | 6 +++++- .../src/test/files/ForceLazyFetchTypeUseFalseTest.java | 8 ++++---- .../src/test/files/ForceLazyFetchTypeUseManyToOne.java | 6 +++--- .../src/test/files/ForceLazyFetchTypeUseOneToOne.java | 6 +++--- .../checks/ForceUsingLazyFetchTypeInJPAEntityTest.java | 4 +++- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java index 80369abf5..76ee2462a 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntity.java @@ -30,7 +30,11 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { VariableTree variableTree = (VariableTree) tree; - List annotations = variableTree.modifiers().annotations(); + ModifiersTree modifierTrees = variableTree.modifiers(); + if (modifierTrees == null) { + return; + } + List annotations = modifierTrees.annotations(); // get all annotations on the attribute for (AnnotationTree annotationTree : annotations) { if (!needToCheckExplicitAnnotation(annotationTree.annotationType().symbolType().name())) { diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java index 855fa5b67..b2df99b91 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseFalseTest.java @@ -12,9 +12,9 @@ @Entity @Table(name = "myTable") -public class ForceLazyFetchTypeUseFalseTest implements Serializable { - - @Column(name = "ORDER", length = 50, nullable = false, unique = false) - private Set items = new HashSet(); +public class ForceLazyFetchTypeUseFalse implements Serializable { + + @Column(name = "ORDER", length = 50, nullable = false, unique = false) + private Set items = new HashSet(); } diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java index 9ec018e05..4635d7d76 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseManyToOne.java @@ -12,9 +12,9 @@ @Entity @Table(name = "myTable") -public class ForceLazyFetchTypeUseFalseTest implements Serializable { +public class ForceLazyFetchTypeUseFalse implements Serializable { - @ManyToOne(fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} - private Order order; + @ManyToOne(fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} + private Order order; } diff --git a/java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java b/java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java index c3957a8bf..fb597aa07 100644 --- a/java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java +++ b/java-plugin/src/test/files/ForceLazyFetchTypeUseOneToOne.java @@ -12,9 +12,9 @@ @Entity @Table(name = "myTable") -public class ForceLazyFetchTypeUseFalseTest implements Serializable { +public class ForceLazyFetchTypeUseFalse implements Serializable { - @OneToOne(fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} - private OrderInformation orderInformations; + @OneToOne(fetch = FetchType.EAGER) // Noncompliant {{Force the use of LAZY FetchType}} + private OrderInformation orderInformations; } diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java index 6447e5b4c..7e3535aa1 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ForceUsingLazyFetchTypeInJPAEntityTest.java @@ -3,7 +3,7 @@ import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; -public class ForceUsingLazyFetchTypeInJPAEntityTest { +class ForceUsingLazyFetchTypeInJPAEntityTest { @Test void checkNonCompliantTests() { @@ -37,6 +37,7 @@ void checkAllTests() { .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) .verifyIssues(); } + @Test void checkDefaultOneToOneTests() { CheckVerifier.newVerifier() @@ -44,6 +45,7 @@ void checkDefaultOneToOneTests() { .withCheck(new ForceUsingLazyFetchTypeInJPAEntity()) .verifyIssues(); } + @Test void checkDefaultManyToOneTests() { CheckVerifier.newVerifier()