You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2021/12/26 17:22:48 UTC

[ofbiz-framework] branch trunk updated: Improved: suppresses 2 warnings related to deprecated CSVFormat.DEFAULT.withHeader()

This is an automated email from the ASF dual-hosted git repository.

jleroux pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new e4d4329  Improved: suppresses 2 warnings related to deprecated CSVFormat.DEFAULT.withHeader()
e4d4329 is described below

commit e4d43297a9c0075c9d30daa5051e374218ff3fe6
Author: Jacques Le Roux <ja...@les7arts.com>
AuthorDate: Sun Dec 26 18:22:48 2021 +0100

    Improved: suppresses 2 warnings related to deprecated CSVFormat.DEFAULT.withHeader()
    
    Also adds a try-with-ressource in InvoiceServices::importInvoice to properly
    close csvReader on returns. Hence removes an useless csvReader.close().
    
    Several comments about Eclipse giving wrong advice
    <<Resource leak: '<unassigned Closeable value>' is not closed at this location>>
    It's OK, we are inside try-with-ressources.
    
    Finally removes a redundant final in PartyServices::importParty
---
 .../ofbiz/accounting/invoice/InvoiceServices.java     | 19 ++++++++++++++-----
 .../org/apache/ofbiz/party/party/PartyServices.java   |  4 ++--
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java
index 9a1f5a6..cb967cc 100644
--- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java
+++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java
@@ -3672,8 +3672,7 @@ public class InvoiceServices {
         String organizationPartyId = (String) context.get("organizationPartyId");
         String encoding = System.getProperty("file.encoding");
         String csvString = Charset.forName(encoding).decode(fileBytes).toString();
-        final BufferedReader csvReader = new BufferedReader(new StringReader(csvString));
-        CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
+        CSVFormat fmt = CSVFormat.DEFAULT;
         List<String> errMsgs = new LinkedList<>();
         List<String> newErrMsgs;
         String lastInvoiceId = null;
@@ -3681,7 +3680,7 @@ public class InvoiceServices {
         String newInvoiceId = null;
         int invoicesCreated = 0;
 
-        try {
+        try (BufferedReader csvReader = new BufferedReader(new StringReader(csvString))) {
             for (final CSVRecord rec : fmt.parse(csvReader)) {
                 currentInvoiceId = rec.get("invoiceId");
                 if (lastInvoiceId == null || !currentInvoiceId.equals(lastInvoiceId)) {
@@ -3758,11 +3757,18 @@ public class InvoiceServices {
                         try {
                             invoiceResult = dispatcher.runSync("createInvoice", invoice);
                             if (ServiceUtil.isError(invoiceResult)) {
+                                // Eclipse reports here: Resource leak: '<unassigned Closeable value>' is not closed at this location
+                                // but it's OK. As csvReader is in a try-with-ressource it will be closed anyway
+                                // I prefer to not put @SuppressWarnings("resource") to the whole method
+                                // BTW to be consistent Eclipse should also reports the same issue in PartyService (see there)
                                 return ServiceUtil.returnError(ServiceUtil.getErrorMessage(invoiceResult));
                             }
                         } catch (GenericServiceException e) {
-                            csvReader.close();
                             Debug.logError(e, MODULE);
+                            // Eclipse reports here: Resource leak: '<unassigned Closeable value>' is not closed at this location
+                            // but it's OK. As csvReader is in a try-with-ressource it will be closed anyway
+                            // I prefer to not put @SuppressWarnings("resource") to the whole method
+                            // BTW to be consistent Eclipse should also reports the same issue in PartyService (see there)
                             return ServiceUtil.returnError(e.getMessage());
                         }
                         newInvoiceId = (String) invoiceResult.get("invoiceId");
@@ -3824,10 +3830,13 @@ public class InvoiceServices {
                         try {
                             Map<String, Object> result = dispatcher.runSync("createInvoiceItem", invoiceItem);
                             if (ServiceUtil.isError(result)) {
+                                // Eclipse reports here: Resource leak: '<unassigned Closeable value>' is not closed at this location
+                                // but it's OK. As csvReader is in a try-with-ressource it will be closed anyway
+                                // I prefer to not put @SuppressWarnings("resource") to the whole method
+                                // BTW to be consistent Eclipse should also reports the same issue in PartyService (see there)
                                 return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
                             }
                         } catch (GenericServiceException e) {
-                            csvReader.close();
                             Debug.logError(e, MODULE);
                             return ServiceUtil.returnError(e.getMessage());
                         }
diff --git a/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java b/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java
index 574d931..466c2a3 100644
--- a/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java
+++ b/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java
@@ -2303,7 +2303,7 @@ public class PartyServices {
         ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
         String encoding = System.getProperty("file.encoding");
         String csvString = Charset.forName(encoding).decode(fileBytes).toString();
-        CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
+        CSVFormat fmt = CSVFormat.DEFAULT;
         List<String> errMsgs = new LinkedList<>();
         List<String> newErrMsgs = new LinkedList<>();
         String lastPartyId = null;        // last partyId read from the csv file
@@ -2333,7 +2333,7 @@ public class PartyServices {
 
 
         try (BufferedReader csvReader = new BufferedReader(new StringReader(csvString))) {
-            for (final CSVRecord rec : fmt.parse(csvReader)) {
+            for (CSVRecord rec : fmt.parse(csvReader)) {
                 if (UtilValidate.isNotEmpty(rec.get("partyId"))) {
                     currentPartyId = rec.get("partyId");
                 }