Class Cloner

java.lang.Object
org.apache.sis.internal.util.Cloner

@Workaround(library="JDK", version="1.7") public final class Cloner extends Object
Clones objects of arbitrary type using reflection methods. This is a workaround for the lack of public clone() method in the Cloneable interface.
Since:
0.3
Version:
1.1
Author:
Martin Desruisseaux (Geomatys)
  • Constructor Details

    • Cloner

      public Cloner()
      Creates a new Cloner instance which requires public clone() method to be present.
    • Cloner

      public Cloner(boolean isCloneRequired)
      Creates a new Cloner instance.
      Parameters:
      isCloneRequired - whether a CloneNotSupportedException should be thrown if no public clone() method is found.
  • Method Details

    • clone

      public Object clone(Object object) throws CloneNotSupportedException
      Clones the given object. If the given object does not provide a public clone() method, then there is a choice:
      • If isCloneRequired(object) returns true (the default), then a CloneNotSupportedException is thrown.
      • Otherwise the given object is returned.
      Parameters:
      object - the object to clone, or null.
      Returns:
      a clone of the given object, or null if object was null.
      Throws:
      CloneNotSupportedException - if the given object cannot be cloned.
    • cloneIfPublic

      public static Object cloneIfPublic(Object object) throws CloneNotSupportedException
      Clones the given object if its clone() method is public, or returns the same object otherwise. This method may be convenient when there is only one object to clone, otherwise instantiating a new Cloner object is more efficient.
      Parameters:
      object - the object to clone, or null.
      Returns:
      the given object (which may be null) or a clone of the given object.
      Throws:
      CloneNotSupportedException - if the call to Object.clone() failed.
      Since:
      0.6