Debian Codegraph @

rhino/testsrc/doctests/javaadapter.doctest

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 js> var x = new JavaAdapter(java.util.HashMap, {})
6 js> x
7 {}
8 js> x instanceof java.util.HashMap
9 true
10 js> x instanceof java.util.Map
11 true
12 js> x.put("foo", "bar")
13 null
14 js> x
15 {foo=bar}
16 js> x.get("foo")
17 bar
18 js> x = new JavaAdapter(java.util.HashMap, {}, {a: "b"})
19 {a=b}
20 js> x.get("a")
21 b
22 js> x.remove("a")
23 b
24 js> x
25 {}
26 js> x.get("a")
27 null
28 js> x = new JavaAdapter(java.util.HashMap, {get: function(key) {return 2 + this.super$get(key)}})
29 {}
30 js> x.put("foo", "bar")
31 null
32 js> x.get("foo")
33 2bar
34 js> x.get("bar")
35 2.0
36 js> x = new JavaAdapter({}); x.class.superclass
37 class java.lang.Object
38 js> x.class.interfaces.length
39 0
40 js> var Appendable = java.lang.Appendable
41 js> var Runnable = java.lang.Runnable
42 js> function append() {return this}
43 js> function run() {return "done"}
44 js> var x = new JavaAdapter(Runnable, Appendable, {run: run, append: append})
45 js> x instanceof Appendable
46 true
47 js> x instanceof Runnable
48 true
49 js> x.append("foo") === x
50 true
51 js> x.run()
52 js> // test access to protected methods
53 js> x = JavaAdapter(java.util.Hashtable, {test: function() {this.rehash();}});
54 {}
55 js> x.test()
56 js> // test access to protected fields
57 js> x = JavaAdapter(java.util.Vector, {test: function() {return this.elementCount;}});
58 []
59 js> x.test()
60 0
61 js> x.add(1)
62 true
63 js> x.test()
64 1
65 js> // test non-empty constructor with protected field
66 js> x = JavaAdapter(java.util.Vector, {test: function() {return this.elementData.length;}}, 20);
67 []
68 js> x.test()
69 20
70 js> // test extending abstract base class
71 js> x = new java.util.AbstractList({get: function(i) { return i + 1; }})
72 []
73 js> x.get(0)
74 1.0
75 js> x.get(1)
76 2.0
77 js> x.get(2)
78 3.0
79 js> // test implementing an interface
80 js> x = new java.util.List({get: function(i) { return i + 1; }, toString: function() { return "[]"; }})
81 []
82 js> x.get(0)
83 1.0
84 js> x.get(1)
85 2.0
86 js> x.get(2)
87 3.0