/** * <p>Compares two <code>Object</code>s via reflection.</p> * * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code> * is used to bypass normal access control checks. This will fail under a * security manager unless the appropriate permissions are set.</p> * * <ul> * <li>Static fields will not be compared</li> * <li>Transient members will be not be compared, as they are likely derived * fields</li> * <li>Superclass fields will be compared</li> * </ul> * * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>, * they are considered equal.</p> * * @param lhs left-hand object * @param rhs right-hand object * @return a negative integer, zero, or a positive integer as <code>lhs</code> * is less than, equal to, or greater than <code>rhs</code> * @throws NullPointerException if either (but not both) parameters are * <code>null</code> * @throws ClassCastException if <code>rhs</code> is not assignment-compatible * with <code>lhs</code> */ public static int reflectionCompare(final Object lhs, final Object rhs) { return reflectionCompare(lhs, rhs, false, null); }
return reflectionCompare(lhs, rhs, false, null, excludeFields);
return reflectionCompare(lhs, rhs, compareTransients, null);
return reflectionCompare(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields));
/** * Returns the signum of the result of comparing x and y with * <code>CompareToBuilder.reflectionCompare</code> * * @param lhs The "left-hand-side" of the comparison. * @param rhs The "right-hand-side" of the comparison. * @param testTransients Whether to include transients in the comparison * @param excludeFields fields to exclude * @return int The signum */ private int reflectionCompareSignum(final Object lhs, final Object rhs, final boolean testTransients, final String[] excludeFields) { return BigInteger.valueOf(CompareToBuilder.reflectionCompare(lhs, rhs, testTransients)).signum(); }
private void assertXYZCompareOrder(final Object x, final Object y, final Object z, final boolean testTransients, final String[] excludeFields) { assertTrue(0 == CompareToBuilder.reflectionCompare(x, x, testTransients, null, excludeFields)); assertTrue(0 == CompareToBuilder.reflectionCompare(y, y, testTransients, null, excludeFields)); assertTrue(0 == CompareToBuilder.reflectionCompare(z, z, testTransients, null, excludeFields)); assertTrue(0 > CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields)); assertTrue(0 > CompareToBuilder.reflectionCompare(x, z, testTransients, null, excludeFields)); assertTrue(0 > CompareToBuilder.reflectionCompare(y, z, testTransients, null, excludeFields)); assertTrue(0 < CompareToBuilder.reflectionCompare(y, x, testTransients, null, excludeFields)); assertTrue(0 < CompareToBuilder.reflectionCompare(z, x, testTransients, null, excludeFields)); assertTrue(0 < CompareToBuilder.reflectionCompare(z, y, testTransients, null, excludeFields)); }
@Test(expected=ClassCastException.class) public void testReflectionCompareEx2() { final TestObject o1 = new TestObject(4); final Object o2 = new Object(); CompareToBuilder.reflectionCompare(o1, o2); }
@Test public void testReflectionCompare() { final TestObject o1 = new TestObject(4); final TestObject o2 = new TestObject(4); assertTrue(CompareToBuilder.reflectionCompare(o1, o1) == 0); assertTrue(CompareToBuilder.reflectionCompare(o1, o2) == 0); o2.setA(5); assertTrue(CompareToBuilder.reflectionCompare(o1, o2) < 0); assertTrue(CompareToBuilder.reflectionCompare(o2, o1) > 0); }
@Test(expected=NullPointerException.class) public void testReflectionCompareEx1() { final TestObject o1 = new TestObject(4); CompareToBuilder.reflectionCompare(o1, null); }
/** * See "Effective Java" under "Consider Implementing Comparable". * * @param x an object to compare * @param y an object to compare * @param z an object to compare * @param testTransients Whether to include transients in the comparison * @param excludeFields fields to exclude */ private void assertReflectionCompareContract(final Object x, final Object y, final Object z, final boolean testTransients, final String[] excludeFields) { // signum assertTrue(reflectionCompareSignum(x, y, testTransients, excludeFields) == -reflectionCompareSignum(y, x, testTransients, excludeFields)); // transitive if (CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields) > 0 && CompareToBuilder.reflectionCompare(y, z, testTransients, null, excludeFields) > 0){ assertTrue(CompareToBuilder.reflectionCompare(x, z, testTransients, null, excludeFields) > 0); } // un-named if (CompareToBuilder.reflectionCompare(x, y, testTransients, null, excludeFields) == 0) { assertTrue(reflectionCompareSignum(x, z, testTransients, excludeFields) == -reflectionCompareSignum(y, z, testTransients, excludeFields)); } // strongly recommended but not strictly required assertTrue(CompareToBuilder.reflectionCompare(x, y, testTransients) ==0 == EqualsBuilder.reflectionEquals(x, y, testTransients)); }
private void testReflectionHierarchyCompare(final boolean testTransients, final String[] excludeFields) { final TestObject to1 = new TestObject(1); final TestObject to2 = new TestObject(2); final TestObject to3 = new TestObject(3); final TestSubObject tso1 = new TestSubObject(1, 1); final TestSubObject tso2 = new TestSubObject(2, 2); final TestSubObject tso3 = new TestSubObject(3, 3); assertReflectionCompareContract(to1, to1, to1, false, excludeFields); assertReflectionCompareContract(to1, to2, to3, false, excludeFields); assertReflectionCompareContract(tso1, tso1, tso1, false, excludeFields); assertReflectionCompareContract(tso1, tso2, tso3, false, excludeFields); assertReflectionCompareContract("1", "2", "3", false, excludeFields); assertTrue(0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(1, 0), testTransients)); assertTrue(0 != CompareToBuilder.reflectionCompare(tso1, new TestSubObject(0, 1), testTransients)); // root class assertXYZCompareOrder(to1, to2, to3, true, null); // subclass assertXYZCompareOrder(tso1, tso2, tso3, true, null); }
@Override public int compareTo(Object o) { return CompareToBuilder.reflectionCompare(this, o); }
@Override public int compareTo(Object o) { return CompareToBuilder.reflectionCompare(this, o); }
public int compareTo(SerializableStepdoc that) { return CompareToBuilder.reflectionCompare(this, that); }
@Override public int compareTo(final Variable other) { return CompareToBuilder.reflectionCompare(this, other, Lists.newArrayList("type", "pointsTo")); }
/** * <p>Compares two <code>Object</code>s via reflection.</p> * * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code> * is used to bypass normal access control checks. This will fail under a * security manager unless the appropriate permissions are set.</p> * * <ul> * <li>Static fields will not be compared</li> * <li>Transient members will be not be compared, as they are likely derived * fields</li> * <li>Superclass fields will be compared</li> * </ul> * * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>, * they are considered equal.</p> * * @param lhs left-hand object * @param rhs right-hand object * @return a negative integer, zero, or a positive integer as <code>lhs</code> * is less than, equal to, or greater than <code>rhs</code> * @throws NullPointerException if either (but not both) parameters are * <code>null</code> * @throws ClassCastException if <code>rhs</code> is not assignment-compatible * with <code>lhs</code> */ public static int reflectionCompare(final Object lhs, final Object rhs) { return reflectionCompare(lhs, rhs, false, null); }
/** * <p>Compares two <code>Object</code>s via reflection.</p> * * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code> * is used to bypass normal access control checks. This will fail under a * security manager unless the appropriate permissions are set.</p> * * <ul> * <li>Static fields will not be compared</li> * <li>Transient members will be not be compared, as they are likely derived * fields</li> * <li>Superclass fields will be compared</li> * </ul> * * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>, * they are considered equal.</p> * * @param lhs left-hand object * @param rhs right-hand object * @return a negative integer, zero, or a positive integer as <code>lhs</code> * is less than, equal to, or greater than <code>rhs</code> * @throws NullPointerException if either (but not both) parameters are * <code>null</code> * @throws ClassCastException if <code>rhs</code> is not assignment-compatible * with <code>lhs</code> */ public static int reflectionCompare(final Object lhs, final Object rhs) { return reflectionCompare(lhs, rhs, false, null); }
return reflectionCompare(lhs, rhs, compareTransients, null);
@Override public int compareTo(AbstractValueType<?> other) { return reflectionCompare(value(), other.value(), true); }
@Override public int compare(final Viewer viewer, final Object e1, final Object e2) { final Tuple<ClasspathEntryInfo, ModelArchiveMetadata<?, ?>> t1 = cast(e1); final File f1 = t1.getSecond().getLocation(); final Tuple<ClasspathEntryInfo, ModelArchiveMetadata<?, ?>> t2 = cast(e2); final File f2 = t2.getSecond().getLocation(); return reflectionCompare(f1, f2); } });