Top Description Inners Fields Constructors Methods
org.apache.commons.lang3.builder

public Class ReflectionDiffBuilder<T>

extends Object
implements Builder<DiffResult<T>>
Class Inheritance
All Implemented Interfaces
org.apache.commons.lang3.builder.Builder
Type Parameters
<T>
type of the left and right object to diff.
Imports
java.lang.reflect.Field, .Modifier, java.util.Arrays, org.apache.commons.lang3.ArraySorter, .ArrayUtils, .ClassUtils, org.apache.commons.lang3.reflect.FieldUtils

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
Diffable, Diff, DiffResult, ToStringStyle, DiffBuilder

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ReflectionDiffBuilder.Builder<
type of the left and right object.
T
>

Constructs a new instance.

Field Summary

Modifier and TypeField and Description
private final DiffBuilder<T>
private String[]
excludeFieldNames

Field names to exclude from output.

Constructor Summary

AccessConstructor and Description
private
ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final String[] excludeFieldNames)

public
ReflectionDiffBuilder(final T
this object.
left
,
final T
the object to diff against.
right
,
final ToStringStyle
the style will use when outputting the objects, null uses the default
style
)
Deprecated Use Builder.

Constructs a builder for the specified objects with the specified style.

Method Summary

Modifier and TypeMethod and Description
private boolean
accept(final Field field)

private void
appendFields(final Class<?> clazz)

Appends fields using reflection.

public DiffResult<T>
build()

Implements org.apache.commons.lang3.builder.Builder.build.

Returns a reference to the object being constructed or result being calculated by the builder.

public static <
type of the left and right object.
T
>
ReflectionDiffBuilder.Builder<T>

Returns:

a new Builder.
builder
()

Constructs a new Builder.

public String[]

Returns:

the excludeFieldNames.
getExcludeFieldNames
()

Gets the field names that should be excluded from the diff.

private T
private T
private Object

Returns:

the field value.
readField
(final Field
the field to use.
field
,
final Object
the object to call on, may be null for static fields.
target
)

Reads a Field, forcing access if needed.

public ReflectionDiffBuilder<T>

Returns:

this instance.
setExcludeFieldNames
(final String...
The field names to exclude from the diff or null.
excludeFieldNames
)

Sets the field names to exclude.

private static String[]
toExcludeFieldNames(final String[] excludeFieldNames)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

diffBuilderback to summary
private final DiffBuilder<T> diffBuilder
excludeFieldNamesback to summary
private String[] excludeFieldNames

Field names to exclude from output. Intended for fields like "password" or "lastModificationDate".

Constructor Detail

ReflectionDiffBuilderback to summary
private ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final String[] excludeFieldNames)
ReflectionDiffBuilderback to summary
public ReflectionDiffBuilder(final T left, final T right, final ToStringStyle style)

Deprecated

Use Builder.

Constructs a builder for the specified objects with the specified style.

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.

Parameters
left:T

this object.

right:T

the object to diff against.

style:ToStringStyle

the style will use when outputting the objects, null uses the default

Annotations
@Deprecated
Exceptions
IllegalArgumentException:
if left or right is null.

Method Detail

acceptback to summary
private boolean accept(final Field field)
appendFieldsback to summary
private void appendFields(final Class<?> clazz)

Appends fields using reflection.

Exceptions
SecurityException:
if an underlying accessible object's method denies the request.
See Also
SecurityManager#checkPermission
buildback to summary
public DiffResult<T> build()

Implements org.apache.commons.lang3.builder.Builder.build.

Doc from org.apache.commons.lang3.builder.Builder.build.

Returns a reference to the object being constructed or result being calculated by the builder.

Annotations
@Override
Exceptions
SecurityException:
if an underlying accessible object's method denies the request.
See Also
SecurityManager#checkPermission
builderback to summary
public static <T> ReflectionDiffBuilder.Builder<T> builder()

Constructs a new Builder.

Parameters
<T>
type of the left and right object.
Returns:ReflectionDiffBuilder.Builder<T>

a new Builder.

Since
3.15.0
getExcludeFieldNamesback to summary
public String[] getExcludeFieldNames()

Gets the field names that should be excluded from the diff.

Returns:String[]

the excludeFieldNames.

Since
3.13.0
getLeftback to summary
private T getLeft()
getRightback to summary
private T getRight()
readFieldback to summary
private Object readField(final Field field, final Object target) throws IllegalAccessException

Reads a Field, forcing access if needed.

Parameters
field:Field

the field to use.

target:Object

the object to call on, may be null for static fields.

Returns:Object

the field value.

Exceptions
IllegalAccessException:
if the field is not made accessible.
NullPointerException:
if the field is null.
SecurityException:
if an underlying accessible object's method denies the request.
See Also
SecurityManager#checkPermission
setExcludeFieldNamesback to summary
public ReflectionDiffBuilder<T> setExcludeFieldNames(final String... excludeFieldNames)

Sets the field names to exclude.

Parameters
excludeFieldNames:String[]

The field names to exclude from the diff or null.

Returns:ReflectionDiffBuilder<T>

this instance.

Annotations
@Deprecated
Since
3.13.0
toExcludeFieldNamesback to summary
private static String[] toExcludeFieldNames(final String[] excludeFieldNames)
org.apache.commons.lang3.builder back to summary

public final Class ReflectionDiffBuilder.Builder<T>

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.lang3.builder.ReflectionDiffBuilder.Builder
Type Parameters
<T>
type of the left and right object.

Constructs a new instance.
Since
3.15.0

Field Summary

Modifier and TypeField and Description
private DiffBuilder<T>
private String[]

Constructor Summary

AccessConstructor and Description
public
Builder()

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public ReflectionDiffBuilder<T>

Returns:

a new configured ReflectionDiffBuilder.
build
()

Builds a new configured ReflectionDiffBuilder.

public ReflectionDiffBuilder.Builder<T>

Returns:

this instance.
setDiffBuilder
(final DiffBuilder<T>
the DiffBuilder.
diffBuilder
)

Sets the DiffBuilder.

public ReflectionDiffBuilder.Builder<T>

Returns:

this instance.
setExcludeFieldNames
(final String...
field names to exclude.
excludeFieldNames
)

Sets field names to exclude from output.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

diffBuilderback to summary
private DiffBuilder<T> diffBuilder
excludeFieldNamesback to summary
private String[] excludeFieldNames

Constructor Detail

Builderback to summary
public Builder()

Constructs a new instance.

Method Detail

buildback to summary
public ReflectionDiffBuilder<T> build()

Builds a new configured ReflectionDiffBuilder.

Returns:ReflectionDiffBuilder<T>

a new configured ReflectionDiffBuilder.

setDiffBuilderback to summary
public ReflectionDiffBuilder.Builder<T> setDiffBuilder(final DiffBuilder<T> diffBuilder)

Sets the DiffBuilder.

Parameters
diffBuilder:DiffBuilder<T>

the DiffBuilder.

Returns:ReflectionDiffBuilder.Builder<T>

this instance.

setExcludeFieldNamesback to summary
public ReflectionDiffBuilder.Builder<T> setExcludeFieldNames(final String... excludeFieldNames)

Sets field names to exclude from output. Intended for fields like "password" or "lastModificationDate".

Parameters
excludeFieldNames:String[]

field names to exclude.

Returns:ReflectionDiffBuilder.Builder<T>

this instance.