Class ReflectionDiffBuilder<T>
- Type Parameters:
T- type of the left and right object to diff.
- All Implemented Interfaces:
Builder<DiffResult<T>>
Diffable.diff(Object) methods.
All non-static, non-transient fields (including inherited fields) of the objects to diff are discovered using reflection and compared for differences.
To use this class, write code as follows:
public class Person implements Diffable<Person> {
String name;
int age;
boolean smoker;
...
public DiffResult<Person> diff(Person obj) {
// No need for null check, as NullPointerException correct if obj is null
return ReflectionDiffBuilder.<Person>builder()
.setDiffBuilder(DiffBuilder.<Person>builder()
.setLeft(this)
.setRight(obj)
.setStyle(ToStringStyle.SHORT_PREFIX_STYLE)
.build())
.setExcludeFieldNames("userName", "password")
.build() // -> ReflectionDiffBuilder
.build(); // -> DiffResult
}
}
The ToStringStyle passed to the constructor is embedded in the returned DiffResult and influences the style of the
DiffResult.toString() method. This style choice can be overridden by calling DiffResult.toString(ToStringStyle).
See DiffBuilder for a non-reflection based version of this class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classConstructs a new instance.Nested classes/interfaces inherited from class org.apache.commons.lang3.builder.AbstractReflection
AbstractReflection.AbstractBuilder<B extends AbstractReflection.AbstractBuilder<B>> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final DiffBuilder<T>private String[]Field names to exclude from output.private static final ThreadLocal<Set<Pair<IDKey,IDKey>>> A registry of objects to detect cyclical object references, avoid infinite loops, and stack overflows. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateReflectionDiffBuilder(DiffBuilder<T> diffBuilder, String[] excludeFieldNames) Constructs a new instance.privateConstructs a new instance.ReflectionDiffBuilder(T left, T right, ToStringStyle style) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionprivate booleanprivate voidappendFields(Class<?> clazz) Appends fields using reflection.build()Returns a reference to the object being constructed or result being calculated by the builder.static <T> ReflectionDiffBuilder.Builder<T>builder()Constructs a newReflectionDiffBuilder.Builder.String[]Gets the field names that should be excluded from the diff.private TgetLeft()Gets the registry of object pairs being traversed by the reflection methods in the current thread.private TgetRight()(package private) static booleanisRegistered(Object lhs, Object rhs) Tests whether the registry contains the given object pair.(package private) static voidRegisters the given object pair.setExcludeFieldNames(String... excludeFieldNames) Deprecated.private static String[]toExcludeFieldNames(String[] excludeFieldNames) (package private) static voidunregister(Object lhs, Object rhs) Unregisters the given object pair.Methods inherited from class org.apache.commons.lang3.builder.AbstractReflection
getForceAccessible, isForceAccessible, isRegistered, register, setAccessible, setAccessible, toRegisterPair, unregister
-
Field Details
-
REGISTRY
A registry of objects to detect cyclical object references, avoid infinite loops, and stack overflows. -
diffBuilder
-
excludeFieldNames
Field names to exclude from output. Intended for fields like"password"or"lastModificationDate".
-
-
Constructor Details
-
ReflectionDiffBuilder
Constructs a new instance.- Parameters:
builder- A non-null Builder.- Throws:
NullPointerException- Thrown on null input.
-
ReflectionDiffBuilder
Constructs a new instance.- Parameters:
diffBuilder- A non-null DiffBuilder.excludeFieldNames- A non-null String array.- Throws:
NullPointerException- Thrown on null input.
-
ReflectionDiffBuilder
Deprecated.Constructs a builder for the specified objects with the specified style.If
left == rightorleft.equals(right)then the builder will not evaluate any calls toappend(...)and will return an emptyDiffResultwhenbuild()is executed.- Parameters:
left-thisobject.right- The object to diff against.style- The style will use when outputting the objects,nulluses the default- Throws:
IllegalArgumentException- Thrown ifleftorrightisnull.
-
-
Method Details
-
builder
Constructs a newReflectionDiffBuilder.Builder.- Type Parameters:
T- type of the left and right object.- Returns:
- A new
ReflectionDiffBuilder.Builder. - Since:
- 3.15.0
-
getRegistry
Gets the registry of object pairs being traversed by the reflection methods in the current thread.- Returns:
- Set the registry of objects being traversed
-
isRegistered
Tests whether the registry contains the given object pair.Used by the reflection methods to avoid infinite loops. Objects might be swapped therefore a check is needed if the object pair is registered in the given or swapped order.
- Parameters:
lhs-thisobject to lookup in registryrhs- The other object to lookup on registry- Returns:
- boolean
trueif the registry contains the given object.
-
register
Registers the given object pair. Used by the reflection methods to avoid infinite loops.- Parameters:
lhs-thisobject to registerrhs- the other object to register
-
toExcludeFieldNames
-
unregister
Unregisters the given object pair.Used by the reflection methods to avoid infinite loops.
- Parameters:
lhs-thisobject to unregisterrhs- the other object to unregister
-
accept
-
appendFields
Appends fields using reflection.- Throws:
SecurityException- Thrown if an underlying accessible object's method denies the request.- See Also:
-
build
Returns a reference to the object being constructed or result being calculated by the builder.- Specified by:
buildin interfaceBuilder<T>- Returns:
- The object constructed or result calculated by the builder.
- Throws:
SecurityException- Thrown if an underlying accessible object's method denies the request.- See Also:
-
getExcludeFieldNames
Gets the field names that should be excluded from the diff.- Returns:
- The excludeFieldNames.
- Since:
- 3.13.0
-
getLeft
-
getRight
-
setExcludeFieldNames
Deprecated.Sets the field names to exclude.- Parameters:
excludeFieldNames- The field names to exclude from the diff ornull.- Returns:
thisinstance.- Since:
- 3.13.0
-
ReflectionDiffBuilder.Builder.