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/29 08:04:58 UTC

[ofbiz-framework] 01/02: 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 release18.12
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git

commit 9853d10bd92dd9e4bd41dd466ef58f9a6dfecdd3
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
    
    Conflicts in InvoiceServices.java handled by hand
---
 .../ofbiz/accounting/invoice/InvoiceServices.java    | 20 +++++++++++++++-----
 .../org/apache/ofbiz/party/party/PartyServices.java  |  4 ++--
 2 files changed, 17 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 4abfdaa..3b1da0c 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
@@ -3552,8 +3552,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;
@@ -3561,7 +3560,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)) {
@@ -3626,11 +3625,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);
+                            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");
@@ -3685,6 +3691,10 @@ 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) {
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 570021f..7bd0079 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
@@ -2326,7 +2326,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
@@ -2356,7 +2356,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");
                 }