1.1 --- a/emul/src/main/java/java/lang/Object.java Thu Jan 03 16:30:09 2013 +0100
1.2 +++ b/emul/src/main/java/java/lang/Object.java Mon Jan 07 16:46:09 2013 +0100
1.3 @@ -221,7 +221,28 @@
1.4 * be cloned.
1.5 * @see java.lang.Cloneable
1.6 */
1.7 - protected native Object clone() throws CloneNotSupportedException;
1.8 + protected Object clone() throws CloneNotSupportedException {
1.9 + Object ret = clone(this);
1.10 + if (ret == null) {
1.11 + throw new CloneNotSupportedException(getClass().getName());
1.12 + }
1.13 + return ret;
1.14 + }
1.15 +
1.16 + @JavaScriptBody(args = "self", body =
1.17 + "\nif (!self.$instOf_java_lang_Cloneable) {"
1.18 + + "\n return null;"
1.19 + + "\n} else {"
1.20 + + "\n var clone = self.constructor(true);"
1.21 + + "\n var props = Object.getOwnPropertyNames(self);"
1.22 + + "\n for (var i = 0; i < props.length; i++) {"
1.23 + + "\n var p = props[i];"
1.24 + + "\n clone[p] = self[p];"
1.25 + + "\n };"
1.26 + + "\n return clone;"
1.27 + + "\n}"
1.28 + )
1.29 + private static native Object clone(Object self) throws CloneNotSupportedException;
1.30
1.31 /**
1.32 * Returns a string representation of the object. In general, the
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CloneTest.java Mon Jan 07 16:46:09 2013 +0100
2.3 @@ -0,0 +1,76 @@
2.4 +/**
2.5 + * Back 2 Browser Bytecode Translator
2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.7 + *
2.8 + * This program is free software: you can redistribute it and/or modify
2.9 + * it under the terms of the GNU General Public License as published by
2.10 + * the Free Software Foundation, version 2 of the License.
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License
2.18 + * along with this program. Look for COPYING file in the top folder.
2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
2.20 + */
2.21 +package org.apidesign.bck2brwsr.tck;
2.22 +
2.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
2.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
2.25 +import org.testng.annotations.Factory;
2.26 +
2.27 +/**
2.28 + *
2.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
2.30 + */
2.31 +public class CloneTest {
2.32 + private int value;
2.33 +
2.34 + @Compare public Object notSupported() {
2.35 + try {
2.36 + return new CloneTest().clone();
2.37 + } catch (CloneNotSupportedException ex) {
2.38 + return ex.getClass().getName() + ":" + ex.getMessage();
2.39 + }
2.40 + }
2.41 +
2.42 + @Compare public String sameClass() throws CloneNotSupportedException {
2.43 + return new Clnbl().clone().getClass().getName();
2.44 + }
2.45 +
2.46 + @Compare public boolean differentInstance() throws CloneNotSupportedException {
2.47 + Clnbl orig = new Clnbl();
2.48 + return orig == orig.clone();
2.49 + }
2.50 +
2.51 + @Compare public int sameReference() throws CloneNotSupportedException {
2.52 + CloneTest self = new CloneTest();
2.53 + Clnbl orig = new Clnbl();
2.54 + self.value = 33;
2.55 + orig.ref = self;
2.56 + return ((Clnbl)orig.clone()).ref.value;
2.57 + }
2.58 +
2.59 + @Compare public int sameValue() throws CloneNotSupportedException {
2.60 + Clnbl orig = new Clnbl();
2.61 + orig.value = 10;
2.62 + return ((Clnbl)orig.clone()).value;
2.63 + }
2.64 +
2.65 + @Factory
2.66 + public static Object[] create() {
2.67 + return VMTest.create(CloneTest.class);
2.68 + }
2.69 +
2.70 + public static final class Clnbl implements Cloneable {
2.71 + public CloneTest ref;
2.72 + private int value;
2.73 +
2.74 + @Override
2.75 + public Object clone() throws CloneNotSupportedException {
2.76 + return super.clone();
2.77 + }
2.78 + }
2.79 +}