You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+83-13Lines changed: 83 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -235,6 +235,8 @@ try {
235
235
}
236
236
```
237
237
238
+
<aname="asyncOptions" >
239
+
238
240
### AsyncOptions: control over the generation of sync, async & promise method variants.
239
241
240
242
As of release 0.4.5 it became possible to create async methods that return promises by setting the `asyncOptions` property of the java object. With release 0.4.7 this feature is extended to allow changing the suffix assigned for sync and async method variants, and to further configure this module to optionally omit generation of any of these variants.
* NOTE: Due to specifics of initialization order, the methods `java.newInstancePromise`, `java.callMethodPromise`, and `java.callStaticMethodPromise` are not available until the JVM has been created. You may need to call some other java method such as `java.import()` to finalize java initialization, or even better, the function `java.ensureJvm()`.
273
275
274
276
##### Special note about the exported module functions `newInstance`, `callMethod`, and `callStaticMethod`.
275
-
These methods come in both async and sync variants. If you provide the `promisify` and `promiseSuffix` attributes in asyncOptions then you'll also get the Promises/A+ variant for these three functions. However, if you change the defacto
276
-
conventions for the `syncSuffix` (i.e. 'Sync') and/or `asyncSuffix` (i.e. '') it will not affect the naming for these three functions. I.e. no matter what you specify in asyncOptions, the async variants are named `newInstance`, `callMethod`, and `callStaticMethod`, and the sync variants are named `newInstanceSync`, `callMethodSync`, and `callStaticMethodSync`.
277
+
These methods come in both async and sync variants. If you provide the `promisify` and `promiseSuffix` attributes in asyncOptions then you'll also get the Promises/A+ variant for these three functions. However, if you change the defacto conventions for the `syncSuffix` (i.e. 'Sync') and/or `asyncSuffix` (i.e. '') it will not affect the naming for these three functions. I.e. no matter what you specify in asyncOptions, the async variants are named `newInstance`, `callMethod`, and `callStaticMethod`, and the sync variants are named `newInstanceSync`, `callMethodSync`, and `callStaticMethodSync`.
277
278
278
279
## Varargs support
279
280
280
-
With v0.5.0 node-java now supports methods with variadic arguments (varargs). Prior to v0.5.0,
281
-
a javascript call to a Java varargs method had to construct an array of the variadic arguments using `java.newArray()`. With v0.5.0 javascript applications can simply use the variadic style.
281
+
With v0.5.0 node-java now supports methods with variadic arguments (varargs). Prior to v0.5.0, a JavaScript call to a Java varargs method had to construct an array of the variadic arguments using `java.newArray()`. With v0.5.0 JavaScript applications can simply use the variadic style.
282
282
283
-
In most cases it is still acceptable to use `java.newArray()`. But it is now possible to pass a plain javascript array, or use the variadic style. For example, consider these snippets from the unit test file `test/varargs-test.js`:
283
+
In most cases it is still acceptable to use `java.newArray()`. But it is now possible to pass a plain JavaScript array, or use the variadic style. For example, consider these snippets from the unit test file `test/varargs-test.js`:
@@ -289,7 +289,7 @@ In most cases it is still acceptable to use `java.newArray()`. But it is now pos
289
289
290
290
```
291
291
292
-
Note that when passing a Javascript array (e.g. `['a', 'b', 'c']`) for a varargs parameter, node-java must infer the Java type of the array. If all of the elements are of the same javascript primitive type (`string` in this example) then node-java will create a Java array of the corresponding type (e.g. `java.lang.String`). The Java types that node-java can infer are: `java.lang.String`, `java.lang.Boolean`, `java.lang.Integer`, `java.lang.Long`, and `java.lang.Double`. If an array has a mix of `Integer`, `Long`, and `Double`, then the inferred type will be `java.lang.Number`. Any other mix will result in an inferred type of `java.lang.Object`.
292
+
Note that when passing a JavaScript array (e.g. `['a', 'b', 'c']`) for a varargs parameter, node-java must infer the Java type of the array. If all of the elements are of the same JavaScript primitive type (`string` in this example) then node-java will create a Java array of the corresponding type (e.g. `java.lang.String`). The Java types that node-java can infer are: `java.lang.String`, `java.lang.Boolean`, `java.lang.Integer`, `java.lang.Long`, and `java.lang.Double`. If an array has a mix of `Integer`, `Long`, and `Double`, then the inferred type will be `java.lang.Number`. Any other mix will result in an inferred type of `java.lang.Object`.
293
293
294
294
Methods accepting varargs of a generic type are also problematic. You will need to fall back to using `java.newArray()`. See [Issue #285](https://github.com/joeferner/node-java/issues/285).
295
295
@@ -314,6 +314,7 @@ With v0.5.1 a new API is available to make it easier for a complex application t
314
314
## java
315
315
*[classpath](#javaClasspath)
316
316
*[options](#javaOptions)
317
+
*[asyncOptions](#javaAsyncOptions)
317
318
*[import](#javaImport)
318
319
*[newInstance](#javaNewInstance)
319
320
*[instanceOf](#javaInstanceOf)
@@ -346,6 +347,8 @@ With v0.5.1 a new API is available to make it easier for a complex application t
346
347
347
348
<aname="javaClasspath" >
348
349
350
+
## classpath
351
+
349
352
*java.classpath**
350
353
351
354
Array of paths or jars to pass to the creation of the JVM.
syncSuffix:"", // Sync methods use the base name(!!)
384
+
promiseSuffix:"Promise", // Generate methods returning promises, using the suffix Promise.
385
+
promisify:require('util').promisify// Needs Node.js version 8 or greater, see comment below
386
+
ifReadOnlySuffix:"_alt"
387
+
};
388
+
```
389
+
390
+
*`asyncSuffix` Suffix for callback-based async method call signatures.
391
+
*`syncSuffix` Suffix for synchronous method call signatures.
392
+
*`promiseSuffix` Suffix for promise-based async method call signatures
393
+
*`promisify` Callback-to-promise transform implementation. From Node.js version 8 one can just use Node.js implementation: `promisify: require('util').promisify`.
394
+
*`ifReadOnlySuffix` See [Static Member Name Conflicts](#staticMemberNameConflicts).
395
+
396
+
See [Async Options](#asyncOptions) for details.
397
+
398
+
## import
399
+
373
400
<aname="javaImport" >
374
401
375
402
*java.import(className)**
376
403
377
-
Loads the class given by className such that it acts and feels like a javascript object.
404
+
Loads the class given by className such that it acts and feels like a JavaScript object.
var newArray = java.newArray("java.lang.String", ["item1", "item2", "item3"]);
531
572
573
+
## newByte
574
+
532
575
<aname="javaNewByte" >
533
576
534
577
*java.newByte(val)**
@@ -543,6 +586,8 @@ __Example__
543
586
544
587
var b = java.newByte(12);
545
588
589
+
## newShort
590
+
546
591
<aname="javaNewShort" >
547
592
548
593
*java.newShort(val)**
@@ -557,6 +602,8 @@ __Example__
557
602
558
603
var s = java.newShort(12);
559
604
605
+
## newLong
606
+
560
607
<aname="javaNewLong" >
561
608
562
609
*java.newLong(val)**
@@ -571,6 +618,8 @@ __Example__
571
618
572
619
var s = java.newLong(12);
573
620
621
+
## newChar
622
+
574
623
<aname="javaNewChar" >
575
624
576
625
*java.newChar(val)**
@@ -585,6 +634,8 @@ __Example__
585
634
586
635
var ch = java.newChar('a');
587
636
637
+
## newDouble
638
+
588
639
<aname="javaNewDouble" >
589
640
590
641
*java.newDouble(val)**
@@ -599,6 +650,8 @@ __Example__
599
650
600
651
var d = java.newDouble(3.14);
601
652
653
+
## newFloat
654
+
602
655
<aname="javaNewFloat" >
603
656
604
657
*java.newFloat(val)**
@@ -613,6 +666,8 @@ __Example__
613
666
614
667
var f = java.newFloat(3.14);
615
668
669
+
## newProxy
670
+
616
671
<aname="javaNewProxy" >
617
672
618
673
*java.newProxy(interfaceName, functions)**
@@ -639,24 +694,32 @@ __Example__
639
694
var thread = java.newInstanceSync("java.lang.Thread", myProxy);
640
695
thread.start();
641
696
697
+
## isJvmCreated
698
+
642
699
<aname="javaisJvmCreated" >
643
700
644
701
*java.isJvmCreated()**
645
702
646
703
Returns true if the JVM has been created. The JVM can only be created once.
647
704
705
+
## registerClient
706
+
648
707
<aname="javaRegisterClient" >
649
708
650
709
*java.registerClient(before, after)**
651
710
652
711
Register that a client wants to be called back immediately before and/or immediately after the JVM is created. If used, this function must be called before the JVM has been created. The before function is typically used to add to the classpath. The function may execute asynchronous operations (such as a async glob function). The after function is sometimes useful for doing one-time initialization that requires the JVM to first be initialized. If either function is unnecessary, use `null` or `undefined`. See also `registerClientP` and `ensureJvm`. See the unit tests in `testAsyncOptions` for examples.
653
712
713
+
## registerClientP
714
+
654
715
<aname="javaRegisterClientP" >
655
716
656
717
*java.registerClientP(before, after)**
657
718
658
719
Like java.registerClient, but before and after are assumed to be functions returning promises.
659
720
721
+
## ensureJvm
722
+
660
723
<aname="javaEnsureJvm" >
661
724
662
725
*java.ensureJvm(callback)**
@@ -667,7 +730,9 @@ If the JVM has not yet been created, execute the full JVM initialization process
667
730
668
731
<aname="javaObject">
669
732
670
-
# java object
733
+
# `java` object
734
+
735
+
## Call Method
671
736
672
737
<aname="javaObjectCallMethod" >
673
738
@@ -691,6 +756,8 @@ __Example__
691
756
if(err) { console.error(err); return; }
692
757
});
693
758
759
+
## Field Access
760
+
694
761
<aname="javaObjectGetSetField" >
695
762
696
763
*obj._fieldName_ = val**
@@ -706,9 +773,9 @@ __Example__
706
773
list.data = "test";
707
774
var data = list.data;
708
775
709
-
<aname="getFullMethodSignature" >
776
+
## Getting the Full Method Signature
710
777
711
-
Getting the Full Method Signature
778
+
<aname="getFullMethodSignature" >
712
779
713
780
Run `javap -s -classpath <your-class-path> <your-class-name>`. Find the method name you are looking for. For example:
When you call a Java method through node-java, any arguments (V8/JavaScript objects) will be converted to Java objects on the v8 main thread via a call to v8ToJava (found in utils.cpp). The JavaScript object is not held on to and can be garbage collected by v8. If this is an async call, the reference count on the Java objects will be incremented. The Java method will be invoked in a node.js async thread (see uv_queue_work). When the method returns, the resulting object will be returned to the main v8 thread and converted to JavaScript objects via a call to javaToV8 and the Java object's reference count will then be decremented to allow for garbage collection. The resulting v8 object will then be returned to the callers callback function.
774
841
842
+
843
+
<aname="staticMemberNameConflicts" >
844
+
775
845
# Static member name conflicts ('name', 'arguments', 'caller')
776
846
777
-
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:
847
+
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:
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`:
858
+
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`:
789
859
790
860
```java
791
861
publicclassTest {
@@ -795,7 +865,7 @@ public class Test {
795
865
}
796
866
```
797
867
798
-
In Javascript, you would expect to be able to use those static members like this:
868
+
In JavaScript, you would expect to be able to use those static members like this:
0 commit comments