author | attila |
Wed, 04 Jun 2014 13:08:57 +0200 | |
changeset 24778 | 2ff5d7041566 |
parent 22669 | 75563515567f |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
22669
diff
changeset
|
4 |
* |
16147 | 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. |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
22669
diff
changeset
|
8 |
* |
16147 | 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). |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
22669
diff
changeset
|
14 |
* |
16147 | 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. |
|
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
22669
diff
changeset
|
18 |
* |
16147 | 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) { |
|
16522 | 30 |
return Java.type("jdk.nashorn.test.models." + n) |
16147 | 31 |
} |
32 |
||
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
22669
diff
changeset
|
33 |
// Can't extend a final class |
16147 | 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 |
||
22669 | 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 |
||
16171
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
69 |
// Can't extend two classes |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
70 |
try { |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
71 |
Java.extend(java.lang.Thread,java.lang.Number) |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
72 |
} catch(e) { |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
73 |
print(e) |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
74 |
} |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
75 |
|
16147 | 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. |
|
16167
d99db3541813
8005983: JavaAdapterFactory generated proxy classes should take extra constructor arguments at the end
attila
parents:
16151
diff
changeset
|
123 |
var cwa = new (Java.extend(model("ConstructorWithArgument")))("cwa-token", function() { print(cwa.token) }) |
16147 | 124 |
cwa.doSomething() |
16167
d99db3541813
8005983: JavaAdapterFactory generated proxy classes should take extra constructor arguments at the end
attila
parents:
16151
diff
changeset
|
125 |
|
d99db3541813
8005983: JavaAdapterFactory generated proxy classes should take extra constructor arguments at the end
attila
parents:
16151
diff
changeset
|
126 |
// Do the same thing with proprietary syntax and object literal |
d99db3541813
8005983: JavaAdapterFactory generated proxy classes should take extra constructor arguments at the end
attila
parents:
16151
diff
changeset
|
127 |
var cwa2 = new (model("ConstructorWithArgument"))("cwa2-token") { doSomething: function() { print("cwa2-" + cwa2.token ) } } |
d99db3541813
8005983: JavaAdapterFactory generated proxy classes should take extra constructor arguments at the end
attila
parents:
16151
diff
changeset
|
128 |
cwa2.doSomething() |
16171
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
129 |
|
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
130 |
// Implement two interfaces |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
131 |
var desertToppingAndFloorWax = new (Java.extend(model("DessertTopping"), model("FloorWax"))) { |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
132 |
pourOnDessert: function() { print("Glop; IM IN UR DESSERT NOW") }, |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
133 |
shineUpTheFloor: function() { print("The floor sure is shining!") } |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
134 |
} |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
135 |
var dtfwDriver = new (model("DessertToppingFloorWaxDriver")) |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
136 |
dtfwDriver.decorateDessert(desertToppingAndFloorWax) |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
137 |
dtfwDriver.waxFloor(desertToppingAndFloorWax) |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
138 |
|
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
139 |
// Extend a class and implement two interfaces. For additional measure, put the class in between the two interfaces |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
140 |
var desertToppingFloorWaxAndToothpaste = new (Java.extend(model("DessertTopping"), model("Toothpaste"), model("FloorWax"))) { |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
141 |
pourOnDessert: function() { print("Yum") }, |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
142 |
shineUpTheFloor: function() { print("Scrub, scrub, scrub") }, |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
143 |
applyToBrushImpl: function() { print("It's a dessert topping! It's a floor wax! It's a toothpaste!") } |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
144 |
} |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
145 |
dtfwDriver.decorateDessert(desertToppingFloorWaxAndToothpaste) |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
146 |
dtfwDriver.waxFloor(desertToppingFloorWaxAndToothpaste) |
90dcd4fc42f0
8006168: ability to generate multi-type Java adapters
attila
parents:
16167
diff
changeset
|
147 |
desertToppingFloorWaxAndToothpaste.applyToBrush(); |