# HG changeset patch # User Jaroslav Tulach # Date 1357573569 -3600 # Node ID 6506d5132e0314fa9d1704f048199b8f1c9dc92e # Parent e440a710bc9721cdf413e93cfacdc01c86ed1774 Object.clone seems to be well supported diff -r e440a710bc97 -r 6506d5132e03 emul/src/main/java/java/lang/Object.java --- a/emul/src/main/java/java/lang/Object.java Thu Jan 03 16:30:09 2013 +0100 +++ b/emul/src/main/java/java/lang/Object.java Mon Jan 07 16:46:09 2013 +0100 @@ -221,7 +221,28 @@ * be cloned. * @see java.lang.Cloneable */ - protected native Object clone() throws CloneNotSupportedException; + protected Object clone() throws CloneNotSupportedException { + Object ret = clone(this); + if (ret == null) { + throw new CloneNotSupportedException(getClass().getName()); + } + return ret; + } + + @JavaScriptBody(args = "self", body = + "\nif (!self.$instOf_java_lang_Cloneable) {" + + "\n return null;" + + "\n} else {" + + "\n var clone = self.constructor(true);" + + "\n var props = Object.getOwnPropertyNames(self);" + + "\n for (var i = 0; i < props.length; i++) {" + + "\n var p = props[i];" + + "\n clone[p] = self[p];" + + "\n };" + + "\n return clone;" + + "\n}" + ) + private static native Object clone(Object self) throws CloneNotSupportedException; /** * Returns a string representation of the object. In general, the diff -r e440a710bc97 -r 6506d5132e03 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CloneTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CloneTest.java Mon Jan 07 16:46:09 2013 +0100 @@ -0,0 +1,76 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.tck; + +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class CloneTest { + private int value; + + @Compare public Object notSupported() { + try { + return new CloneTest().clone(); + } catch (CloneNotSupportedException ex) { + return ex.getClass().getName() + ":" + ex.getMessage(); + } + } + + @Compare public String sameClass() throws CloneNotSupportedException { + return new Clnbl().clone().getClass().getName(); + } + + @Compare public boolean differentInstance() throws CloneNotSupportedException { + Clnbl orig = new Clnbl(); + return orig == orig.clone(); + } + + @Compare public int sameReference() throws CloneNotSupportedException { + CloneTest self = new CloneTest(); + Clnbl orig = new Clnbl(); + self.value = 33; + orig.ref = self; + return ((Clnbl)orig.clone()).ref.value; + } + + @Compare public int sameValue() throws CloneNotSupportedException { + Clnbl orig = new Clnbl(); + orig.value = 10; + return ((Clnbl)orig.clone()).value; + } + + @Factory + public static Object[] create() { + return VMTest.create(CloneTest.class); + } + + public static final class Clnbl implements Cloneable { + public CloneTest ref; + private int value; + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + } +}