简体   繁体   中英

How to make my loops more generic?

Below is my code. As you will see there is 2 almost identical blocks of code. The only thing that is different is the type of the object. I am looking for a way to make it more generic. To have one block of code and just set the type (Owner, Car) as a param in my method. Any hint? I've tried using reflection but I never was able to make it dynamic cause I had to loop over the collection

if(queryResult.get(0) instanceof Owner) {
        classFields = Owner.class.getDeclaredFields();
        data = new Object[queryResult.size()][classFields.length];
        for(Owner owner : (List<Owner>)(Object)queryResult) {
            int rCounter = 0;
            for(Field field : owner.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    data[lCounter][rCounter] = field.get(owner);
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                }

                rCounter++;
            }
            lCounter++;
        }
    } else if(queryResult.get(0) instanceof Car) {
        classFields = Car.class.getDeclaredFields();
        data = new Object[queryResult.size()][classFields.length];
        for(Car car : (List<Car>)(Object)queryResult) {
            int rCounter = 0;
            for(Field field : car.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    data[lCounter][rCounter] = field.get(car);
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                }

                rCounter++;
            }
            lCounter++;
        }
    }

You should be able to collapse your entire code block down to:

 Class clazz = queryResult.get(0).getClass();
 classFields = clazz.getDeclaredFields();
 data = new Object[queryResult.size()][classFields.length];
 for(Object result : queryResult) {
     int rCounter = 0;
     for(Field field : clazzFields) {
         field.setAccessible(true);
          try {
              data[lCounter][rCounter] = field.get(result);
          } catch (IllegalArgumentException e1) {
               e1.printStackTrace();
          } catch (IllegalAccessException e1) {
               e1.printStackTrace();
          }
          rCounter++;
      }
      lCounter++;
}

Warning: I didn't compile and run this to check

You don't need this code repetition. Just use car and owner
as instances of Object . You don't use the types Car and Owner
in any different/meaningful way. So your code will continue to work fine
because both cases are processed the same way. Seems the only thing
you need to change is this:

Car.class.getDeclaredFields(); and
Owner.class.getDeclaredFields();

to this:

queryResult.get(0).getClass().getDeclaredFields();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM