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

        }

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

err i am on Android so that Beaninfo will not wrk. Lookign for dynamic typecasting way.
I'm not talking about 'Bean info' but Jackson mapper - take a look at the second answer in that topic
ok in my case the number of attributes could be more than 2.
and what does it change? Mapping objects to maps will handle any amount of fields, arguments etc (just you will need more maps to check)
Agree with @m.antkowicz you can convert both object to map, and compare each map key&value.
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;
                    }

                }

            }
        }
    }

   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.