[
https://issues.apache.org/jira/browse/LANG-307?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12967257#action_12967257
]
Griffin DeJohn edited comment on LANG-307 at 12/8/10 4:10 PM:
--------------------------------------------------------------
Initial version of AutoClone source code attached. Please let me know if you
have any comments or questions, and if anybody can contribute good test cases,
I'd appreciate it.
The way it works is this. Given a target object that is a reference type, the
first step is to reflectively create a new instance of the dynamic type of that
object. This is accomplished by trying to invoke each declared constructor in
that object's class until a working one is found (starting with the fewest
parameters and working up from there). For each constructor, an argument to
match each parameter is recursively instantiated (with primitives being the
base case, at which point the respective default value is passed). Once an
instance of the target object's class has been successfully instantiated, all
non-static fields in the target object and its superclasses are recursively
cloned (again, with primitives being the base case, at which point the value is
simply assigned to the corresponding field in the clone), ensuring a deep copy.
It works with the null type, primitive types, reference types, and arbitrarily
nested arrays of any type. Instances of static nested classes, inner classes
(nested in a concrete class or an interface), local classes, and anonymous
classes are all handled.
It doesn't work yet with an array that contains itself, and it fails with
instances of non-static nested classes that are dependent on enclosing abstract
classes (haven't figured out a way to dynamically instantiate such a class; any
ideas?). It also fails if necessary fields or constructors are private and
can't be made accessible due to a restrictive security manager, or if a
necessary constructor is picky and throws an exception when passed arguments
that don't meet certain requirements.
The attached version is not generic. It returns the clone as an Object, which
must be cast to the appropriate type. I have already rewritten it to be generic
by using unchecked casts and suppressing the generated compiler warnings, but
while it seems to work well enough, since e.g. "int foo =
AutoClone.toClone(0.0d)" won't compile, I'd still like to hear what other
people think about it.
was (Author: gdejohn):
Initial version of AutoClone source code attached. Please let me know if
you have any comments or questions, and if anybody can help write a good test
plan, I'd appreciate it.
> CloneUtils - utility class to enable cloning via various different mechanisms
> -----------------------------------------------------------------------------
>
> Key: LANG-307
> URL: https://issues.apache.org/jira/browse/LANG-307
> Project: Commons Lang
> Issue Type: New Feature
> Components: lang.*
> Reporter: Henri Yandell
> Fix For: 3.1
>
> Attachments: AutoClone.java, lang-307.patch
>
>
> Taken from the tasks.html. No idea if we want to do it or not.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.