Menu

#116 Ignore static final fields

wont-fix
None
5
2019-11-16
2019-11-13
Jim Sellers
No

Please ignore any static final fields by default. I don't think that it makes sense to write these since they are typically specific to the class, not to the instance. It just makes a row of the same data.

`
public class StaticFinalTest {

private static final Random RANDOM = new Random();

public static void main(String[] args) {

    List<ModelClass> models = new ArrayList<>();

    // generate some model rows
    for (int i = 0; i < 10; i++) {
        ModelClass model = new ModelClass();
        model.setId(String.valueOf(RANDOM.nextInt(500)));
        model.setName("Bob #" + System.currentTimeMillis());
        models.add(model);
    }

    // now write it to a file
    File outFile = new File("target/example.csv");
    outFile.getParentFile().mkdirs();
    try (Writer writer = Files.newBufferedWriter(outFile.toPath())) {
        StatefulBeanToCsv<ModelClass> beanToCsv = new StatefulBeanToCsvBuilder<ModelClass>(writer).build();
        beanToCsv.write(models);
    } catch (Exception e) {
        throw new RuntimeException("Could not write the csv file", e);
    }
}

public static class ModelClass {

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public String id;

    public String name;

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
}

}
`

Discussion

  • Andrew Rucker Jones

    I do have one concern, besides the fact that this changes existing behavior: if we exclude these, we would have to develop a mechanism for reincluding them by force, if a user wants to. That might get a little ugly. Might it not make more sense for you to exclude them yourself using the CsvIgnore annotation introduced in 5.0?

     
  • Jim Sellers

    Jim Sellers - 2019-11-16

    I had tried that on code other than the example and I didn't think that it worked. But it does work for the example if I add that.

    I'll dig into the issue on my side, but at this point you can probably cancel this ticket.

    Thanks for your time.

     
  • Andrew Rucker Jones

    • status: open --> wont-fix
    • assigned_to: Andrew Rucker Jones
     
  • Andrew Rucker Jones

    Always happy to help any way I can. Let us know if you find anything else!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.