Skip to content

Commit d9c25b0

Browse files
committed
Promisify java class static methods on import.
1 parent 63be4ce commit d9c25b0

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/nodeJavaBridge.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ java.import = function(name) {
4242
}
4343
}
4444

45+
var promisify = undefined;
46+
var promiseSuffix;
47+
if (java.asyncOptions && java.asyncOptions.promisify) {
48+
promisify = java.asyncOptions.promisify;
49+
promiseSuffix = java.asyncOptions.suffix;
50+
}
51+
4552
// copy static methods
4653
var methods = clazz.getDeclaredMethodsSync();
4754
for (i = 0; i < methods.length; i++) {
@@ -50,6 +57,9 @@ java.import = function(name) {
5057
var methodName = methods[i].getNameSync();
5158
result[methodName + 'Sync'] = java.callStaticMethodSync.bind(java, name, methodName);
5259
result[methodName] = java.callStaticMethod.bind(java, name, methodName);
60+
if (promisify) {
61+
result[methodName + promiseSuffix] = promisify(java.callStaticMethod.bind(java, name, methodName));
62+
}
5363
}
5464
}
5565

test/promises-test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ var java = require("../testHelpers").java;
33
var nodeunit = require("nodeunit");
44
var util = require("util");
55

6+
67
exports['Promises'] = nodeunit.testCase({
8+
79
"create an instance of a class and call methods (getClassPromise & getNamePromise)": function(test) {
10+
// Adapted from a test in simple-test.js
811
java.newInstance("java.util.ArrayList", function(err, list) {
912
if (err) {
1013
console.log(err);
@@ -23,9 +26,24 @@ exports['Promises'] = nodeunit.testCase({
2326
.then(function() {
2427
test.expect(2);
2528
test.done();
26-
})
29+
});
2730
}
2831
});
2932
},
33+
34+
"import and execute promisified static method": function (test) {
35+
var Test = java.import('Test');
36+
Test.staticMethodPromise(99)
37+
.then(function (result) {
38+
test.equals(100, result);
39+
})
40+
.catch(function (err) {
41+
test.ifError(err);
42+
})
43+
.then(function() {
44+
test.expect(1);
45+
test.done();
46+
});
47+
}
3048
});
3149

0 commit comments

Comments
 (0)