Class ClassCopierBase

java.lang.Object
org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierBase
All Implemented Interfaces:
ClassCopier
Direct Known Subclasses:
ClassCopierOrdinaryImpl

public abstract class ClassCopierBase extends Object implements ClassCopier
A convenient base class for making ClassCopier types. This takes care of checking oldToNew and updating oldToNew when an actual copy is made. All subclasses must override createCopy, which allocates a new result. In some simple cases, this is all that is needed. In the more complex cases, doCopy must also be overridden to make the actual copy.
  • Field Details

    • name

      private String name
    • isReflective

      private boolean isReflective
  • Constructor Details

    • ClassCopierBase

      protected ClassCopierBase(String name)
      Pass a name here that can be used for toString, hashCode, and equals. All different ClassCopier classes derived from this base should have unique names.
    • ClassCopierBase

      protected ClassCopierBase(String name, boolean isReflective)
  • Method Details

    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public final boolean equals(Object obj)
      Overrides:
      equals in class Object
    • copy

      public final Object copy(Map<Object,Object> oldToNew, Object source) throws ReflectiveCopyException
      Make the actual copy of source, using oldToNew to preserve aliasing. This first checks to see whether source has been previously copied. If so, the value obtained from oldToNew is returned. Otherwise,
      1. createCopy( source ) is called to create a new copy of source.
      2. The new copy is placed in oldToNew with source as its key.
      3. doCopy is called to complete the copy.
      This split into two phases isolates all subclasses from the need to update oldToNew. It accommodates simple cases (arrays of primitives for example) that only need to define createCopy, as well as more complex case (general objects) that must first create the copy, update oldToNew, and then do the copy, as otherwise self-references would cause infinite recursion.
      Specified by:
      copy in interface ClassCopier
      Throws:
      ReflectiveCopyException
    • doSpecialCaseCopy

      private Object doSpecialCaseCopy(Map<Object,Object> oldToNew, Object source)
      Sometimes the internal representation of an object will be violated by our field-by-field copy. This method handles those special cases. It suggests that there may be a basic problem with our code logic.
      Parameters:
      oldToNew - a map of already copied objects and their corresponding copies.
      source - the object to copy.
    • isEmptyHashMap

      private boolean isEmptyHashMap(Object source)
      As of JDK 1.7-40, the JDK relies on the HashMap#table field being identical to a constant. That will not be true if we invoke #doByFieldCopy to copy it, so we need special processing.
      Parameters:
      source - the object to copy.
    • cloneEmptyHashMap

      private HashMap cloneEmptyHashMap()
    • doByFieldCopy

      private Object doByFieldCopy(Map<Object,Object> oldToNew, Object source)
    • isReflectiveClassCopier

      public boolean isReflectiveClassCopier()
      Description copied from interface: ClassCopier
      We need to know whether this class copier operates via reflection or not, as the reflective class copier must be able to tell when a super class is copied by an incompatible copier.
      Specified by:
      isReflectiveClassCopier in interface ClassCopier
    • createCopy

      protected abstract Object createCopy(Object source) throws ReflectiveCopyException
      Create a copy of source. The copy may or may not be fully initialized. This method must always be overridden in a subclass.
      Throws:
      ReflectiveCopyException
    • doCopy

      protected Object doCopy(Map<Object,Object> oldToNew, Object source, Object result) throws ReflectiveCopyException
      Do the copying of data from source to result. This just returns the result by default, but it may be overrideden in a subclass. When this method completes, result must be fully initialized.
      Throws:
      ReflectiveCopyException