Class ObjectAccessor<T>

  • Type Parameters:
    T - The specified object's class.

    public abstract class ObjectAccessor<T>
    extends Object
    Wraps an object to provide access to it. ObjectAccessor can copy and scramble the wrapped object.
    • Method Detail

      • get

        public T get()
        Returns the wrapped object.
        Returns:
        The wrapped object.
      • type

        public Class<T> type()
        Returns the type of the object.
        Returns:
        The type of the object.
      • getField

        public T getField​(Field field)
        Returns the value of the given field.
        Parameters:
        field - The field whose value we want to get.
        Returns:
        The value of the given field.
      • copy

        public abstract 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 abstract <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.

        Type Parameters:
        S - The subclass.
        Parameters:
        subclass - A subclass of the wrapped object's class.
        Returns:
        A shallow copy.
      • copyIntoAnonymousSubclass

        public abstract 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 abstract ObjectAccessor<T> scramble​(PrefabValues prefabValues,
                                                   TypeTag enclosingType)
        Modifies all fields of the wrapped object that are declared in T and in its superclasses. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.

        This method is consistent: given two equal objects; after scrambling both objects, they remain equal to each other.

        It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may 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.
        Returns:
        An accessor to the scrambled object.
      • shallowScramble

        public abstract ObjectAccessor<T> shallowScramble​(PrefabValues prefabValues,
                                                          TypeTag enclosingType)
        Modifies all fields of the wrapped object that are declared in T, but not those inherited from superclasses. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.

        This method is consistent: given two equal objects; after scrambling both objects, they remain equal to each other.

        It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may 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.
        Returns:
        An accessor to the scrambled object.
      • clear

        public abstract ObjectAccessor<T> clear​(Predicate<Field> canBeDefault,
                                                PrefabValues prefabValues,
                                                TypeTag enclosingType)
        Clears all fields of the wrapped object to their default values, but only if canBeDefault for the given field returns true. Otherwise, leaves the value intact. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.

        It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.

        Parameters:
        canBeDefault - A predicate that determines for the wrapped object's fields whether or not they are allowed to be 'default', i.e. 0 or null. If a field is marked with @NonNull, for example, it may not be default.
        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.
        Returns:
        An accessor to the cleared object.
      • withDefaultedField

        public abstract ObjectAccessor<T> withDefaultedField​(Field field)
        Clears the given field of the wrapped object to its default value. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.

        It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.

        Parameters:
        field - The field to set to its default value.
        Returns:
        An accessor to the object with the defaulted field.
      • withChangedField

        public abstract ObjectAccessor<T> withChangedField​(Field field,
                                                           PrefabValues prefabValues,
                                                           TypeTag enclosingType)
        Changes the given field of the wrapped object to some unspecified, but different value. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.

        It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.

        Parameters:
        field - The field to set to a different value.
        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.
        Returns:
        An accessor to the object with the changed field.
      • withFieldSetTo

        public abstract ObjectAccessor<T> withFieldSetTo​(Field field,
                                                         Object newValue)
        Changes the given field of the wrapped object to the given value. It may or may not mutate the object of the current ObjectAccessor. Either way, the current ObjectAccessor and any reference to its object should be considered 'spent' after calling this method. The returned ObjectAccessor can safely be used.

        It may not be able to modify: 1. static final fields, and 2. final fields that are initialized to a compile-time constant in the field declaration. These fields may be left unmodified.

        Parameters:
        field - The field to set to the given value.
        newValue - The value to set the field to.
        Returns:
        An accessor to the object with the defaulted field.