LANG-1067: Add a reflection-based variant of DiffBuilder

This commit is contained in:
pascalschumacher 2016-11-06 21:06:52 +01:00
parent 7d54a14a75
commit 011775551e
2 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,140 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.builder;
import static org.apache.commons.lang3.reflect.FieldUtils.readField;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
/**
* <p>
* Assists in implementing {@link Diffable#diff(Object)} methods.
* </p>
* <p>
* All non-static, non-transient fields (including inherited fields)
* of the objects to diff are discovered using reflection and compared
* for differences.
* </p>
*
* <p>
* To use this class, write code as follows:
* </p>
*
* <pre>
* public class Person implements Diffable&lt;Person&gt; {
* String name;
* int age;
* boolean smoker;
* ...
*
* public DiffResult diff(Person obj) {
* // No need for null check, as NullPointerException correct if obj is null
* return new ReflectionDiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE)
* .build();
* }
* }
* </pre>
*
* <p>
* The {@code ToStringStyle} passed to the constructor is embedded in the
* returned {@code DiffResult} and influences the style of the
* {@code DiffResult.toString()} method. This style choice can be overridden by
* calling {@link DiffResult#toString(ToStringStyle)}.
* </p>
* @see Diffable
* @see Diff
* @see DiffResult
* @see ToStringStyle
* @since 3.6
*/
public class ReflectionDiffBuilder implements Builder<DiffResult> {
private final Object left;
private final Object right;
private final DiffBuilder diffBuilder;
/**
* <p>
* Constructs a builder for the specified objects with the specified style.
* </p>
*
* <p>
* If {@code lhs == rhs} or {@code lhs.equals(rhs)} then the builder will
* not evaluate any calls to {@code append(...)} and will return an empty
* {@link DiffResult} when {@link #build()} is executed.
* </p>
* @param <T>
* type of the objects to diff
* @param lhs
* {@code this} object
* @param rhs
* the object to diff against
* @param style
* the style will use when outputting the objects, {@code null}
* uses the default
* @throws IllegalArgumentException
* if {@code lhs} or {@code rhs} is {@code null}
*/
public <T> ReflectionDiffBuilder(final T lhs, final T rhs, final ToStringStyle style) {
this.left = lhs;
this.right = rhs;
diffBuilder = new DiffBuilder(lhs, rhs, style);
}
@Override
public DiffResult build() {
if (left.equals(right)) {
return diffBuilder.build();
}
appendFields(left.getClass());
return diffBuilder.build();
}
private void appendFields(final Class<?> clazz) {
for (final Field field : FieldUtils.getAllFields(clazz)) {
if (accept(field)) {
try {
diffBuilder.append(field.getName(), readField(field, left, true),
readField(field, right, true));
} catch (final IllegalAccessException ex) {
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
}
}
}
}
private boolean accept(final Field field) {
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
return false;
}
if (Modifier.isTransient(field.getModifiers())) {
return false;
}
if (Modifier.isStatic(field.getModifiers())) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,131 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang3.builder;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class ReflectionDiffBuilderTest {
private static final ToStringStyle SHORT_STYLE = ToStringStyle.SHORT_PREFIX_STYLE;
@SuppressWarnings("unused")
private static class TypeTestClass implements Diffable<TypeTestClass> {
private ToStringStyle style = SHORT_STYLE;
private boolean booleanField = true;
private boolean[] booleanArrayField = {true};
private byte byteField = (byte) 0xFF;
private byte[] byteArrayField = {(byte) 0xFF};
private char charField = 'a';
private char[] charArrayField = {'a'};
private double doubleField = 1.0;
private double[] doubleArrayField = {1.0};
private float floatField = 1.0f;
private float[] floatArrayField = {1.0f};
int intField = 1;
private int[] intArrayField = {1};
private long longField = 1L;
private long[] longArrayField = {1L};
private short shortField = 1;
private short[] shortArrayField = {1};
private Object objectField = null;
private Object[] objectArrayField = {null};
private static int staticField;
private transient String transientField;
@Override
public DiffResult diff(final TypeTestClass obj) {
return new ReflectionDiffBuilder(this, obj, style).build();
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, false);
}
@Override
public boolean equals(final Object obj) {
return EqualsBuilder.reflectionEquals(this, obj, false);
}
}
@SuppressWarnings("unused")
private static class TypeTestChildClass extends TypeTestClass {
String field = "a";
}
@Test
public void test_no_differences() {
final TypeTestClass firstObject = new TypeTestClass();
final TypeTestClass secondObject = new TypeTestClass();
final DiffResult list = firstObject.diff(secondObject);
assertEquals(0, list.getNumberOfDiffs());
}
@Test
public void test_primitive_difference() {
final TypeTestClass firstObject = new TypeTestClass();
firstObject.charField = 'c';
final TypeTestClass secondObject = new TypeTestClass();
final DiffResult list = firstObject.diff(secondObject);
assertEquals(1, list.getNumberOfDiffs());
}
@Test
public void test_array_difference() {
final TypeTestClass firstObject = new TypeTestClass();
firstObject.charArrayField = new char[] { 'c' };
final TypeTestClass secondObject = new TypeTestClass();
final DiffResult list = firstObject.diff(secondObject);
assertEquals(1, list.getNumberOfDiffs());
}
@Test
public void test_transient_field_difference() {
final TypeTestClass firstObject = new TypeTestClass();
firstObject.transientField = "a";
final TypeTestClass secondObject = new TypeTestClass();
firstObject.transientField = "b";
final DiffResult list = firstObject.diff(secondObject);
assertEquals(0, list.getNumberOfDiffs());
}
@Test
public void test_no_differences_inheritance() {
final TypeTestChildClass firstObject = new TypeTestChildClass();
final TypeTestChildClass secondObject = new TypeTestChildClass();
final DiffResult list = firstObject.diff(secondObject);
assertEquals(0, list.getNumberOfDiffs());
}
@Test
public void test_difference_in_inherited_field() {
final TypeTestChildClass firstObject = new TypeTestChildClass();
firstObject.intField = 99;
final TypeTestChildClass secondObject = new TypeTestChildClass();
final DiffResult list = firstObject.diff(secondObject);
assertEquals(1, list.getNumberOfDiffs());
}
}