...
| Code Block |
|---|
| title | new anonymous class-like syntax |
|---|
|
// This syntax is primarily used to support anonymous class-like syntax for
// Java interface implementation as shown below.
var r = new java.lang.Runnable() {
run: function() { print("run"); }
} |
Anonymous function statements
...
| Code Block |
|---|
| title | multi-line string literals |
|---|
|
var str = <<EOF
This is a string that contains multiple lines
hello
world
That's all!
EOF
print(str) |
...
String interpolation (-scripting mode only)
...
| Code Block |
|---|
|
// JavaImporter constructor accepts one or more Java Package objects
var imports = new JavaImporter(java.util, java.io);
// a JavaImporter can be used as a "with" expression object
with(imports) {
// classes from java.util and java.io packages can
// can be accessed by unqualified names
var map = new HashMap(); // refers to java.util.HashMap
map.put("js", "javascript");
map.put("java", "java");
map.put("cpp", "c++");
print(map);
var f = new File("."); // refers to java.io.File
print(f.getAbsolutePath());
} |
| Code Block |
|---|
| title | JavaImporter in 'with' statement example |
|---|
|
// read text content from the given URL
function readText(url) {
// Using JavaImporter to resolve classes
// from specified java packages within the
// 'with' statement below
with (new JavaImporter(java.io, java.net)) {
// more or less regular java code except for static types
var is = new URL(url).openStream();
try {
var reader = new BufferedReader(
new InputStreamReader(is));
var buf = '', line = null;
while ((line = reader.readLine()) != null) {
buf += line;
}
} finally {
reader.close();
}
return buf;
}
}
print(readText("http://google.com?q=jdk8")) |
Java object
"Java" global property is a script object that defines useful functions for script-to-Java interface.
...