Bridge API to connect with existing Java APIs.
JavaScript only supports 32-bit integers. Because of this java longs must be treated specially.
When getting a long result the value may be truncated. If you need the original value there is
a property off of the result called "longValue" which contains the un-truncated value as a string.
If you are calling a method that takes a long you must create it using java.newInstance.
Exceptions from calling methods either caught using JavaScript try/catch block or passed
to a callback as the first parameter may have a property named "cause" which has a reference
to the Java Exception object which caused the error.
**java.newInstance(className, [args...], callback)**
java.newInstanceSync(className, [args...]) : result
Creates an instance of the specified class. If you are using the sync method an exception will be throw if an error occures,
otherwise it will be the first argument in the callback.
Arguments
- className - The name of the class to create. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
- callback(err, item) - Callback to be called when the class is created.
Example
var list = java.newInstanceSync("java.util.ArrayList");
java.newInstance("java.util.ArrayList", function(err, list) {
if(err) { console.error(err); return; }
// new list
});
**java.callStaticMethod(className, methodName, [args...], callback)**
java.callStaticMethodSync(className, methodName, [args...]) : result
Calls a static method on the specified class. If you are using the sync method an exception will be throw if an error occures,
otherwise it will be the first argument in the callback.
Arguments
- className - The name of the class to call the method on. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
- methodName - The name of the method to call.
- callback(err, item) - Callback to be called when the class is created.
Example
var result = java.callStaticMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
java.callStaticMethod("com.nearinfinty.MyClass", "doSomething", 42, "test", function(err, results) {
if(err) { console.error(err); return; }
// results from doSomething
});
**java.newProxy(interfaceName, functions)**
Creates a new java Proxy for the given interface. Functions passed in will run on the v8 main thread and not a new thread.
The returned object has two methods ref() and unref() which you can use to maintain references to prevent premature
garbage collection. You must call these methods to ensure the proxy stays around.
Arguments
- interfaceName - The name of the interface to proxy. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
- functions - A hash of functions matching the function in the interface.
Example
var myProxy = java.newProxy('java.lang.Runnable', {
run: function () {
// This is actually run on the v8 thread and not the new java thread
console.log("hello from thread");
}
});
var thread = java.newInstanceSync("java.lang.Thread", myProxy);
thread.start();
**obj._methodName_([args...], callback)**
obj.methodNameSync([args...]) : result
Once you have a java object either by creating a new instance or as a result of a method call you can then call methods on that object.
All public, non-static methods are exposed in synchronous and asynchronous flavors.
Arguments
- args - The arguments to pass to the method.
- callback(err, item) - Callback to be called when the method has completed.
Example
var list = java.newInstanceSync("java.util.ArrayList");
list.addSync("item1");
list.add("item2", function(err, result) {
if(err) { console.error(err); return; }
});
**obj._fieldName_ = val**
val = obj.fieldName
Once you have a java object either by creating a new instance or as a result of a method call you can get instance
field values.
Example
var list = java.newInstanceSync("com.nearinfinty.MyClass");
list.data = "test";
var data = list.data;
License
(The MIT License)
Copyright (c) 2012 Near Infinity Corporation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.