Package org.apache.commons.lang3.builder
Class ReflectionDiffBuilder<T>
- java.lang.Object
-
- org.apache.commons.lang3.builder.ReflectionDiffBuilder<T>
-
- Type Parameters:
T
- type of the left and right object to diff.
- All Implemented Interfaces:
Builder<DiffResult<T>>
public class ReflectionDiffBuilder<T> extends java.lang.Object implements Builder<DiffResult<T>>
Assists in implementingDiffable.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 diff(Person obj) { // No need for null check, as NullPointerException correct if obj is null return new ReflectionDiffBuilder.<Person>builder() .setDiffBuilder(DiffBuilder.<Person>builder() .setLeft(this) .setRight(obj) .setStyle(ToStringStyle.SHORT_PREFIX_STYLE) .build()) .setExcludeFieldNames("userName", "password") .build(); } }
The
ToStringStyle
passed to the constructor is embedded in the returnedDiffResult
and influences the style of theDiffResult.toString()
method. This style choice can be overridden by callingDiffResult.toString(ToStringStyle)
.See
DiffBuilder
for a non-reflection based version of this class.- Since:
- 3.6
- See Also:
Diffable
,Diff
,DiffResult
,ToStringStyle
,DiffBuilder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ReflectionDiffBuilder.Builder<T>
Constructs a new instance.
-
Constructor Summary
Constructors Constructor Description ReflectionDiffBuilder(T left, T right, ToStringStyle style)
Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method 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 newReflectionDiffBuilder.Builder
.java.lang.String[]
getExcludeFieldNames()
Gets the field names that should be excluded from the diff.ReflectionDiffBuilder<T>
setExcludeFieldNames(java.lang.String... excludeFieldNames)
Deprecated.
-
-
-
Constructor Detail
-
ReflectionDiffBuilder
@Deprecated public ReflectionDiffBuilder(T left, T right, ToStringStyle style)
Deprecated.Constructs a builder for the specified objects with the specified style.If
left == right
orleft.equals(right)
then the builder will not evaluate any calls toappend(...)
and will return an emptyDiffResult
whenbuild()
is executed.- Parameters:
left
-this
object.right
- the object to diff against.style
- the style will use when outputting the objects,null
uses the default- Throws:
java.lang.IllegalArgumentException
- ifleft
orright
isnull
.
-
-
Method Detail
-
builder
public static <T> ReflectionDiffBuilder.Builder<T> builder()
Constructs a newReflectionDiffBuilder.Builder
.- Type Parameters:
T
- type of the left and right object.- Returns:
- a new
ReflectionDiffBuilder.Builder
. - Since:
- 3.15.0
-
build
public DiffResult<T> build()
Description copied from interface:Builder
Returns a reference to the object being constructed or result being calculated by the builder.
-
getExcludeFieldNames
public java.lang.String[] getExcludeFieldNames()
Gets the field names that should be excluded from the diff.- Returns:
- Returns the excludeFieldNames.
- Since:
- 3.13.0
-
setExcludeFieldNames
@Deprecated public ReflectionDiffBuilder<T> setExcludeFieldNames(java.lang.String... excludeFieldNames)
Deprecated.Sets the field names to exclude.- Parameters:
excludeFieldNames
- The field names to exclude from the diff ornull
.- Returns:
this
- Since:
- 3.13.0
-
-