attila@175: /* attila@175: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. attila@175: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. attila@962: * attila@175: * This code is free software; you can redistribute it and/or modify it attila@175: * under the terms of the GNU General Public License version 2 only, as attila@175: * published by the Free Software Foundation. attila@962: * attila@175: * This code is distributed in the hope that it will be useful, but WITHOUT attila@175: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or attila@175: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License attila@175: * version 2 for more details (a copy is included in the LICENSE file that attila@175: * accompanied this code). attila@962: * attila@175: * You should have received a copy of the GNU General Public License version attila@175: * 2 along with this work; if not, write to the Free Software Foundation, attila@175: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. attila@962: * attila@175: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA attila@175: * or visit www.oracle.com if you need additional information or have any attila@175: * questions. attila@175: */ attila@175: attila@175: /** attila@175: * Check behavior of class-level overrides. attila@175: * attila@175: * @test attila@175: * @run attila@175: */ attila@175: attila@175: attila@175: // Make two classes with class overrides attila@175: attila@175: var R1 = Java.extend(java.lang.Runnable, { attila@175: run: function() { attila@175: print("R1.run() invoked") attila@175: } attila@175: }) attila@175: attila@175: var R2 = Java.extend(java.lang.Runnable, { attila@175: run: function() { attila@175: print("R2.run() invoked") attila@175: } attila@175: }) attila@175: attila@175: var r1 = new R1 attila@175: var r2 = new R2 attila@175: // Create one with an instance-override too attila@719: var R3 = Java.extend(R2) attila@719: var r3 = new R3({ run: function() { print("r3.run() invoked") }}) attila@175: attila@175: // Run 'em - we're passing them through a Thread to make sure they indeed attila@175: // are full-blown Runnables attila@175: function runInThread(r) { attila@175: var t = new java.lang.Thread(r) attila@175: t.start() attila@175: t.join() attila@175: } attila@175: runInThread(r1) attila@175: runInThread(r2) attila@175: runInThread(r3) attila@175: attila@175: // Two class-override classes differ attila@719: print("r1.class !== r2.class: " + (r1.class !== r2.class)) attila@719: // instance-override class also differs attila@719: print("r2.class !== r3.class: " + (r2.class !== r3.class)) attila@175: attila@175: function checkAbstract(r) { attila@175: try { attila@175: r.run() attila@175: print("Expected to fail!") attila@175: } catch(e) { attila@175: print("Got exception: " + e) attila@175: } attila@175: } attila@175: attila@175: // Check we're hitting UnsupportedOperationException if neither class attila@175: // overrides nor instance overrides are present attila@175: var RAbstract = Java.extend(java.lang.Runnable, {}) attila@175: checkAbstract(new RAbstract()) // class override (empty) attila@719: checkAbstract(new (Java.extend(RAbstract))() {}) // class+instance override (empty) attila@175: attila@175: // Check we delegate to superclass if neither class attila@175: // overrides nor instance overrides are present attila@175: var ExtendsList = Java.extend(java.util.ArrayList, {}) attila@175: print("(new ExtendsList).size() = " + (new ExtendsList).size()) attila@719: print("(new (Java.extend(ExtendsList)){}).size() = " + (new (Java.extend(ExtendsList)){}).size())