nl.jqno.equalsverifier.util
Class ObjectAccessor<T>

java.lang.Object
  extended by nl.jqno.equalsverifier.util.ObjectAccessor<T>
Type Parameters:
T - The specified object's class.

public class ObjectAccessor<T>
extends Object

Wraps an object to provide reflective access to it. ObjectAccessor can copy and scramble the wrapped object.

Author:
Jan Ouwens

Method Summary
 T copy()
          Creates a copy of the wrapped object.
 T copyIntoAnonymousSubclass()
          Creates a copy of the wrapped object, where the copy type is an anonymous subclass of the wrapped object's class.
<S extends T>
S
copyIntoSubclass(Class<S> subclass)
          Creates a copy of the wrapped object, where the copy's type is a specified subclass of the wrapped object's class.
 FieldAccessor fieldAccessorFor(Field field)
          Returns a FieldAccessor for the wrapped object and the specified field.
 T get()
          Returns the wrapped object.
static
<T> ObjectAccessor<T>
of(T object)
          Factory method.
static
<T> ObjectAccessor<T>
of(T object, Class<T> type)
          Factory method.
 void scramble(PrefabValues prefabValues)
          Modifies all fields of the wrapped object that are declared in T and in its superclasses.
 void shallowScramble(PrefabValues prefabValues)
          Modifies all fields of the wrapped object that are declared in T, but not those inherited from superclasses.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

of

public static <T> ObjectAccessor<T> of(T object)
Factory method.

Type Parameters:
T - object's type.
Parameters:
object - The object to wrap.
Returns:
An ObjectAccessor for object.

of

public static <T> ObjectAccessor<T> of(T object,
                                       Class<T> type)
Factory method.

Type Parameters:
T - object's type, or a supertype.
Parameters:
object - The object to wrap.
type - Superclass of object's type, as which it will be treated by ObjectAccessor.
Returns:
An ObjectAccessor for object.

get

public T get()
Returns the wrapped object.

Returns:
The wrapped object.

fieldAccessorFor

public FieldAccessor fieldAccessorFor(Field field)
Returns a FieldAccessor for the wrapped object and the specified field.

Parameters:
field - A field in T.
Returns:
A FieldAccessor for the wrapped object and the specified field.

copy

public T copy()
Creates a copy of the wrapped object. Note: it does a "shallow" copy. Reference fields are not copied recursively.

Returns:
A shallow copy.

copyIntoSubclass

public <S extends T> S copyIntoSubclass(Class<S> subclass)
Creates a copy of the wrapped object, where the copy's type is a specified subclass of the wrapped object's class. Note: it does a "shallow" copy. Reference fields are not copied recursively.

Parameters:
subclass - A subclass of the wrapped object's class.
Returns:
A shallow copy.

copyIntoAnonymousSubclass

public T copyIntoAnonymousSubclass()
Creates a copy of the wrapped object, where the copy type is an anonymous subclass of the wrapped object's class. Note: it does a "shallow" copy. Reference fields are not copied recursively.

Returns:
A shallow copy.

scramble

public void scramble(PrefabValues prefabValues)
Modifies all fields of the wrapped object that are declared in T and in its superclasses. This method is consistent: given two equal objects; after scrambling both objects, they remain equal to each other. It cannot modifiy: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields will be left unmodified.

Parameters:
prefabValues - Prefabricated values to take values from.

shallowScramble

public void shallowScramble(PrefabValues prefabValues)
Modifies all fields of the wrapped object that are declared in T, but not those inherited from superclasses. This method is consistent: given two equal objects; after scrambling both objects, they remain equal to each other. It cannot modifiy: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields will be left unmodified.

Parameters:
prefabValues - Prefabricated values to take values from.


Copyright © 2015. All Rights Reserved.