garydgregory commented on code in PR #276:
URL: https://github.com/apache/commons-csv/pull/276#discussion_r999860555


##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2280,6 +2279,27 @@ private void validate() throws IllegalArgumentException {
         }
     }
 
+    private Object readResolve() throws ObjectStreamException {
+        // check whether field duplicateHeaderMode (as of 1.10.0) was found in 
serialized form
+        // if not, set from boolean allowDuplicateHeaderNames
+        if (duplicateHeaderMode == null) {
+            setFieldValue("duplicateHeaderMode",
+                    allowDuplicateHeaderNames ? DuplicateHeaderMode.ALLOW_ALL 
: DuplicateHeaderMode.DISALLOW);
+        }
+        return this;
+    }
+
+    private void setFieldValue(final String fieldName, final Object value) {
+        try {
+            Field fld = getClass().getDeclaredField(fieldName);

Review Comment:
   Use final where you can.



##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2280,6 +2279,27 @@ private void validate() throws IllegalArgumentException {
         }
     }
 
+    private Object readResolve() throws ObjectStreamException {
+        // check whether field duplicateHeaderMode (as of 1.10.0) was found in 
serialized form
+        // if not, set from boolean allowDuplicateHeaderNames
+        if (duplicateHeaderMode == null) {
+            setFieldValue("duplicateHeaderMode",
+                    allowDuplicateHeaderNames ? DuplicateHeaderMode.ALLOW_ALL 
: DuplicateHeaderMode.DISALLOW);
+        }
+        return this;
+    }
+
+    private void setFieldValue(final String fieldName, final Object value) {
+        try {
+            Field fld = getClass().getDeclaredField(fieldName);
+            fld.setAccessible(true); // make field non-final
+            fld.set(this, value);
+            fld.setAccessible(false);
+        } catch (Exception ex) {

Review Comment:
   Unlikely to need to catch `Exception`, catching 
`ReflectiveOperationException` is cleaner IMO. But where is this used from 
anyway? If it's only called from `readResolve()` why not turn it into an 
`ObjectStreamException`?



##########
src/test/java/org/apache/commons/csv/CSVFormatSerializationTest.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.csv;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Test deserialization of class {@link CSVFormat}.
+ */
+class CSVFormatSerializationTest {
+
+    @ParameterizedTest(name = "[{index}] {0} ({1})")
+    @CsvSource(delimiterString = ";", value = {
+            "1.1.9;CSVFormat-Excel-1.9.0.ser"
+            //BROKEN ,"1.8;CSVFormat-Excel-1.8.ser" // field 
CSVFormat.delimiter was of type char (now String)
+            //BROKEN ,"1.7;CSVFormat-Excel-1.7.ser" // field 
CSVFormat.delimiter was of type char (now String)
+    })
+    void testDeserializeCsvFormat(String version, String fileName) throws 
IOException, ClassNotFoundException {
+
+        InputStream resourceAsStream = 
getClass().getResourceAsStream("/org/apache/commons/csv/serialization/" + 
fileName);

Review Comment:
   Use final where you can.
   



##########
src/test/java/org/apache/commons/csv/CSVFormatSerializationTest.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.csv;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Test deserialization of class {@link CSVFormat}.
+ */
+class CSVFormatSerializationTest {

Review Comment:
   Note that the convention in Commons CSV is that test classes and methods are 
public.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to