I am trying to compare the field values of two different objects in a generic way. I have a function (seen below) that takes in two Objects and then gets the fields and then compares the fields in a loop and adds the fields to a list if they are not the same - is this the proper way to do this?
public void compareFields(Object qa, Object qa4) throws FieldsNotEqualException
{
Field[] qaFields = qa.getClass().getFields();
Field[] qa4Fields = qa4.getClass().getFields();
for(Field f:qaFields)
{
for(Field f4:qa4Fields)
{
if(f4.equals(f))
{
found = true;
break;
}
else
{
continue;
}
}
}
if(!found)
{
report.add(/*some_formatted_string*/) //some global list
throw new FieldsNotEqualException();
}
}
I was googling and I saw that C# had like a PropertyInfo Class - does Java have anything like that?
ALSO, is there a way to do like f.getFieldValue()
-I know there is no method like this but maybe there is another way???
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…