0

I have two objects - output ( having 20 fields or attributes ) and an object o with some of these fields (varying from 1 - 5) .

I want to compare the values of these all 5 fields of object o with thier values in output object.

Say output object has attributes a to z. and Object o has attributes a to c , so I want to compare the value of output.a,output.b and output.c with o.a,o.b and o.c

Note I dont know if o.a,o.b and o.c exists or not but I am retrieving their attributes from the fields dynamically:

Below is the code. I have marked with the line what is working and what is not working . I dont know how to type cast it as I dont know the attribute names .

  private void CompareObjects(Output output,Object o) throws IllegalAccessException, NoSuchFieldException {

        Class<?>  r = o.getClass();
        Field[] fields = respclass.getDeclaredFields();
        for (Field field : fields) {

          String fname =   field.getType().getSimpleName();
          print((field.getName())output.getClass().getDeclaredField(fname));// NOT Working error line, not able to typecast it with the field.getName()
//          print(field.getType())output.getClass().getDeclaredField(fname)); // NOT Working

        }
kaya3
  • 47,440
  • 4
  • 68
  • 97
Raulp
  • 7,758
  • 20
  • 93
  • 155

2 Answers2

0

field.getName() will return the string literal with field's name so it's obvious that you are not able to cast anything to it

Anyway, what I would suggest would be to rather convert both objects to HashMaps

<fieldName, fieldValue>

and then check whether the output map includes the o map

You can do this easily e.g. using Jackson Mapper described here: Java introspection: object to map

m.antkowicz
  • 13,268
  • 18
  • 37
0

I think you should use two loop to solve the problem.

package com.test.dal;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

public class LTest {
    public static class Output {
        public String a;
        public int b;
        public boolean c;

        public Output(String a, int b, boolean c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    public static class Input {

        public int b;
        public boolean c;

        public Input(int b, boolean c) {
            this.b = b;
            this.c = c;
        }
    }

    public static void main(String[] args) throws Exception {
        Output output = new Output("a", 1, false);
        Input input = new Input(1, false);
        CompareObjects(output, input);

    }

    private static void CompareObjects(Output output, Input o) throws IllegalAccessException, NoSuchFieldException {

        Field[] oFilds = o.getClass().getDeclaredFields();
        Field[] outputFilds = output.getClass().getDeclaredFields();
        for (Field ofield : oFilds) {

            String oFiledName = ofield.getName();
            String oFiledType = ofield.getType().getTypeName();

            for (Field outputField : outputFilds) {
                String outputFiledName = outputField.getName();
                String outputFiledType = outputField.getType().getTypeName();
                if (oFiledName.equals(outputFiledName) && oFiledType.equals(outputFiledType)) {
                    Object oFildObj = ofield.get(o);
                    Object outputFildObj = outputField.get(output);

                    if (oFiledType.equals("int")) {
                        int oFildInt = (int) oFildObj;
                        int outputFildInt = (int) outputFildObj;
                    } else if (oFiledType.equals("boolean")) {
                        boolean oFildInt = (boolean) oFildObj;
                        boolean outputFildInt = (boolean) outputFildObj;
                    }

                }

            }
        }
    }

   
}
sk l
  • 81
  • 1
  • 5