You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Dave Meikle (JIRA)" <ji...@apache.org> on 2007/08/23 01:40:30 UTC

[jira] Created: (LANG-354) Implementation of a CloneBuilder Class

Implementation of a CloneBuilder Class
--------------------------------------

                 Key: LANG-354
                 URL: https://issues.apache.org/jira/browse/LANG-354
             Project: Commons Lang
          Issue Type: New Feature
            Reporter: Dave Meikle
            Priority: Minor


As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:

 public Object clone() {
     return CloneBuilder.reflectionClone(this);
 }

or

 public Object clone() {
     return new CloneBuilder(this)
         .append("field1")         // note the 'field by name' usage
         .append("field2")         // rather than 'field by value'
         ...
         .append("fieldn")
         .toClone();
 }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-354:
-------------------------------

    Fix Version/s:     (was: 3.0)
                   2.4

Consider for 2.4.

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>             Fix For: 2.4
>
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Dave Meikle (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Dave Meikle updated LANG-354:
-----------------------------

    Comment: was deleted

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-354:
-------------------------------

    Fix Version/s: 3.0

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>             Fix For: 3.0
>
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Ben Speakmon (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12541995 ] 

Ben Speakmon commented on LANG-354:
-----------------------------------

A neat idea. Problems:

1) I don't like calling it CloneBuilder. Cloning is associated with java.lang.Cloneable and Object.clone(), which follow a rather hairy extralinguistic procedure for making clones. It implies a whole bunch of weird conditions and contracts (see Item 10 in Effective Java). Something like ObjectCopier, maybe?
2) Some objects (singletons, system classes, etc.) aren't meant to have multiple instances, and the designers usually go to lengths to stop you (final, private constructor, etc.). This won't work for those classes.
3) Most seriously, this breaks for objects with non-primitive fields. Consider:

        Person joe = new Person();
        joe.setName("Joe");
        joe.setAge(28);
        ArrayList joeKids = new ArrayList();
        joeKids.add("Jack");
        joeKids.add("Jill");
        joe.setDependents(joeKids);

        Person joe2 = (Person) CloneBuilder.reflectionClone(joe);

        joeKids.remove("Jill");    // YOU WERE ADOPTED

        // First Joe doesn't have Jill anymore
        assertTrue(joe.getDependents().contains("Jack"));
        assertFalse(joe.getDependents().contains("Jill"));

        // Second Joe should still have her
        assertTrue(joe2.getDependents().contains("Jack"));
        assertTrue(joe2.getDependents().contains("Jill"));

If you run this test, poor Second Joe lost his Jill too. It's a shallow clone instead of a deep clone -- what this does is copy the reference to the dependents, not the dependents themselves. So if you start fooling around with your original object, you wind up changing things out from under your clone object. The only way to get around this is to have some code in your class that knows how to deep-copy the fields you want, which was the original idea behind Object.clone(). You just can't do the same thing with reflection alone.

So all this really guarantees is that you can copy struct-like objects through reflection, which is pretty long-winded and time-consuming. A copy constructor would be much simpler.

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>             Fix For: 2.4
>
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Bjorn Townsend (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12542039 ] 

Bjorn Townsend commented on LANG-354:
-------------------------------------

I agree with Ben.  It's a cool concept, but there are some fatal limitations that limit its usefulness.  Don't think we can document said limitations concisely enough to make the method useable.

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>             Fix For: 2.4
>
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Dave Meikle (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Dave Meikle updated LANG-354:
-----------------------------

    Attachment:     (was: CloneBuilder.java)

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>         Attachments: CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Dave Meikle (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Dave Meikle updated LANG-354:
-----------------------------

    Attachment:     (was: CloneBuilder.java)

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-354:
-------------------------------

    Fix Version/s:     (was: 2.4)
                   3.0

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>             Fix For: 3.0
>
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Closed: (LANG-354) Implementation of a CloneBuilder Class

Posted by "Dave Meikle (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-354?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Dave Meikle closed LANG-354.
----------------------------

    Resolution: Invalid

This cannot be implemented due to limitations in what can be done.

> Implementation of a CloneBuilder Class
> --------------------------------------
>
>                 Key: LANG-354
>                 URL: https://issues.apache.org/jira/browse/LANG-354
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Dave Meikle
>            Priority: Minor
>             Fix For: 3.0
>
>         Attachments: CloneBuilder.java, CloneBuilderTest.java
>
>
> As discussed on the Mailing List an implementation of a CloneBuilder class to simplify creating basic clone methods like the other builders in Lang. Example usage would be as follows:
>  public Object clone() {
>      return CloneBuilder.reflectionClone(this);
>  }
> or
>  public Object clone() {
>      return new CloneBuilder(this)
>          .append("field1")         // note the 'field by name' usage
>          .append("field2")         // rather than 'field by value'
>          ...
>          .append("fieldn")
>          .toClone();
>  }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.