mardi 9 novembre 2021

How to get fields names of a class inside another class?

We are creating CSV files from DTO classes, using reflection to get the names of all the column names. ClassName.class.getDeclaredFields() gives us array of all column names.

We have a use case where, we have classes with composition relation, like below:

public class Book {
  private String title;

  private Author author;
}

public class Author {
  private String name;

  private Integer age:
}

Here Book is the root class, i.e., final CSV will be named books.csv

With Book.class.getDeclaredFields(), we only get field names title and author, but we also want field names from Author class (name and age), is there a way to get those?





Aucun commentaire:

Enregistrer un commentaire