Major changes
Improved object to string conversion
Previously, when a java object was converted to a string using an implicit conversion,
the toString
method of the java object wasn't used. Instead, the js object representation
was returned as a string.
In order to improve this behaviour, the toString
method now returns a string
instead
of a Promise<string>
, a toStringAsync
method returning a Promise<string>
was added
and the toStringSync
method has been deprecated.
Now, the toString
method of the java object is properly utilized in order to convert an object
to a string:
import { importClass } from 'java-bridge';
const ArrayList = importClass('java.util.ArrayList');
const list = new ArrayList();
list.addSync('Hello');
list.addSync('World');
// Convert the list to a string (implicit conversion)
const str = list + ''; // [Hello, World]
Improved the conversion of objects to a string inside console.log
This improved conversion also applies to objects passed into console.log
,
when the customInspect
config setting is set to true:
import { importClass, config } from 'java-bridge';
config.customInspect = true;
const ArrayList = importClass('java.util.ArrayList');
const list = new ArrayList();
list.addSync('Hello');
list.addSync('World');
console.log(list); // [Hello, World]
Added more config options
A few rules around the configuration of the module have changed:
- The configuration is now bound to a java class proxy. This means,
the config cannot be for a specific class, once this class has been imported
explicitly or implicitly (as a dependency of some sorts) usingimportClass
orimportClassAsync
. This means, that the config should be set before importing
a class. If you still want to change the config afterwards, you need to call the
clearClassProxies
method before re-importing a class with an updated config, in order
to apply the config to the newly imported class. - The config may now be reset to its default values using
config.reset()
. - The whole config can now be updated at once using the
config.config
setter.
Added options to set custom sync and async suffixes
Sync and async suffixes may now be set to custom values using the
syncSuffix
and asyncSuffix
config values.
Please note, that these values cannot be equal. If equal values
are set for both values, an error will be thrown inside the specific
setter. These options do not affect standard methods of java classes
like toString
, toStringSync
, toStringAsync
and newInstanceAsync
.
import { importClass, config, clearClassProxies } from 'java-bridge';
// Set the async suffix in order to prevent errors
config.asyncSuffix = 'Async';
// Set the sync suffix to an empty string
config.syncSuffix = '';
// This would do the same
config.syncSuffix = null;
// Clear the class proxy cache
clearClassProxies();
// Import the class
const ArrayList = importClass('java.util.ArrayList');
// Create a new instance
const list = new ArrayList();
// Call the method
list.add('Hello World!');
// Async methods now have the 'Async' suffix
await list.addAsync('Hello World!');
Added an option to importClass
and importClassAsync
in order to override the config
The config for a specific class or class import can be temporarily
overriden by passing a config object to importClass
or importClassAsync
.
This config passed does not apply to any classes transiently imported by this call,
nor does it affect any other imports (even ones of the same class).
import { importClass } from 'java-bridge';
const JavaString = importClass('java.lang.String', {
syncSuffix: 'SyncSuffix',
});
const str = new JavaString('test');
str.containsSyncSuffix('e'); // true
Added support for logging
A special version of the module may be built which includes support for
logging. This requires the module to be built using npm run build:all
.
Refer to the logging section of the readme
for further information on logging.
The settings for logging are located inside the logging
namespace:
import { logging } from 'java-bridge';
logging.initLogger('log4rs.json');
logging.setLogCallbacks(
(out) => console.log(out),
(err) => console.error(err)
);
The full documentation for the module is available through the jsdoc of
the module once it has been built with logging support.
If the current build does not support logging, all methods inside
the logging
namespace will be dummies and print an error message
to stdout
once called (the message will only be printed once per
program execution).
Enhanced stack traces of async method calls
The stack traces returned by errors thrown inside asnyc contexts
now return the full stack trace including the stack trace on
the javascript side. A call like
import { importClass } from 'java-bridge';
const JavaString = importClass('java.lang.String');
await JavaString.newInstanceAsync(null);
will now throw:
Error: java.lang.NullPointerException: Cannot invoke "java.lang.StringBuffer.toString()" because "buffer" is null
at java.base/java.lang.String.<init>(String.java:1446)
at crates\java-rs\src\java\java_env_wrapper.rs:1371
at crates\java-rs\src\java\java_env_wrapper.rs:304
at C:\workspace\test.js:56:13
at Object.<anonymous> (C:\workspace\test.js:57:3)
at Module._compile (node:internal/modules/cjs/loader:1226:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
at Module.load (node:internal/modules/cjs/loader:1089:32)
at Module._load (node:internal/modules/cjs/loader:930:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
code: 'GenericFailure'
}
What's Changed
- docs: add vc redist info for windows by @MarkusJx in #76
- ci(arm): fix jdk install error by @MarkusJx in #80
- ci: check rust style by @MarkusJx in #79
- refactor: move java-rs package into this repo by @MarkusJx in #82
- fix(toString): allow java object conversions to string by @MarkusJx in #84
- feat(bridge): improve config by @MarkusJx in #85
- feat(bridge): add support for logging by @MarkusJx in #78
- feat(bridge): enhance stack traces of asynchronous method calls by @MarkusJx in #86
Full Changelog: v2.3.0...v2.4.0