StatefulBeanToCsvBuilder - ignoring serialVersionUID field in Java Beans
Brought to you by:
aruckerjones,
sconway
Hi,
first of all let me thank you for your library. I was just starting writing my own and then I stumbled upon opencsv and saved me a couple sleepless nights.
I've written a generic importer and exporter and it works like a charm but I wanted to know how you suggest to ignore arbitrary fields, first of all serialVersionUID since my application can export java beans in xml (and csv now).
Thank you,
Giacomo
At the moment I'm assuming you are using the HeaderColumnNameMappingStrategy without explicit annotations. That's the only thing I can imagine that would cause the behavior you're describing. The only suggestion that I have is to use explicit annotations. That way only the fields you annotate will be written.
Do you have another suggestion?
Hi Andrew.
yes I'm using no annotations at all, my idea was to just add an CsvIgnoreAddnotation to the fields I wanted to exclude from the export.
I have something like 500 java beans and yes, I'd like to avoid annotating everything. Each bean has between 5 and 30 fields.
I tought ignoring serialVersionUID could be hardcoded in the library since is a reserved serialization field, but also adding an ignore annotation should be fine.
But I fear at the moment such an annotation does not exist.
I have just committed a change for opencsv 5.0 that will ignore any field named "serialVersionUID" in a bean to be written as long as the bean implements Serializable. I will mull over a further annotation, because I think it sounds good and I think it would be easy to implement.
What worries me a little more at the moment is that while writing tests for this feature, I discovered a bug I added in 5.0 for which we currently do not have a test. I will have to take care of that. So thanks for accidentally helping me find that bug!
I am thinking of adding a method to MappingStrategy (with suitable methods in the builders to pass the information along to MappingStrategy) that takes a java.lang.Class and a java.lang.reflect.Field and tells the MappingStrategy to ignore that particular field completely. I think in this case that's better than an annotation because I would like to provide something for people who may not have source code control over their beans or who at least aren't allowed to alter them. How would you feel about that implementation?
I would be curious to see your implementation. Personally I think what
you are describing is a reverse of the
HeaderColumnNameTranslateMappingStrategy where you pass in a map of columns
you want to map in the csv file to the columns you want to map in the
bean. I added a test to the HeaderColumnNameTranslateMappingStrategyTest
so you can see what I mean.
BTW I just started getting a build error today but it was on code that has
been in for almost a year and a half. Are you seeing this too?
Scott :(
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile
(default-testCompile) on project opencsv: Compilation failure
[ERROR] /Users/scott/Documents/workspace/opencsv-source/src/test/java/com/opencsv/bean/mocks/ErrorLineMappingStrategy.java:[41,13]
error: populateNewBean(String[]) in ErrorLineMappingStrategy cannot
implement populateNewBean(String[]) in MappingStrategy
[ERROR] overridden method does not throw InstantiationException
[ERROR] where T#1,T#2 are type-variables:
[ERROR] T#1 extends Object declared in class ErrorLineMappingStrategy
[ERROR] T#2 extends Object declared in interface MappingStrategy
S
On Tue, Aug 20, 2019 at 12:50 AM Andrew Rucker Jones aruckerjones@users.sourceforge.net wrote:
--
Scott Conway
scott.conway@gmail.com
http://www.conwayfamily.name
Related
Patches:
#57nevermind on the last part. The error must be because of some patch on my
home computer because I was able to compile it without issues on a
different computer. I will look at it this weekend.
Scott :)
On Thu, Aug 22, 2019 at 9:56 PM Scott Conway sconway@users.sourceforge.net
wrote:
--
Scott Conway
scott.conway@gmail.com
http://www.conwayfamily.name
Related
Patches:
#57Well, I think it's right to ignore serialVersionUID if the bean implements Serializable.
Also, an annotation to ignore fields could always be precious and I was surprised that the library didn't already had it.
Passing ignored fields to the builder could be great, as you really could treat with any kind of bean without editing the source, and since you can't add an annotation to a bean at runtime, yes, it could be useful. Maybe you could take a
Map<java.lang.Class, java.lang.reflect.Field>
as a parameter to ignore multiple fields.But I'll be completely satisfied with just the annotation.
Thank you!
Last edit: Giacomo Arru 2019-08-20
I have implemented this for the upcoming release of 5.0, which does not yet have a release date.
opencsv 5.0 has been released.