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