jlaskey@3: /* jlaskey@7: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. jlaskey@3: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. attila@962: * jlaskey@3: * This code is free software; you can redistribute it and/or modify it jlaskey@3: * under the terms of the GNU General Public License version 2 only, as jlaskey@3: * published by the Free Software Foundation. attila@962: * jlaskey@3: * This code is distributed in the hope that it will be useful, but WITHOUT jlaskey@3: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jlaskey@3: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jlaskey@3: * version 2 for more details (a copy is included in the LICENSE file that jlaskey@3: * accompanied this code). attila@962: * jlaskey@3: * You should have received a copy of the GNU General Public License version jlaskey@3: * 2 along with this work; if not, write to the Free Software Foundation, jlaskey@3: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. attila@962: * jlaskey@3: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jlaskey@3: * or visit www.oracle.com if you need additional information or have any jlaskey@3: * questions. jlaskey@3: */ jlaskey@3: jlaskey@3: /** jlaskey@3: * @test jlaskey@3: * @run jlaskey@3: */ jlaskey@3: jlaskey@3: function model(n) { sundar@136: return Java.type("jdk.nashorn.test.models." + n) jlaskey@3: } jlaskey@3: attila@962: // Can't extend a final class jlaskey@3: try { jlaskey@3: Java.extend(model("FinalClass")) jlaskey@3: } catch(e) { jlaskey@3: print(e) jlaskey@3: } jlaskey@3: jlaskey@3: // Can't extend a class with no public or protected constructor jlaskey@3: try { jlaskey@3: Java.extend(model("NoAccessibleConstructorClass")) jlaskey@3: } catch(e) { jlaskey@3: print(e) jlaskey@3: } jlaskey@3: jlaskey@3: // Can't extend a non-public class jlaskey@3: try { jlaskey@3: Java.extend(model("NonPublicClass")) jlaskey@3: } catch(e) { jlaskey@3: print(e) jlaskey@3: } jlaskey@3: attila@719: // Can't extend a class with explicit non-overridable finalizer attila@719: try { attila@719: Java.extend(model("ClassWithFinalFinalizer")) attila@719: } catch(e) { attila@719: print(e) attila@719: } attila@719: attila@719: // Can't extend a class with inherited non-overridable finalizer attila@719: try { attila@719: Java.extend(model("ClassWithInheritedFinalFinalizer")) attila@719: } catch(e) { attila@719: print(e) attila@719: } attila@719: attila@719: attila@27: // Can't extend two classes attila@27: try { attila@27: Java.extend(java.lang.Thread,java.lang.Number) attila@27: } catch(e) { attila@27: print(e) attila@27: } attila@27: jlaskey@3: // Make sure we can implement interfaces from the unnamed package jlaskey@3: var c = new (Java.extend(Java.type("UnnamedPackageTestCallback")))() { call: function(s) { return s + s } } jlaskey@3: print(c.call("abcd")) jlaskey@3: jlaskey@3: // Basic Runnable from an object jlaskey@3: new (Java.extend(java.lang.Runnable))({ run: function() { print("run-object") } }).run() jlaskey@3: jlaskey@3: // Basic Runnable from a function jlaskey@3: new (Java.extend(java.lang.Runnable))(function() { print("run-fn") }).run() jlaskey@3: jlaskey@3: // Basic Runnable from an autoconverted function jlaskey@3: var t = new java.lang.Thread(function() { print("run-fn-autoconvert") }) jlaskey@3: t.start() jlaskey@3: t.join() jlaskey@3: jlaskey@3: // SAM conversion should work on overloaded methods of same name jlaskey@3: var os = new (Java.extend(model("OverloadedSam")))(function(s1, s2) { print("overloaded-sam: " + s1 + ", " + s2) }) jlaskey@3: os.sam("x") jlaskey@3: os.sam("x", "y") jlaskey@3: jlaskey@3: // Test overriding of hashCode, equals, and toString jlaskey@3: var oo = Java.extend(model("OverrideObject")) jlaskey@3: // First, see non-overridden values jlaskey@3: print("oo-plain-hashCode: " + (new oo({})).hashCode()) jlaskey@3: print("oo-plain-toString: " + (new oo({})).toString()) jlaskey@3: print("oo-plain-equals : " + (new oo({})).equals({})) jlaskey@3: // Now, override them jlaskey@3: print("oo-overridden-hashCode: " + (new oo({ hashCode: function() { return 6 }})).hashCode()) jlaskey@3: print("oo-overridden-toString: " + (new oo({ toString: function() { return "override-object-overriden" }})).toString()) jlaskey@3: print("oo-overridden-equals : " + (new oo({ equals: function() { return true }})).equals({})) jlaskey@3: // Finally, test that equals and hashCode can be overridden with functions from a prototype, but toString() can't: jlaskey@3: function Proto() { jlaskey@3: return this; jlaskey@3: } jlaskey@3: Proto.prototype = { jlaskey@3: toString: function() { return "this-will-never-be-seen" }, // toString only overridden when it's own property, never from prototype jlaskey@3: equals: function() { return true }, jlaskey@3: hashCode: function() { return 7 } jlaskey@3: } jlaskey@3: print("oo-proto-overridden-hashCode: " + (new oo(new Proto())).hashCode()) jlaskey@3: print("oo-proto-overridden-toString: " + (new oo(new Proto())).toString()) jlaskey@3: print("oo-proto-overridden-equals : " + (new oo(new Proto())).equals({})) jlaskey@3: jlaskey@3: // Subclass a class with a protected constructor, and one that takes jlaskey@3: // additional constructor arguments (a token). Also demonstrates how can jlaskey@3: // you access the Java adapter instance from the script (just store it in the jlaskey@3: // scope, in this example, "cwa") to retrieve the token later on. attila@23: var cwa = new (Java.extend(model("ConstructorWithArgument")))("cwa-token", function() { print(cwa.token) }) jlaskey@3: cwa.doSomething() attila@23: attila@23: // Do the same thing with proprietary syntax and object literal attila@23: var cwa2 = new (model("ConstructorWithArgument"))("cwa2-token") { doSomething: function() { print("cwa2-" + cwa2.token ) } } attila@23: cwa2.doSomething() attila@27: attila@27: // Implement two interfaces attila@27: var desertToppingAndFloorWax = new (Java.extend(model("DessertTopping"), model("FloorWax"))) { attila@27: pourOnDessert: function() { print("Glop; IM IN UR DESSERT NOW") }, attila@27: shineUpTheFloor: function() { print("The floor sure is shining!") } attila@27: } attila@27: var dtfwDriver = new (model("DessertToppingFloorWaxDriver")) attila@27: dtfwDriver.decorateDessert(desertToppingAndFloorWax) attila@27: dtfwDriver.waxFloor(desertToppingAndFloorWax) attila@27: attila@27: // Extend a class and implement two interfaces. For additional measure, put the class in between the two interfaces attila@27: var desertToppingFloorWaxAndToothpaste = new (Java.extend(model("DessertTopping"), model("Toothpaste"), model("FloorWax"))) { attila@27: pourOnDessert: function() { print("Yum") }, attila@27: shineUpTheFloor: function() { print("Scrub, scrub, scrub") }, attila@27: applyToBrushImpl: function() { print("It's a dessert topping! It's a floor wax! It's a toothpaste!") } attila@27: } attila@27: dtfwDriver.decorateDessert(desertToppingFloorWaxAndToothpaste) attila@27: dtfwDriver.waxFloor(desertToppingFloorWaxAndToothpaste) attila@27: desertToppingFloorWaxAndToothpaste.applyToBrush();