|
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.internal.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 two classes |
|
55 try { |
|
56 Java.extend(java.lang.Thread,java.lang.Number) |
|
57 } catch(e) { |
|
58 print(e) |
|
59 } |
|
60 |
|
61 // Make sure we can implement interfaces from the unnamed package |
|
62 var c = new (Java.extend(Java.type("UnnamedPackageTestCallback")))() { call: function(s) { return s + s } } |
|
63 print(c.call("abcd")) |
|
64 |
|
65 // Basic Runnable from an object |
|
66 new (Java.extend(java.lang.Runnable))({ run: function() { print("run-object") } }).run() |
|
67 |
|
68 // Basic Runnable from a function |
|
69 new (Java.extend(java.lang.Runnable))(function() { print("run-fn") }).run() |
|
70 |
|
71 // Basic Runnable from an autoconverted function |
|
72 var t = new java.lang.Thread(function() { print("run-fn-autoconvert") }) |
|
73 t.start() |
|
74 t.join() |
|
75 |
|
76 // SAM conversion should work on overloaded methods of same name |
|
77 var os = new (Java.extend(model("OverloadedSam")))(function(s1, s2) { print("overloaded-sam: " + s1 + ", " + s2) }) |
|
78 os.sam("x") |
|
79 os.sam("x", "y") |
|
80 |
|
81 // Test overriding of hashCode, equals, and toString |
|
82 var oo = Java.extend(model("OverrideObject")) |
|
83 // First, see non-overridden values |
|
84 print("oo-plain-hashCode: " + (new oo({})).hashCode()) |
|
85 print("oo-plain-toString: " + (new oo({})).toString()) |
|
86 print("oo-plain-equals : " + (new oo({})).equals({})) |
|
87 // Now, override them |
|
88 print("oo-overridden-hashCode: " + (new oo({ hashCode: function() { return 6 }})).hashCode()) |
|
89 print("oo-overridden-toString: " + (new oo({ toString: function() { return "override-object-overriden" }})).toString()) |
|
90 print("oo-overridden-equals : " + (new oo({ equals: function() { return true }})).equals({})) |
|
91 // Finally, test that equals and hashCode can be overridden with functions from a prototype, but toString() can't: |
|
92 function Proto() { |
|
93 return this; |
|
94 } |
|
95 Proto.prototype = { |
|
96 toString: function() { return "this-will-never-be-seen" }, // toString only overridden when it's own property, never from prototype |
|
97 equals: function() { return true }, |
|
98 hashCode: function() { return 7 } |
|
99 } |
|
100 print("oo-proto-overridden-hashCode: " + (new oo(new Proto())).hashCode()) |
|
101 print("oo-proto-overridden-toString: " + (new oo(new Proto())).toString()) |
|
102 print("oo-proto-overridden-equals : " + (new oo(new Proto())).equals({})) |
|
103 |
|
104 // Subclass a class with a protected constructor, and one that takes |
|
105 // additional constructor arguments (a token). Also demonstrates how can |
|
106 // you access the Java adapter instance from the script (just store it in the |
|
107 // scope, in this example, "cwa") to retrieve the token later on. |
|
108 var cwa = new (Java.extend(model("ConstructorWithArgument")))("cwa-token", function() { print(cwa.token) }) |
|
109 cwa.doSomething() |
|
110 |
|
111 // Do the same thing with proprietary syntax and object literal |
|
112 var cwa2 = new (model("ConstructorWithArgument"))("cwa2-token") { doSomething: function() { print("cwa2-" + cwa2.token ) } } |
|
113 cwa2.doSomething() |
|
114 |
|
115 // Implement two interfaces |
|
116 var desertToppingAndFloorWax = new (Java.extend(model("DessertTopping"), model("FloorWax"))) { |
|
117 pourOnDessert: function() { print("Glop; IM IN UR DESSERT NOW") }, |
|
118 shineUpTheFloor: function() { print("The floor sure is shining!") } |
|
119 } |
|
120 var dtfwDriver = new (model("DessertToppingFloorWaxDriver")) |
|
121 dtfwDriver.decorateDessert(desertToppingAndFloorWax) |
|
122 dtfwDriver.waxFloor(desertToppingAndFloorWax) |
|
123 |
|
124 // Extend a class and implement two interfaces. For additional measure, put the class in between the two interfaces |
|
125 var desertToppingFloorWaxAndToothpaste = new (Java.extend(model("DessertTopping"), model("Toothpaste"), model("FloorWax"))) { |
|
126 pourOnDessert: function() { print("Yum") }, |
|
127 shineUpTheFloor: function() { print("Scrub, scrub, scrub") }, |
|
128 applyToBrushImpl: function() { print("It's a dessert topping! It's a floor wax! It's a toothpaste!") } |
|
129 } |
|
130 dtfwDriver.decorateDessert(desertToppingFloorWaxAndToothpaste) |
|
131 dtfwDriver.waxFloor(desertToppingFloorWaxAndToothpaste) |
|
132 desertToppingFloorWaxAndToothpaste.applyToBrush(); |