0

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.

4
  • Sounds like you might want to use reflection
    – selig
    Jun 29, 2013 at 19:27
  • @selig, ofcouse :). I have two large questions. Is exists ready library for this? If no, then how I can parse field as array/collection. Jun 29, 2013 at 19:31
  • @YShinkarev: Just to be clear, B will not inherit a, since a is private. Jun 30, 2013 at 6:07
  • @Rahul Bobhate, yes, you are right, but anyway still opened question Jun 30, 2013 at 10:01

1 Answer 1

0

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.