Top Description Fields Constructors Methods
org.glassfish.pfl.dynamic.copyobject.impl

public abstract Class ClassCopierBase

extends Object
implements ClassCopier
Class Inheritance
All Implemented Interfaces
org.glassfish.pfl.dynamic.copyobject.impl.ClassCopier
Known Direct Subclasses
org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierOrdinaryImpl
Imports
java.util.HashMap, .Map, org.glassfish.pfl.dynamic.copyobject.spi.ReflectiveCopyException

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 Summary

Modifier and TypeField and Description
private boolean
private String

Constructor Summary

AccessConstructor and Description
protected
ClassCopierBase(String name)

Pass a name here that can be used for toString, hashCode, and equals.

protected
ClassCopierBase(String name, boolean isReflective)

Method Summary

Modifier and TypeMethod and Description
private HashMap<K, V>
public final Object
copy(Map<Object, Object> oldToNew, Object source)

Implements org.glassfish.pfl.dynamic.copyobject.impl.ClassCopier.copy.

Make the actual copy of source, using oldToNew to preserve aliasing.

protected abstract Object
createCopy(Object source)

Create a copy of source.

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

protected Object
doCopy(Map<Object, Object> oldToNew, Object source, Object result)

Do the copying of data from source to result.

private Object
doSpecialCaseCopy(Map<Object, Object>
a map of already copied objects and their corresponding copies.
oldToNew
,
Object
the object to copy.
source
)

Sometimes the internal representation of an object will be violated by our field-by-field copy.

public final boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

private boolean
isEmptyHashMap(Object
the object to copy.
source
)

As of JDK 1.7-40, the JDK relies on the HashMap#table field being identical to a constant.

public boolean
isReflectiveClassCopier()

Implements org.glassfish.pfl.dynamic.copyobject.impl.ClassCopier.isReflectiveClassCopier.

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.

public final String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

isReflectiveback to summary
private boolean isReflective
nameback to summary
private String name

Constructor Detail

ClassCopierBaseback to summary
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.

ClassCopierBaseback to summary
protected ClassCopierBase(String name, boolean isReflective)

Method Detail

cloneEmptyHashMapback to summary
private HashMap<K, V> cloneEmptyHashMap()
copyback to summary
public final Object copy(Map<Object, Object> oldToNew, Object source) throws ReflectiveCopyException

Implements org.glassfish.pfl.dynamic.copyobject.impl.ClassCopier.copy.

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.
Annotations
@Override
createCopyback to summary
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.

doByFieldCopyback to summary
private Object doByFieldCopy(Map<Object, Object> oldToNew, Object source)
doCopyback to summary
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.

doSpecialCaseCopyback to summary
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:Map<Object, Object>

a map of already copied objects and their corresponding copies.

source:Object

the object to copy.

equalsback to summary
public final boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
isEmptyHashMapback to summary
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:Object

the object to copy.

isReflectiveClassCopierback to summary
public boolean isReflectiveClassCopier()

Implements org.glassfish.pfl.dynamic.copyobject.impl.ClassCopier.isReflectiveClassCopier.

Doc from org.glassfish.pfl.dynamic.copyobject.impl.ClassCopier.isReflectiveClassCopier.

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.

Annotations
@Override
toStringback to summary
public final String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override