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>>
Assists in implementing
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.
- Since:
- 3.6
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classConstructs a new instance. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final DiffBuilder<T> private String[]Field names to exclude from output. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateReflectionDiffBuilder(DiffBuilder<T> diffBuilder, String[] excludeFieldNames) 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()private TgetRight()private ObjectReads aField, forcing access if needed.setExcludeFieldNames(String... excludeFieldNames) Deprecated.private static String[]toExcludeFieldNames(String[] excludeFieldNames)
-
Field Details
-
diffBuilder
-
excludeFieldNames
Field names to exclude from output. Intended for fields like"password"or"lastModificationDate".
-
-
Constructor Details
-
ReflectionDiffBuilder
-
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- 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
-
toExcludeFieldNames
-
accept
-
appendFields
Appends fields using reflection.- Throws:
SecurityException- 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- 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
-
readField
Reads aField, forcing access if needed.- Parameters:
field- the field to usetarget- the object to call on, may benullforstaticfields- Returns:
- the field value
- Throws:
NullPointerException- if the field isnullIllegalAccessException- if the field is not made accessibleSecurityException- if an underlying accessible object's method denies the request.- See Also:
-
setExcludeFieldNames
Deprecated.Sets the field names to exclude.- Parameters:
excludeFieldNames- The field names to exclude from the diff ornull.- Returns:
this- Since:
- 3.13.0
-
ReflectionDiffBuilder.Builder.