How to fill all the fields in the class?

前端 未结 5 1644
时光说笑
时光说笑 2021-01-12 16:40

Suppose I have a class that has a lot of different fields. This class is a DTO and for testing purposes, I do not care about actual values, just it exists. Is there any tool

相关标签:
5条回答
  • 2021-01-12 17:18

    You can get the type of the field by reflection, and then initialize it to whatever you want, also using reflection.

    public static void main(String[] args) {
            MyDTO myDTO = new MyDTO();
            Field [] fields = myDTO.getClass().getDeclaredFields();
            for (Field field: fields){
                if (field.getType().equals(Integer.TYPE)){
                    //Primitives are already initialized to 0, so no need to do anything.
                }else if (field.getType().equals(String.class)){
                    System.out.println("String");
                    //Invoke setter method with reflection, initialize to ""
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-12 17:19

    Just a small googling provide this results:

    • EasyRandom simple to use modern java solution, formerly known as Random beans
    • EasyRandom for Java 6, formerly known as JPopulator.
    • PODAM with a tutorial

    else you can use reflection to populate:

    • primitive/wrapper with default value
    • string with rendom value
    • Collection<T>(set,list) with random size and re-using code to populate <T>

    and so on.

    Else XML binding (with jaxb or other technology) can be an option but needs to prepare xml with data in advance.
    Except frameworks all other solutions have two big issues: needs to be written and a lot of testing!

    0 讨论(0)
  • 2021-01-12 17:28

    Write a method with Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors() in a small loop to fill fields as needed. You can recursively call this method to fill classes, collections etc.

    Other way is a XML deserialization using JAXB, XStream or Abraxas.

    0 讨论(0)
  • 2021-01-12 17:37

    If you are using primitives ,they will be automatically set to their default values. In case of Wrapper calss, if you do not care about actual values, you might leave them to null. You can throw a NullPointerException if they are accessed without initializing them.

    For populating it in the list, the simplest way should be create a object of class and adding objects to the list.

    class DTO
    {
      int a;
      String b;
      float c;
     DTO (int a,String b,float c)
       {
         this.a=a;
         this.b=b;
         this.c=c;
       }
    public static void main (String args[])
    
      {
          List <DTO> list = new ArrayList<DTO>();
          DTO o = new DTO (1,"Test",11.3f);
          list.add(o);
       }
    }
    

    Printing the list and overriding toString() should display the values.

    0 讨论(0)
  • 2021-01-12 17:37

    Is there any tool that can traversal through all fields and set primitives, 0 for Number (0.0 for Float, Double, 0 for Integer, 0L for Long, but not null as default), something like "test" for String? Also I want that tool to populate Collections (List, Set, Map).

    AFAIK, there is nothing that will save you much effort over doing it the simple way. It is a trivial matter to declare the fields with an initializer, or to do some simple default initialization in the constructor(s).

    It is probably a good idea to use primitive types rather than wrapper types; e.g. int rather Integer. One advantage is that fields with primitive types are default initialized to 0, 0.0 or false ... saving you the bother.

    0 讨论(0)
提交回复
热议问题