Skip to content

Commit 478a9d1

Browse files
committed
switch tests to using typescript and add type definitions
1 parent 7e3d64b commit 478a9d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2068
-741
lines changed

README.md

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Bridge API to connect with existing Java APIs.
99
### Other projects that might be helpful
1010

1111
* [node-java-maven](https://github.com/joeferner/node-java-maven) - manages your node-java classpath by using maven dependency management.
12+
* [ts-java](https://github.com/RedSeal-co/ts-java) - Create TypeScript declaration files for Java packages.
1213

1314
## Installation
1415

@@ -92,8 +93,8 @@ npm install --unsafe-perm java
9293
Then create a file called `test.js` with the following contents
9394

9495
```
95-
var java = require('java');
96-
var javaLangSystem = java.import('java.lang.System');
96+
const java = require('java');
97+
const javaLangSystem = java.import('java.lang.System');
9798
9899
javaLangSystem.out.printlnSync('Hello World');
99100
```
@@ -130,10 +131,10 @@ Then create the following module javaInit:
130131

131132
```javascript
132133
"use strict";
133-
var fs = require("fs");
134-
var java = require("java");
135-
var baseDir = "./target/dependency";
136-
var dependencies = fs.readdirSync(baseDir);
134+
const fs = require("fs");
135+
const java = require("java");
136+
const baseDir = "./target/dependency";
137+
const dependencies = fs.readdirSync(baseDir);
137138

138139
dependencies.forEach(function(dependency){
139140
java.classpath.push(baseDir + "/" + dependency);
@@ -151,8 +152,8 @@ and then in the consuming class write:
151152

152153
```javascript
153154

154-
var javaInit = require('./javaInit');
155-
var java = javaInit.getJavaInstance();
155+
const javaInit = require('./javaInit');
156+
const java = javaInit.getJavaInstance();
156157

157158
//your code goes here
158159
```
@@ -162,11 +163,11 @@ var java = javaInit.getJavaInstance();
162163
## Quick Examples
163164

164165
```javascript
165-
var java = require("java");
166+
const java = require("java");
166167
java.classpath.push("commons-lang3-3.1.jar");
167168
java.classpath.push("commons-io.jar");
168169

169-
var list1 = java.newInstanceSync("java.util.ArrayList");
170+
const list1 = java.newInstanceSync("java.util.ArrayList");
170171
console.log(list1.sizeSync()); // 0
171172
list1.addSync('item1');
172173
console.log(list1.sizeSync()); // 1
@@ -177,22 +178,22 @@ java.newInstance("java.util.ArrayList", function(err, list2) {
177178
console.log(list2.toStringSync()); // [item1, item2]
178179
});
179180

180-
var ArrayList = java.import('java.util.ArrayList');
181-
var list3 = new ArrayList();
181+
const ArrayList = java.import('java.util.ArrayList');
182+
const list3 = new ArrayList();
182183
list3.addSync('item1');
183184
list3.equalsSync(list1); // true
184185
```
185186

186187
### Create a char array
187188

188189
```javascript
189-
var charArray = java.newArray("char", "hello world\n".split(''));
190+
const charArray = java.newArray("char", "hello world\n".split(''));
190191
```
191192

192193
### Create a byte array
193194

194195
```javascript
195-
var byteArray = java.newArray(
196+
const byteArray = java.newArray(
196197
"byte",
197198
"hello world\n"
198199
.split('')
@@ -207,7 +208,7 @@ a property off of the result called "longValue" which contains the un-truncated
207208
If you are calling a method that takes a long you must create it using [java.newInstance](#javaNewInstance).
208209

209210
```javascript
210-
var javaLong = java.newInstanceSync("java.lang.Long", 5);
211+
const javaLong = java.newInstanceSync("java.lang.Long", 5);
211212
console.log('Possibly truncated long value: ' + javaLong);
212213
console.log('Original long value (as a string): ' + javaLong.longValue);
213214
java.callStaticMethodSync("Test", "staticMethodThatTakesALong", javaLong);
@@ -236,7 +237,7 @@ As of release 0.4.5 it became possible to create async methods that return promi
236237
Example:
237238

238239
```javascript
239-
var java = require("java");
240+
const java = require("java");
240241
java.asyncOptions = {
241242
asyncSuffix: undefined, // Don't generate node-style methods taking callbacks
242243
syncSuffix: "", // Sync methods use the base name(!!)
@@ -401,13 +402,13 @@ __Arguments__
401402

402403
__Example__
403404

404-
var Test = java.import('Test');
405+
const Test = java.import('Test');
405406
Test.someStaticMethodSync(5);
406407
console.log(Test.someStaticField);
407408

408-
var value1 = Test.NestedEnum.Value1;
409+
const value1 = Test.NestedEnum.Value1;
409410

410-
var test = new Test();
411+
const test = new Test();
411412
list.instanceMethodSync('item1');
412413

413414
## newInstance
@@ -428,7 +429,7 @@ __Arguments__
428429

429430
__Example__
430431

431-
var list = java.newInstanceSync("java.util.ArrayList");
432+
const list = java.newInstanceSync("java.util.ArrayList");
432433

433434
java.newInstance("java.util.ArrayList", function(err, list) {
434435
if(err) { console.error(err); return; }
@@ -450,7 +451,7 @@ __Arguments__
450451

451452
__Example__
452453

453-
var obj = java.newInstanceSync("my.package.SubClass");
454+
const obj = java.newInstanceSync("my.package.SubClass");
454455

455456
if(java.instanceOf(obj, "my.package.SuperClass")) {
456457
console.log("obj is an instance of SuperClass");
@@ -464,8 +465,7 @@ __Example__
464465

465466
**java.callStaticMethodSync(className, methodName, [args...]) : result**
466467

467-
Calls a static method on the specified class. If you are using the sync method an exception will be throw if an error occurs,
468-
otherwise it will be the first argument in the callback.
468+
Calls a static method on the specified class. If you are using the sync method an exception will be throw if an error occurs, otherwise it will be the first argument in the callback.
469469

470470
__Arguments__
471471

@@ -475,7 +475,7 @@ __Arguments__
475475

476476
__Example__
477477

478-
var result = java.callStaticMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
478+
const result = java.callStaticMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
479479

480480
java.callStaticMethod("com.nearinfinty.MyClass", "doSomething", 42, "test", function(err, results) {
481481
if(err) { console.error(err); return; }
@@ -501,11 +501,11 @@ __Arguments__
501501

502502
__Example__
503503

504-
var instance = java.newInstanceSync("com.nearinfinty.MyClass");
504+
const instance = java.newInstanceSync("com.nearinfinty.MyClass");
505505

506-
var result = java.callMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
506+
const result = java.callMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
507507

508-
java.callMethodSync(instance, "doSomething", 42, "test", function(err, results) {
508+
java.callMethod(instance, "doSomething", 42, "test", function(err, results) {
509509
if(err) { console.error(err); return; }
510510
// results from doSomething
511511
});
@@ -525,7 +525,7 @@ __Arguments__
525525

526526
__Example__
527527

528-
var data = java.getStaticFieldValue("com.nearinfinty.MyClass", "data");
528+
const data = java.getStaticFieldValue("com.nearinfinty.MyClass", "data");
529529

530530
## setStaticFieldValue
531531

@@ -560,7 +560,7 @@ __Arguments__
560560

561561
__Example__
562562

563-
var newArray = java.newArray("java.lang.String", ["item1", "item2", "item3"]);
563+
const newArray = java.newArray("java.lang.String", ["item1", "item2", "item3"]);
564564

565565
## newByte
566566

@@ -576,7 +576,7 @@ __Arguments__
576576

577577
__Example__
578578

579-
var b = java.newByte(12);
579+
const b = java.newByte(12);
580580

581581
## newShort
582582

@@ -592,7 +592,7 @@ __Arguments__
592592

593593
__Example__
594594

595-
var s = java.newShort(12);
595+
const s = java.newShort(12);
596596

597597
## newLong
598598

@@ -608,7 +608,7 @@ __Arguments__
608608

609609
__Example__
610610

611-
var s = java.newLong(12);
611+
const s = java.newLong(12);
612612

613613
## newChar
614614

@@ -624,7 +624,7 @@ __Arguments__
624624

625625
__Example__
626626

627-
var ch = java.newChar('a');
627+
const ch = java.newChar('a');
628628

629629
## newDouble
630630

@@ -640,7 +640,7 @@ __Arguments__
640640

641641
__Example__
642642

643-
var d = java.newDouble(3.14);
643+
const d = java.newDouble(3.14);
644644

645645
## newFloat
646646

@@ -656,7 +656,7 @@ __Arguments__
656656

657657
__Example__
658658

659-
var f = java.newFloat(3.14);
659+
const f = java.newFloat(3.14);
660660

661661
## newProxy
662662

@@ -676,14 +676,14 @@ __Arguments__
676676

677677
__Example__
678678

679-
var myProxy = java.newProxy('java.lang.Runnable', {
679+
const myProxy = java.newProxy('java.lang.Runnable', {
680680
run: function () {
681681
// This is actually run on the v8 thread and not the new java thread
682682
console.log("hello from thread");
683683
}
684684
});
685685

686-
var thread = java.newInstanceSync("java.lang.Thread", myProxy);
686+
const thread = java.newInstanceSync("java.lang.Thread", myProxy);
687687
thread.start();
688688

689689
## isJvmCreated
@@ -742,7 +742,7 @@ __Arguments__
742742

743743
__Example__
744744

745-
var list = java.newInstanceSync("java.util.ArrayList");
745+
const list = java.newInstanceSync("java.util.ArrayList");
746746
list.addSync("item1");
747747
list.add("item2", function(err, result) {
748748
if(err) { console.error(err); return; }
@@ -761,9 +761,9 @@ field values.
761761

762762
__Example__
763763

764-
var list = java.newInstanceSync("com.nearinfinty.MyClass");
764+
const list = java.newInstanceSync("com.nearinfinty.MyClass");
765765
list.data = "test";
766-
var data = list.data;
766+
const data = list.data;
767767

768768
## Getting the Full Method Signature
769769

@@ -816,9 +816,9 @@ public class ShutdownHookHelper {
816816
Compile ShutdownHookHelper and then use it as follows.
817817

818818
```javascript
819-
var java = require('./');
819+
const java = require('./');
820820
java.classpath.push('.');
821-
var ShutdownHookHelper = java.import('ShutdownHookHelper');
821+
const ShutdownHookHelper = java.import('ShutdownHookHelper');
822822

823823
ShutdownHookHelper.setShutdownHookSync(java.newProxy('java.lang.Runnable', {
824824
run: function () {
@@ -839,12 +839,12 @@ When you call a Java method through node-java, any arguments (V8/JavaScript obje
839839
The JavaScript object returned by `java.import(classname)` is a JavaScript constructor Function, implemented such that you can create instances of the Java class. For example:
840840

841841
```javascript
842-
var Test = java.import('Test');
843-
var test = new Test();
842+
const Test = java.import('Test');
843+
const test = new Test();
844844

845845
Test.someStaticMethod(function(err, result) { ... });
846846

847-
var value1 = Test.NestedEnum.Value1;
847+
const value1 = Test.NestedEnum.Value1;
848848
```
849849

850850
But JavaScript reserves a few property names of Function objects: `name`, `arguments`, and `caller`. If your class has public static members (either methods or fields) with these names, node-java is unable to create the necessary property to implement the class's API. For example, suppose your class `Test` implements a static method named `caller`, or has a `NestedEnum` with a value `name`:
@@ -860,25 +860,25 @@ public class Test {
860860
In JavaScript, you would expect to be able to use those static members like this:
861861

862862
```javascript
863-
var Test = java.import('Test');
863+
const Test = java.import('Test');
864864
Test.caller(function(err, result) { ... }); // ERROR
865-
var value = Test.NestedEnum.name; // ERROR
865+
const value = Test.NestedEnum.name; // ERROR
866866
```
867867

868868
Node-java can't create those properties, so the above code won't work. Instead, node-java appends a suffix to the name. The default suffix is simply an underscore `_`, but you can change the suffix using `asyncOptions`:
869869

870870
```javascript
871-
var java = require('java');
871+
const java = require('java');
872872

873873
java.asyncOptions = {
874874
asyncSuffix: "",
875875
syncSuffix: "Sync",
876876
ifReadOnlySuffix: "_alt"
877877
};
878878

879-
var Test = java.import('Test');
879+
const Test = java.import('Test');
880880
Test.caller_alt(function(err, result) { ... }); // OK
881-
var value = Test.NestedEnum.name_alt; // OK
881+
const value = Test.NestedEnum.name_alt; // OK
882882
```
883883

884884
# Troubleshooting

0 commit comments

Comments
 (0)