1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @run 27 */ 28 29 function model(n) { 30 return Java.type("jdk.nashorn.test.models." + n) 31 } 32 33 // Can't extend a final class 34 try { 35 Java.extend(model("FinalClass")) 36 } catch(e) { 37 print(e) 38 } 39 40 // Can't extend a class with no public or protected constructor 41 try { 42 Java.extend(model("NoAccessibleConstructorClass")) 43 } catch(e) { 44 print(e) 45 } 46 47 // Can't extend a non-public class 48 try { 49 Java.extend(model("NonPublicClass")) 50 } catch(e) { 51 print(e) 52 } 53 54 // Can't extend a class with explicit non-overridable finalizer 55 try { 56 Java.extend(model("ClassWithFinalFinalizer")) 57 } catch(e) { 58 print(e) 59 } 60 61 // Can't extend a class with inherited non-overridable finalizer 62 try { 63 Java.extend(model("ClassWithInheritedFinalFinalizer")) 64 } catch(e) { 65 print(e) 66 } 67 68 69 // Can't extend two classes 70 try { 71 Java.extend(java.lang.Thread,java.lang.Number) 72 } catch(e) { 73 print(e) 74 } 75 76 // Make sure we can implement interfaces from the unnamed package 77 var c = new (Java.extend(Java.type("UnnamedPackageTestCallback")))() { call: function(s) { return s + s } } 78 print(c.call("abcd")) 79 80 // Basic Runnable from an object 81 new (Java.extend(java.lang.Runnable))({ run: function() { print("run-object") } }).run() 82 83 // Basic Runnable from a function 84 new (Java.extend(java.lang.Runnable))(function() { print("run-fn") }).run() 85 86 // Basic Runnable from an autoconverted function 87 var t = new java.lang.Thread(function() { print("run-fn-autoconvert") }) 88 t.start() 89 t.join() 90 91 // SAM conversion should work on overloaded methods of same name 92 var os = new (Java.extend(model("OverloadedSam")))(function(s1, s2) { print("overloaded-sam: " + s1 + ", " + s2) }) 93 os.sam("x") 94 os.sam("x", "y") 95 96 // Test overriding of hashCode, equals, and toString 97 var oo = Java.extend(model("OverrideObject")) 98 // First, see non-overridden values 99 print("oo-plain-hashCode: " + (new oo({})).hashCode()) 100 print("oo-plain-toString: " + (new oo({})).toString()) 101 print("oo-plain-equals : " + (new oo({})).equals({})) 102 // Now, override them 103 print("oo-overridden-hashCode: " + (new oo({ hashCode: function() { return 6 }})).hashCode()) 104 print("oo-overridden-toString: " + (new oo({ toString: function() { return "override-object-overriden" }})).toString()) 105 print("oo-overridden-equals : " + (new oo({ equals: function() { return true }})).equals({})) 106 // Finally, test that equals and hashCode can be overridden with functions from a prototype, but toString() can't: 107 function Proto() { 108 return this; 109 } 110 Proto.prototype = { 111 toString: function() { return "this-will-never-be-seen" }, // toString only overridden when it's own property, never from prototype 112 equals: function() { return true }, 113 hashCode: function() { return 7 } 114 } 115 print("oo-proto-overridden-hashCode: " + (new oo(new Proto())).hashCode()) 116 print("oo-proto-overridden-toString: " + (new oo(new Proto())).toString()) 117 print("oo-proto-overridden-equals : " + (new oo(new Proto())).equals({})) 118 119 // Subclass a class with a protected constructor, and one that takes 120 // additional constructor arguments (a token). Also demonstrates how can 121 // you access the Java adapter instance from the script (just store it in the 122 // scope, in this example, "cwa") to retrieve the token later on. 123 var cwa = new (Java.extend(model("ConstructorWithArgument")))("cwa-token", function() { print(cwa.token) }) 124 cwa.doSomething() 125 126 // Do the same thing with proprietary syntax and object literal 127 var cwa2 = new (model("ConstructorWithArgument"))("cwa2-token") { doSomething: function() { print("cwa2-" + cwa2.token ) } } 128 cwa2.doSomething() 129 130 // Implement two interfaces 131 var desertToppingAndFloorWax = new (Java.extend(model("DessertTopping"), model("FloorWax"))) { 132 pourOnDessert: function() { print("Glop; IM IN UR DESSERT NOW") }, 133 shineUpTheFloor: function() { print("The floor sure is shining!") } 134 } 135 var dtfwDriver = new (model("DessertToppingFloorWaxDriver")) 136 dtfwDriver.decorateDessert(desertToppingAndFloorWax) 137 dtfwDriver.waxFloor(desertToppingAndFloorWax) 138 139 // Extend a class and implement two interfaces. For additional measure, put the class in between the two interfaces 140 var desertToppingFloorWaxAndToothpaste = new (Java.extend(model("DessertTopping"), model("Toothpaste"), model("FloorWax"))) { 141 pourOnDessert: function() { print("Yum") }, 142 shineUpTheFloor: function() { print("Scrub, scrub, scrub") }, 143 applyToBrushImpl: function() { print("It's a dessert topping! It's a floor wax! It's a toothpaste!") } 144 } 145 dtfwDriver.decorateDessert(desertToppingFloorWaxAndToothpaste) 146 dtfwDriver.waxFloor(desertToppingFloorWaxAndToothpaste) 147 desertToppingFloorWaxAndToothpaste.applyToBrush();