T - type of the left and right object to diff.public class ReflectionDiffBuilder<T> extends Object implements 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.
Diffable,
Diff,
DiffResult,
ToStringStyle,
DiffBuilder| Modifier and Type | Class and Description |
|---|---|
static class |
ReflectionDiffBuilder.Builder<T>
Constructs a new instance.
|
| Constructor and Description |
|---|
ReflectionDiffBuilder(T left,
T right,
ToStringStyle style)
Deprecated.
|
| Modifier and Type | Method and Description |
|---|---|
DiffResult<T> |
build()
Returns a reference to the object being constructed or result being
calculated by the builder.
|
static <T> ReflectionDiffBuilder.Builder<T> |
builder()
Constructs a new
ReflectionDiffBuilder.Builder. |
String[] |
getExcludeFieldNames()
Gets the field names that should be excluded from the diff.
|
ReflectionDiffBuilder<T> |
setExcludeFieldNames(String... excludeFieldNames)
Deprecated.
|
@Deprecated public ReflectionDiffBuilder(T left, T right, ToStringStyle style)
ReflectionDiffBuilder.Builder.
If left == right or left.equals(right) then the builder will not evaluate any calls to append(...) and will return an empty
DiffResult when build() is executed.
left - this object.right - the object to diff against.style - the style will use when outputting the objects, null uses the defaultIllegalArgumentException - if left or right is null.public static <T> ReflectionDiffBuilder.Builder<T> builder()
ReflectionDiffBuilder.Builder.T - type of the left and right object.ReflectionDiffBuilder.Builder.public DiffResult<T> build()
build in interface Builder<DiffResult<T>>SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public String[] getExcludeFieldNames()
@Deprecated public ReflectionDiffBuilder<T> setExcludeFieldNames(String... excludeFieldNames)
ReflectionDiffBuilder.Builder.setExcludeFieldNames(String[]).excludeFieldNames - The field names to exclude from the diff or null.thisCopyright © 2001–2025 The Apache Software Foundation. All rights reserved.