Skip to content

Commit aba16bd

Browse files
committed
add classes and enums to java.import resulting object
1 parent 1cf6f3a commit aba16bd

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

lib/nodeJavaBridge.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,29 @@ java.import = function(name) {
5151
}
5252
}
5353

54+
// copy static classes/enums
55+
var classes = clazz.getDeclaredClassesSync();
56+
for (i = 0; i < classes.length; i++) {
57+
if (((classes[i].getModifiersSync() & MODIFIER_PUBLIC) === MODIFIER_PUBLIC)
58+
&& ((classes[i].getModifiersSync() & MODIFIER_STATIC) === MODIFIER_STATIC)) {
59+
var className = classes[i].getNameSync();
60+
var simpleName = classes[i].getSimpleNameSync();
61+
Object.defineProperty(result, simpleName, {
62+
get: function(result, simpleName, className) {
63+
var c = java.import(className);
64+
65+
// memoize the import
66+
var d = Object.getOwnPropertyDescriptor(result, simpleName);
67+
d.get = function(c) { return c; }.bind(null, c);
68+
Object.defineProperty(result, simpleName, d);
69+
70+
return c;
71+
}.bind(this, result, simpleName, className),
72+
enumerable: true,
73+
configurable: true
74+
});
75+
}
76+
}
77+
5478
return result;
5579
};

test/Test$StaticEnum.class

872 Bytes
Binary file not shown.

test/Test.class

151 Bytes
Binary file not shown.

test/Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,10 @@ public static long[] getArrayOfLongs() {
127127
arr[4] = 5;
128128
return arr;
129129
}
130+
131+
public static enum StaticEnum {
132+
Value1,
133+
Value2
134+
}
135+
public static String staticEnumToString(StaticEnum e) { return e.toString(); }
130136
}

test/java-callStaticMethod-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,14 @@ exports['Java - Call Static Method'] = nodeunit.testCase({
191191
test.equal(result.longValue, "9223372036854775807");
192192
test.done();
193193
});
194+
},
195+
196+
"Call method with nested enum value": function(test) {
197+
var Test = java.import("Test");
198+
Test.staticEnumToStringSync(Test.StaticEnum.Value1);
199+
var str = Test.staticEnumToStringSync(Test.StaticEnum.Value1); // call it twice to ensure memo-ize is working
200+
test.equal(str, "Value1");
201+
test.done();
194202
}
203+
195204
});

0 commit comments

Comments
 (0)