You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/10/19 16:57:09 UTC

[GitHub] [commons-csv] garydgregory commented on a diff in pull request #276: [CSV-302] CSVFormat.duplicateHeaderMode requires default DISALLOW, fix deserialization of v1.9.0 CSVFormat objects

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


##########
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(String fieldName, 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) {
+            throw new RuntimeException("Failed to set value of field " + fieldName, ex);

Review Comment:
   RuntimeException is an anti-pattern, using IAE or ISE or another more descriptive exception.



##########
src/test/java/org/apache/commons/csv/CSVFormatSerializationTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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}.
+ * @author Markus Spann

Review Comment:
   We don't use `author` tags. Credit is given in the changes.xml file post-merge.



##########
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(String fieldName, Object value) {

Review Comment:
   Use `final` where you can.



-- 
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-unsubscribe@commons.apache.org

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