Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for inherited fields #45

Closed
manish-in-java opened this issue Sep 30, 2015 · 8 comments
Closed

Add support for inherited fields #45

manish-in-java opened this issue Sep 30, 2015 · 8 comments

Comments

@manish-in-java
Copy link

I have a class hierarchy such as the following:

abstract class Named {
  @Parsed(field = "Code")
  private String code;
  @Parsed(field = "Name")
  private String name;
}

class Department extends Named {}

class Employee extends Named {}

When I use BeanListProcessor with a CsvParser, the parser fails to read both the fields from the parent class (Named). I can see that this is because of the line Field[] declared = clazz.getDeclaredFields(); in the initialize method of the BeanConversionProcessor class. Due to this line, the CSV parsing code only considers fields declared immediately inside the class being parsed.

I am not sure if this is intended. I certainly feel this behaviour is incorrect as we cannot use class inheritance if inherited fields cannot be read by the parser.

Is there a way to read inherited fields? If not, can this be added?

@jbax jbax added the invalid label Sep 30, 2015
@jbax jbax self-assigned this Sep 30, 2015
@jbax
Copy link
Member

jbax commented Sep 30, 2015

Inherited methods work. Try the following example:

public class Github_45 {

    public static abstract class Named {
        @Parsed(field = "Code")
        private String code;
        @Parsed(field = "Name")
        private String name;

        public Named(){

        }

        public String toString(){
            return code + ": " + name;
        }
    }

    public static class Department extends Named {}

    public static class Employee extends Named {}

    public static void main(String ... args){
        CsvParserSettings settings = new CsvParserSettings();
        BeanListProcessor<Employee> list = new BeanListProcessor<Employee>(Employee.class);
        settings.setRowProcessor(list);

        CsvParser parser = new CsvParser(settings);
        parser.parse(new StringReader("Code,Name\nA,B\nC,D"));

        for(Employee e : list.getBeans()){
            System.out.println(e);
        }
    }
}

You are having trouble because your classes are not public and the parser can't instantiate them.

@jbax jbax closed this as completed Sep 30, 2015
@manish-in-java
Copy link
Author

Jeronimo, thanks for your prompt reply. I took your sample, copied it to a file and ran the code. I got:

null: null
null: null
null: null

As for my original code, I had only provided a quick sample, not the actual classes. Actual classes are all public. You can download the full sample project from http://www.filedropper.com/csv-mapping_1 and run it with Maven as mvn clean test. You will find that the tests fail with the latest version (1.5.6) as well as the one I am using (1.4.0). I am running these on a Windows 7 Professional, 64-bit machine (if that matters).

@manish-in-java
Copy link
Author

See line 75 in BeanConversionProcessor in release 1.5.6 and compare it with the code on the master branch. On the master, the code attempts to go up the class hierarchy looking for fields to parse, whereas it doesn't do that in release 1.5.6.

That explains why the released code does not pick up inherited fields. Most likely, your sample must have been run against the master, which is why it would have succeeded.

@jbax jbax added enhancement and removed invalid labels Sep 30, 2015
@jbax
Copy link
Member

jbax commented Sep 30, 2015

Thanks Manish. You are correct. Use version 2.0.0-SNAPSHOT to get this fixed. We'll release the final 2.0.0 version within a month or so.

@manish-in-java
Copy link
Author

Cool, I am grateful for having stumbled across this library. I am using it in a web app that supports uploading .xls and .csv files. The difference in performance between .xls and .csv parsing is absolutely mind-blowing, and our advanced users who upload .csv files are patently thrilled.

Is it possible for you to release an intermediate version (may be 1.5.7) with just this fix? Our CSV upload feature is currently in limited beta and users are baying to release it across the application. Getting a fix will allow us to release CSV uploads to a wider audience.

@jbax
Copy link
Member

jbax commented Sep 30, 2015

For more immediate needs, we offer paid support and will take care of your problem straight away.

@manish-in-java
Copy link
Author

Jeronimo, do you have a release date for 2.0.0?

@jbax
Copy link
Member

jbax commented Oct 29, 2015

Will be around January, 2016. We are working on a lot of stuff that integrates with univocity-parsers-2.0.0 and some development is still to be made on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants