[
https://issues.apache.org/jira/browse/HTTPCORE-316?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15233529#comment-15233529
]
Markus Thies commented on HTTPCORE-316:
---------------------------------------
Well, maybe it was was not the best wording..., but I meant, clone(ing) from
its theoretical definition would only make shallow copies. That means, there
should not be a copy made of the List.
But the bug in the code is that the new clone is handled as if it was a real
copy (instead of a clone) and the headers are removed from the clone ( even I
could not understand the intend why the header of the clone should be cleared).
Removing from the clone removed it also from the original, what is not expected
and considered as being the bug.
If any code would rely on the behavior that the new clone has no headers
defined should not use clone but a deep copy (actually copying the object).
The Unit test demonstrates perfectly the correct behavior!
> HeaderGroup clone removes headers from original
> -----------------------------------------------
>
> Key: HTTPCORE-316
> URL: https://issues.apache.org/jira/browse/HTTPCORE-316
> Project: HttpComponents HttpCore
> Issue Type: Bug
> Components: HttpCore
> Affects Versions: 4.2.2
> Environment: All platforms
> Reporter: Markus Thies
> Labels: HeaderGroup, patch
> Fix For: 4.2.3
>
> Original Estimate: 2h
> Remaining Estimate: 2h
>
> The class org.apache.http.message.HeaderGroup provides a method clone().
> If clone is called the original object's headers are removed.
> The root cause of this is, that the class HeaderGroup has only one attribute
> (headers) which is a List.
> As clone does a shallow copy the List is not cloned (that is correct).
> But within the method clone(), the headers of the newly created clone are
> removed (by calling clear()) but they actually also clear the headers of the
> original object (since it is not a copy).
> So this leads to very tricky problems in code where the headers are essential
> to be available in the clone and in the original object.
> Original code:
> public Object clone() throws CloneNotSupportedException {
> HeaderGroup clone = (HeaderGroup) super.clone();
> clone.headers.clear();
> clone.headers.addAll(this.headers);
> return clone;
> }
> Corrected code:
> public Object clone() throws CloneNotSupportedException {
> HeaderGroup clone = (HeaderGroup) super.clone();
> //BUG: would also clear the headers original
> //clone.headers.clear();
> //clone.headers.addAll(this.headers);
> return clone;
> }
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]