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<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
ToStringStylepassed to the constructor is embedded in the returnedDiffResultand influences the style of theDiffResult.toString()method. This style choice can be overridden by callingDiffResult.toString(ToStringStyle).See
DiffBuilderfor 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 classReflectionDiffBuilder.Builder<T>Constructs a new instance.
-
Field Summary
Fields Modifier and Type Field Description private DiffBuilder<T>diffBuilderprivate java.lang.String[]excludeFieldNamesField names to exclude from output.
-
Constructor Summary
Constructors Modifier Constructor Description privateReflectionDiffBuilder(DiffBuilder<T> diffBuilder, java.lang.String[] excludeFieldNames)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 private booleanaccept(java.lang.reflect.Field field)private voidappendFields(java.lang.Class<?> clazz)Appends fields using reflection.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.private TgetLeft()private TgetRight()private java.lang.ObjectreadField(java.lang.reflect.Field field, java.lang.Object target)Reads aField, forcing access if needed.ReflectionDiffBuilder<T>setExcludeFieldNames(java.lang.String... excludeFieldNames)Deprecated.private static java.lang.String[]toExcludeFieldNames(java.lang.String[] excludeFieldNames)
-
-
-
Field Detail
-
diffBuilder
private final DiffBuilder<T> diffBuilder
-
excludeFieldNames
private java.lang.String[] excludeFieldNames
Field names to exclude from output. Intended for fields like"password"or"lastModificationDate".
-
-
Constructor Detail
-
ReflectionDiffBuilder
private ReflectionDiffBuilder(DiffBuilder<T> diffBuilder, java.lang.String[] excludeFieldNames)
-
ReflectionDiffBuilder
@Deprecated public ReflectionDiffBuilder(T left, T right, ToStringStyle style)
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:
java.lang.IllegalArgumentException- ifleftorrightisnull.
-
-
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
-
toExcludeFieldNames
private static java.lang.String[] toExcludeFieldNames(java.lang.String[] excludeFieldNames)
-
accept
private boolean accept(java.lang.reflect.Field field)
-
appendFields
private void appendFields(java.lang.Class<?> clazz)
Appends fields using reflection.- Throws:
java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
build
public DiffResult<T> build()
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:
- the excludeFieldNames.
- Since:
- 3.13.0
-
getLeft
private T getLeft()
-
getRight
private T getRight()
-
readField
private java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target) throws java.lang.IllegalAccessExceptionReads aField, forcing access if needed.- Parameters:
field- the field to usetarget- the object to call on, may benullforstaticfields- Returns:
- the field value
- Throws:
java.lang.NullPointerException- if the field isnulljava.lang.IllegalAccessException- if the field is not made accessiblejava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
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
-
-