简体   繁体   中英

Show different values two objects of one class

I use in my java tests asserting actual and expected values. Ofcouse, throw assert exception if two objects is different. But I want to know, what different.

If anybody know java library, answer me.

Simple: I want print to console all different fields: public, protected, private (primitive types). Object's class can have many inherited class. I want log only simple fields: String, Number, Boolean, Char, items of array[] / collections.

If field is another class, don't log it value (toString function). I want show different only simple fields for this subclass.

For Collections show only different values.

For example:

class A {
private String a;
protected int b;
}


class A1 { 
private int c;
private String d;
}

class B extends A {
private byte[] array;
private List<A> collection
private A1 a1;
}

So, we compare to object class B. If different fields a/b, I show:

field: a. values: Tom / Jerry.

If don't equals a1, then I want know, what field:

field: a1.c. value: 10 / 12.

Similarly for array and collections.

I can parse all fields for all inherited classes. I get each field, get field type. If it simple (String, Number, Boolean...) log different values.

If it's another subclass with own field I can recursively repeat for extract all fields. But I have issue: how I can extract from field value, if it's array or collection? I get from Field::get() object.

How I understood and get from object -> Type[] or Collection{Type} and that class is Type? If I can it, then simple. I compare arrays/collections by size. If size equals, then start equals each item.

To know if you field's type is an array, use isArray() method from Class .

From Collection{Type}, the type of the field is 'Collection', 'Type' is a generic. You can do extra check if you want on the generic by retrieving it with ((ParameterizedTypeImpl) myField.getGenericType()).getActualTypeArguments() but that's probably not necessary.

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