dimanche 23 octobre 2016

How do I call a constructor in java via reflection? [duplicate]

This question already has an answer here:

I am dumping CSV via jackson. I have a couple of mapping classes and want to pass the mapping class to the CSV export method.

I have an abstract class, extended it to each of the csv column formats. I pass the name of the class to the export function then want to map the data via the constructor for the class and dump it as CSV.

All fine until I get to creating the class that does the mapping and is to be exported.

Invocation exception/Invalid number of parameters exception.

protected String mapTransactionsToCSV(List<Object[]> results, String rowClassName) 
  Class rowClass  = Class.forName(rowClassName);
  for (Object[] component : results)
    VehicleAbstract vehicle  = (VehicleAbstract) rowClass.getDeclaredConstructor(Object[].class).newInstance(component);
    csv.append(mapper.writer(schema).writeValueAsString(vehicle));
  }
}

My specific class (and abstract class, which I just copied to try). has 2 constructors

public Bus() {} 
public Bus(Object[] component) {}





Aucun commentaire:

Enregistrer un commentaire