[ 
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 6: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 as one base 
case, for which the respective default value is passed, and arrays as the other 
base case, for which an empty array of the same component type is passed). Once 
an instance of the target object's class has been successfully instantiated, 
all non-static fields in the target object from its class and superclasses are 
recursively cloned (with primitives as a base case once again, as well as 
Strings and enum types, for which 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. Classes that maintain their state partly or wholly 
in instance variables declared in a superclass (e.g. StringBuilder) are 
handled, as are instances of static nested classes, inner classes (nested in a 
concrete class or an interface), local classes, and anonymous classes.

It doesn't work yet with an array that contains itself, and it fails with 
instances of inner classes (i.e. 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, or if a 
necessary constructor has parameters that can't be instantiated (e.g. abstract 
classes, enum types, or interfaces). I'm looking into using 
java.lang.reflect.Proxy for that last bit.

The attached version is written to be generic through the use of unchecked 
casts (the generated compiler warnings are suppressed). It seems to work well 
enough, since e.g. "int foo = AutoClone.toClone(0.0d)" won't compile, but 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 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 from its class and 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. Classes that maintain their state partly or wholly 
in instance variables declared in a superclass (e.g. StringBuilder) are 
handled, as are instances of static nested classes, inner classes (nested in a 
concrete class or an interface), local classes, and anonymous classes.

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.
  
> 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.

Reply via email to