0

I know that is not the best solution but I need to add attributes (and relative methods get/set) on a class dynamically: each time the attribute names (and values of course) must be differents.
Why? Simple.

I have to use a class to print some CSV, (CsvBuilderService).

This class is initialized taking another class:

CSVBuilderService<DTO> builder = new CSVBuilderService<DTO>();

Now the CSVBuilderService is initialized to print a list of DTO, the attribute names of DTO are used as columns and the values as rows.

The problem is: the DTO format can change every time, different column names and a different number of columns. (they are query results from different tables each time).

So, I have to add dynamically each time a different number of attributes to DTO with different names, someone has an idea to achieve this?

I didn't search another method to make the CSV since i must use CsvBuilder, i can't use any other methods.

Cœur
  • 37,241
  • 25
  • 195
  • 267
RemovedQuasar
  • 31
  • 1
  • 13
  • 2
    You need to define what you mean by "dynamically". Do you want to change the class while your application is running? Or do you expect to recompile it every time the attribute names change? – biziclop May 03 '17 at 09:26
  • 1
    The first one, adding attributes and methods. I need different attributes each time, I need the right number of attributes to print the right number of relative columns. – RemovedQuasar May 03 '17 at 09:29
  • 3
    Is this another case of "I don't know how to use `Map` or other collections"? You really do not need to create properties dynamically. You think you do, but no you don't. – Kayaman May 03 '17 at 09:34
  • Possible duplicate of [Can you recommend a Java library for reading (and possibly writing) CSV files?](http://stackoverflow.com/questions/200609/can-you-recommend-a-java-library-for-reading-and-possibly-writing-csv-files) – Timothy Truckle May 03 '17 at 09:36
  • About the CSV, i must use that method, i had already used a method that print correctly but..orders are orders. About Maps, i don't know if try trully works correctly on this case, i need perfect attributes each time. – RemovedQuasar May 03 '17 at 09:40
  • Possible duplicate of [How to add attributes Dynamically for java object?](http://stackoverflow.com/questions/12832554/how-to-add-attributes-dynamically-for-java-object) – AxelH May 04 '17 at 08:38
  • Separate the data logic from the csv/print logic – mike May 04 '17 at 08:38
  • This has nothing to do with CSV then, just how to modify a class byte code. I would not do that. Use an abstract class an implement every possibilites (if there is not 100...). Then implement the solution to use the correct builder – AxelH May 04 '17 at 08:38

2 Answers2

0

Since you want to match your DTO to the column of a query, the simpler solution (and cleanest for me at least) would be to use OO.

Implement subclasses of DTO for every possibilities, study the best approch here based on similar subclass.

Then you just need to update the initialisation of CSVBuilderService to get a specific builder CSVBuilderService<T extends DTO> based on the query. Without having any idea of what is going on in there, I can't really help you more. But this is the solution I use to manage DAO on a project.

AxelH
  • 14,325
  • 2
  • 25
  • 55
0

Is it possible to generate code dynamically?

Yes. ASM, javassist, javapoet, etc.

Is it your case?

Likely no. Why? Examples of real usecases for that are adaptive systems, high performance.

I know that is not the best solution

And this is one of the best solution, when you program adaptive systems and/or has very strong requirements to performance.

Should you use OOP?

Likely yes.

Should you write all classes?

Likely no. For generating CSV you need only String[] array to know headers and Object[] array to handle rows. There is no problem write one class, which will generate this metadata accordinly to your inputs (with reflection for example) and cache. I suppose you should go deeper and see what you have besides CSVBuilderService to implement your logic. Maybe it simpler to extend CSVBuilderService.

Anyway, if you not program some specific algorithms for each particular class, which you intend to generate, you benefits more if just create another CSV writer, which will write CSV using metadata from your inputs. It reduces GC overhead and complexity of your code.

egorlitvinenko
  • 2,736
  • 2
  • 16
  • 36