Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Packages, java, javax, javafx, com, edu, org

Packages and friends related objects are there to support java package, class access from script. The properties of the Packages variable are all the top-level Java packages, such as java and javax etc.

...

Code Block
titleJava.super example
var CharArray = Java.type("char[]")
var jString = Java.type("java.lang.String")
var Character = Java.type("java.lang.Character")

function capitalize(s) {
    if(s instanceof CharArray) {
        return new jString(s).toUpperCase()
    }
    if(s instanceof jString) {
        return s.toUpperCase()
    }
    return Character.toUpperCase(s) // must be int
}

var sw = new (Java.type("java.io.StringWriter"))

var FilterWriterAdapter = Java.extend(Java.type("java.io.FilterWriter"))

var cw = new FilterWriterAdapter(sw) {
    write: function(s, off, len) {
        s = capitalize(s)
        // Must handle overloads by arity
        if(off === undefined) {
            cw_super.write(s, 0, s.length())
        } else if (typeof s === "string") {
            cw_super.write(s, off, len)
        }
    }
}
var cw_super = Java.super(cw)

cw.write("abcd")
cw.write("e".charAt(0))
cw.write("fgh".toCharArray())
cw.write("**ijk**", 2, 3)
cw.write("***lmno**".toCharArray(), 3, 4)
cw.flush()
print(sw)

JSAdapter constructor

Lambdas, SAM types and Script functions

Every function is a lambda and a SAM type object

Where ever a JDK8 lambda or SAM  (single-abstract-method) type is required, an ECMAScript function can be passed as argument. Nashorn will auto-convert a script function to a lambda object or any SAM interface implementing objectJSAdapter supports java.lang.reflect.Proxy-like proxy mechanism for script objects.

Code Block
titleJSAdapter lambda, SAM example
//var IntStream = Java.type("java.util.stream.IntStream");

// IntStream's forEach requires a lambda implementing
// java.util.function.IntConsumer. You can pass a script function!

IntStream.range(0, 100).forEach(
    function(x) { 
        print(x*x) 
    });

var Thread = Java.type("java.lang.Thread");

// java.lang.Thread constructor requires a java.lang.Runnable
// You can pass script function wherever a SAM is expected
new Thread(function() {
    print("I am a thread!");
}).start();

Every lambda is a script function


Any Java object that is an instance of lambda type can be treated like a script function.

 

Code Block
titlelambda as function example
var Function = Java.type('java.util.function.Function')

var obj = new Function() { 
   apply: function(x) { print(x*x) } 
}

// every lambda object is a 'function'

print(typeof obj); // prints "function"

// 'calls' lambda as though it is a function
obj(91);

 

JSAdapter constructor

JSAdapter supports java.lang.reflect.Proxy-like proxy mechanism for script objects.

Code Block
titleJSAdapter example
// A A JSAdapter object with proxy-like special hooks
var obj = new JSAdapter() {
    __get__: function(name) {
        print("getter called for '" + name + "'"); return name;
    },

    __put__: function(name, value) {
        print("setter called for '" + name + "' with " + value);
    },

    __call__: function(name, arg1, arg2) {
        print("method '" + name + "' called with " + arg1 + ", " + arg2);
    },

    __new__: function(arg1, arg2) {
        print("new with " + arg1 + ", " + arg2);
    },

    __getIds__: function() {
        print("__getIds__ called");
        return [ "foo", "bar" ];
    },

    __getValues__: function() {
        print("__getValues__ called");
        return [ "fooval", "barval" ];
    },

    __has__: function(name) {ECMAscript typed arrays
        print("__has__ called with '" + name + "'");
        return name == "js";
    },

    __delete__: function(name) {
        print("__delete__ called with '" + name + "'");
        return true;
    }
};

// calls __get__
print(obj.foo);

// calls __put__
obj.foo = 33;

// calls __call__
obj.func("hello", "world");

// calls __new__
new obj("hey!", "it works!");

for (i in obj) {
    print(i);
}

for each (i in obj) {
    print(i);
}

...