public static int reflectionCompare(Object lhs, Object rhs, boolean testTransients) { if (lhs == rhs) { return 0; } if (lhs == null || rhs == null) { throw new NullPointerException(); } Class c1 = lhs.getClass(); if (!c1.isInstance(rhs)) { throw new ClassCastException(); } Field[] fields = c1.getDeclaredFields(); Field.setAccessible(fields, true); CompareToBuilder compareToBuilder = new CompareToBuilder(); for (int i = 0; i < fields.length && compareToBuilder.comparison == 0; ++i) { Field f = fields[i]; if (testTransients || !Modifier.isTransient(f.getModifiers())) { if ( !Modifier.isStatic(f.getModifiers())) { try { compareToBuilder.append(f.get(lhs), f.get(rhs)); } catch (IllegalAccessException e) {   throw new InternalError("Unexpected IllegalAccessException"); } } } } return compareToBuilder.toComparison();
public static int reflectionCompare(Object lhs, Object rhs) { return reflectionCompare(lhs, rhs, false);
    public static int reflectionCompare(Object lhs, Object rhs,             boolean testTransients) {        if (lhs == rhs) {            return 0;        }        if (lhs == null || rhs == null) {            throw new NullPointerException();        }        Class c1 = lhs.getClass();        if (!c1.isInstance(rhs)) {            throw new ClassCastException();        }        Field[] fields = c1.getDeclaredFields();        Field.setAccessible(fields, true);        CompareToBuilder compareToBuilder = new CompareToBuilder();        for (int i = 0; i < fields.length && compareToBuilder.comparison == 0; ++i) {            Field f = fields[i];            if (testTransients || !Modifier.isTransient(f.getModifiers())) {                if ( !Modifier.isStatic(f.getModifiers())) {                    try {                        compareToBuilder.append(f.get(lhs), f.get(rhs));                    } catch (IllegalAccessException e) {                        //this can't happen. Would get a Security exception instead                        //throw a runtime exception in case the impossible happens.                        throw new InternalError("Unexpected IllegalAccessException");                    }                }            }        }        return compareToBuilder.toComparison();    }