Hi, I'm currently working on a small prototype to bind CSV data to POJOs using annotation. Camel through its component file:/// and dataformat will provide me the infrastructure that I need to parse the file content, extract each CSV record and return an array of String for each record found in a file.
Remark : A string contains the list of CSV values separated by a comma. In order to bind the CSV values to my POJOs, I have created two annotations : @Retention(RetentionPolicy.RUNTIME) public @interface CSVRecord { String name(); --> root name of the class handling the content of a CSV record String separator(); --> separator used to separate CSV data } @Retention(RetentionPolicy.RUNTIME) public @interface CSVField { int pos(); --> key indicating the position of the CSV data in the CSV record String name(); --> key assigned to the column name of a CSV field } Here is an example of a POJO (= Model) using these annotations : @CSVRecord(separator =",", name = "OrderModel") public class OrderModel { @CSVField(name = "Number", pos = 0) String orderNr; @CSVField(name = "Client Number", pos = 1) String clientNr; @CSVField(name = "ISIN", pos = 2) String ISIN_Code; @CSVField(name = "Name", pos = 3) String Instrument_Name; @CSVField(name = "Quantity", pos = 4) String Quantity; @CSVField(name = "Cur", pos = 5) String Currency; Here is my question ? Using reflection (or project ASM in an next step), I'm able at the launch of the application to discover all the fields annotated and their parameters. With this information, I'm able to set the field value with the CSV data using the following syntax : String[] result = record.split(csv.retrieveSeparator()); --> to retrieve the separator assigned to my CSVRecord .... field.set(order, result[csvField.pos()]); A CSV record can contain one or several classes like : - Order (with reference to a client, instrument, billing, ...), - Client, - Instrument, - Billing where annotations have been defined for the fields corresponding to what we have in CSV record. Remark : In this first prototype implementation, I will only support mapping 1-1 between objects Questions : 1) What is the best to strategy to recursively find all the annotated fields defined within the different classes ? 2) Should we have to create a kind of java tree objects to store the annotated Fields with existing relation between the different classes (= model) ? ex : RootNode = Order, value = list of annotatedFields, childNode = Client, childNode = Instrument, ... 3) Is there performant java algorythm to read the Java Tree in order to find the annotated field corresponding to a key no matter if the field is defined in the parent node or any its children ? Regards, ----- Charles Moulliard SOA Architect My Blog : http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/ -- View this message in context: http://www.nabble.com/Advise-on-%40Annotation---Reflection-%21%21-tp21141422s22882p21141422.html Sent from the Camel - Users mailing list archive at Nabble.com.