Class ObjectAccessor<T>
- java.lang.Object
-
- nl.jqno.equalsverifier.internal.reflection.ObjectAccessor<T>
-
- Type Parameters:
T
- The specified object's class.
public final class ObjectAccessor<T> extends java.lang.Object
Wraps an object to provide reflective access to it. ObjectAccessor can copy and scramble the wrapped object.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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>
ScopyIntoSubclass(java.lang.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(java.lang.reflect.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, java.lang.Class<T> type)
Factory method.void
scramble(PrefabValues prefabValues, TypeTag enclosingType)
Modifies all fields of the wrapped object that are declared in T and in its superclasses.void
shallowScramble(PrefabValues prefabValues, TypeTag enclosingType)
Modifies all fields of the wrapped object that are declared in T, but not those inherited from superclasses.
-
-
-
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
forobject
.
-
of
public static <T> ObjectAccessor<T> of(T object, java.lang.Class<T> type)
Factory method.- Type Parameters:
T
-object
's type, or a supertype.- Parameters:
object
- The object to wrap.type
- Superclass ofobject
's type, as which it will be treated byObjectAccessor
.- Returns:
- An
ObjectAccessor
forobject
.
-
get
public T get()
Returns the wrapped object.- Returns:
- The wrapped object.
-
fieldAccessorFor
public FieldAccessor fieldAccessorFor(java.lang.reflect.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(java.lang.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.- Type Parameters:
S
- The subclass.- 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, TypeTag enclosingType)
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.enclosingType
- Describes the type that contains this object as a field, to determine any generic parameters it may contain.
-
shallowScramble
public void shallowScramble(PrefabValues prefabValues, TypeTag enclosingType)
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.enclosingType
- Describes the type that contains this object as a field, to determine any generic parameters it may contain.
-
-