clone()
is Object
,
so one may have to cast the type returned by this method in order
to use the value returned by it: for example
By contrast, the method {link Cloner#makeClone(Object)} does not require a cast.Path2D path1 = ... Path2D path2 = (Path2D)path1.clone();
Path2D path1 = ...; Path2D path2 = Cloner.makeClone(path1);
In a few corner cases, makeClone(Object)
will not
work. The class Path2D.Double
, for example,
declares its clone
method to be final, which implies
that cloning a subclass of Path2D.Double
will
return an object whose type is Path2D.Double
.
The class SplinePath2D
is such a subclass
(it only adds additional methods, not fields). As a result, the
code
will fail becauseSplinePath2D path1 = ...; Path2D.Double path2 = Cloner.makeClone(path1);
makeClone(Object)
would cast the
cloned object to SplinePath2D
. For this case, one
can use the method makeCastedClone(Class,Object)
:
The first argument of this method provides the compile-time class for the object that will be returned.SplinePath2D path1 = ...; Path2D.Double path2 = Cloner.makeCastedClone(Path2D.Double.class, path1);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <C,
T extends C>
CmakeCastedClone
(Class<C> resultClass, T obj) Clone an object and cast it to a specific type.static <T> T
makeClone
(T obj) Clone an object.static <C,
T extends C>
CmakePartialClone
(Class<C> resultClass, T obj) Deprecated.The name of this method was confusing.
-
Constructor Details
-
Cloner
public Cloner()
-
-
Method Details
-
makeClone
Clone an object.- Type Parameters:
T
- the type of the object to cllone- Parameters:
obj
- the object to clone- Returns:
- a cloned object with the same type as that of its argument
- Throws:
CloneNotSupportedException
- the object could not be cloned
-
makePartialClone
@Deprecated public static <C,T extends C> C makePartialClone(Class<C> resultClass, T obj) throws CloneNotSupportedException Deprecated.The name of this method was confusing. Please usemakeCastedClone(Class,Object)
instead.Partially clone an object.- Type Parameters:
C
- the type of the cloned objectT
- the type of the value to clone- Parameters:
resultClass
- a superclass or interface of the object that will be returnedobj
- the object to clone- Returns:
- a cloned object with the same type as that of its argument
- Throws:
CloneNotSupportedException
- the object could not be cloned or the type of a clone did not match the type requested by the resultClass argument
-
makeCastedClone
public static <C,T extends C> C makeCastedClone(Class<C> resultClass, T obj) throws CloneNotSupportedException Clone an object and cast it to a specific type. This method calls clone() and return an object with a type that is a supertype or interface of the compile-time type of the object being cloned.Some classes in the standard Java library are clonable but declare a public clone() method to be final, but not while the class itself is not final. Examples include
Path2D.Double
: a couple of classes in the org.bzdev.geom package are subclasses of Path2D.Double, but none of the methods of Path2D.Double were overridden. Calling clone() on an instance of a subclass of Path2D.Double will produce an instance of Path2D.Double, not an instance of the subclass. The classSplinePath2D
is a good example. The following code will create a clone of an instance of SplinePath2D:
At runtime, this is equivalent toSplinePath2D spath = ...; Path2D path = Cloner.makeCastedClone(Path2D.Double.class, spath);
but with compile-time tests to ensure that Path2D.Double is in fact a superclass of SplinePath2D. Such a test is not automatic because spath.clone() has a compile-time type ofSplinePath2D spath = ...; Path2D path = (Path2D.Double)(spath.clone());
Object
. Note that
would have failed because the compile-type type of the makeClone method would be the compile-time type of spath (i.e., SplinePath2D) and the actual object returned is an instance of Path2D.Double.SplinePath2D spath = ...; Path2D path = Cloner.makeClone(Path2D.Double.class, spath);
Note: This method will not clone an array.
- Type Parameters:
C
- the type of the cloned objectT
- the type of the value to clone- Parameters:
resultClass
- a superclass or interface of the object that will be returnedobj
- the object to clone- Returns:
- a cloned object whose compile-time type matches the resultClass argument
- Throws:
CloneNotSupportedException
- the object could not be cloned or the type of a clone did not match the type requested by the resultClass argument
-