rhino1.7.6.testsrc.doctests.javaadapter.doctest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rhino Show documentation
Show all versions of rhino Show documentation
Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically
embedded into Java applications to provide scripting to end users.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
js> var x = new JavaAdapter(java.util.HashMap, {})
js> x
{}
js> x instanceof java.util.HashMap
true
js> x instanceof java.util.Map
true
js> x.put("foo", "bar")
null
js> x
{foo=bar}
js> x.get("foo")
bar
js> x = new JavaAdapter(java.util.HashMap, {}, {a: "b"})
{a=b}
js> x.get("a")
b
js> x.remove("a")
b
js> x
{}
js> x.get("a")
null
js> x = new JavaAdapter(java.util.HashMap, {get: function(key) {return 2 + this.super$get(key)}})
{}
js> x.put("foo", "bar")
null
js> x.get("foo")
2bar
js> x.get("bar")
2.0
js> x = new JavaAdapter({}); x.class.superclass
class java.lang.Object
js> x.class.interfaces.length
0
js> var Appendable = java.lang.Appendable
js> var Runnable = java.lang.Runnable
js> function append() {return this}
js> function run() {return "done"}
js> var x = new JavaAdapter(Runnable, Appendable, {run: run, append: append})
js> x instanceof Appendable
true
js> x instanceof Runnable
true
js> x.append("foo") === x
true
js> x.run()
js> // test access to protected methods
js> x = JavaAdapter(java.util.Hashtable, {test: function() {this.rehash();}});
{}
js> x.test()
js> // test access to protected fields
js> x = JavaAdapter(java.util.Vector, {test: function() {return this.elementCount;}});
[]
js> x.test()
0
js> x.add(1)
true
js> x.test()
1
js> // test non-empty constructor with protected field
js> x = JavaAdapter(java.util.Vector, {test: function() {return this.elementData.length;}}, 20);
[]
js> x.test()
20
js> // test extending abstract base class
js> x = new java.util.AbstractList({get: function(i) { return i + 1; }})
[]
js> x.get(0)
1.0
js> x.get(1)
2.0
js> x.get(2)
3.0
js> // test implementing an interface
js> x = new java.util.List({get: function(i) { return i + 1; }, toString: function() { return "[]"; }})
[]
js> x.get(0)
1.0
js> x.get(1)
2.0
js> x.get(2)
3.0
© 2015 - 2024 Weber Informatics LLC | Privacy Policy