From 629ef12e1110f512d2855f8b8f1b642ce160895a Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Wed, 15 Apr 2015 16:48:02 +0100 Subject: Make Manifest#clone obey clone's contract. We must call through to Object.clone to ensure that we always return an object of the right type. If not, subclasses will have to override clone() instead of calling through to super.clone(). bug: 19748843 Change-Id: Ie2177bbc05c62281f48780e521ad66de3612561e --- luni/src/main/java/java/util/jar/Manifest.java | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'luni') diff --git a/luni/src/main/java/java/util/jar/Manifest.java b/luni/src/main/java/java/util/jar/Manifest.java index 6a3936d..5a6b42d 100644 --- a/luni/src/main/java/java/util/jar/Manifest.java +++ b/luni/src/main/java/java/util/jar/Manifest.java @@ -41,8 +41,10 @@ public class Manifest implements Cloneable { private static final byte[] VALUE_SEPARATOR = new byte[] { ':', ' ' }; - private final Attributes mainAttributes; - private final HashMap entries; + /* non-final for {@code #clone()} */ + private Attributes mainAttributes; + /* non-final for {@code #clone()} */ + private HashMap entries; static final class Chunk { final int start; @@ -93,9 +95,7 @@ public class Manifest implements Cloneable { */ @SuppressWarnings("unchecked") public Manifest(Manifest man) { - mainAttributes = (Attributes) man.mainAttributes.clone(); - entries = (HashMap) ((HashMap) man - .getEntries()).clone(); + cloneAttributesAndEntriesFrom(man); } Manifest(byte[] manifestBytes, boolean readChunks) throws IOException { @@ -156,7 +156,21 @@ public class Manifest implements Cloneable { */ @Override public Object clone() { - return new Manifest(this); + Manifest result; + try { + result = (Manifest) super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError(e); + } + + result.cloneAttributesAndEntriesFrom(this); + return result; + } + + private final void cloneAttributesAndEntriesFrom(Manifest other) { + mainAttributes = (Attributes) other.mainAttributes.clone(); + entries = (HashMap) ((HashMap) other + .getEntries()).clone(); } /** -- cgit v1.1