java-bridge 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/java.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { InterfaceProxyOptions, Java, JavaOptions } from '../native';
1
+ import { InterfaceProxyOptions, Java, JavaOptions, JavaConfig, ClassConfiguration } from '../native';
2
2
  import { JavaClass, JavaClassConstructorType, JavaVersion, UnknownJavaClass, UnknownJavaClassType } from './definitions';
3
- export { clearDaemonProxies } from '../native';
3
+ export { clearDaemonProxies, clearClassProxies, logging } from '../native';
4
4
  /**
5
5
  * Options for creating the Java VM.
6
6
  */
@@ -116,6 +116,14 @@ export declare function setClassLoader(classLoader: UnknownJavaClass): void;
116
116
  * constructor type of the class as the template parameter to get
117
117
  * the proper type returned. You could also just cast the result.
118
118
  *
119
+ * When passing a {@link ClassConfiguration} object, the config will be applied
120
+ * to this class. This config does not apply to any other class.
121
+ * If you want to change the config for all classes, use the
122
+ * {@link config} class in order to do that. Any undefined field
123
+ * in the config will be ignored and the default value will be used.
124
+ * If you want to change the sync and async suffixes to an empty string,
125
+ * you can pass an empty string as the suffix.
126
+ *
119
127
  * ## Examples
120
128
  * ### Import ``java.util.ArrayList`` and create a new instance of it
121
129
  * ```ts
@@ -130,10 +138,10 @@ export declare function setClassLoader(classLoader: UnknownJavaClass): void;
130
138
  *
131
139
  * ### Import ``java.util.ArrayList`` with types
132
140
  * ```ts
133
- * import { importClass, JavaClassInstance, JavaType } from 'java-bridge';
141
+ * import { importClass, JavaClass, JavaType } from 'java-bridge';
134
142
  *
135
143
  * // Definitions for class java.util.List
136
- * declare class List <T extends JavaType> extends JavaClassInstance {
144
+ * declare class List<T extends JavaType> extends JavaClass {
137
145
  * size(): Promise<number>;
138
146
  * sizeSync(): number;
139
147
  * add(e: T): Promise<void>;
@@ -168,15 +176,36 @@ export declare function setClassLoader(classLoader: UnknownJavaClass): void;
168
176
  * assert.equals(list.getSync(1), 'World');
169
177
  * ```
170
178
  *
179
+ * ### Import ``java.util.ArrayList`` with custom config
180
+ * ```ts
181
+ * import { importClass, config } from 'java-bridge';
182
+ *
183
+ * // Import java.util.ArrayList with custom config
184
+ * const ArrayList = importClass('java.util.ArrayList', {
185
+ * syncSuffix: '',
186
+ * asyncSuffix: 'Async',
187
+ * });
188
+ *
189
+ * // Create a new instance of ArrayList
190
+ * const list = new ArrayList();
191
+ *
192
+ * // Call the async method
193
+ * await list.addAsync('Hello World!');
194
+ *
195
+ * // Call the sync method
196
+ * list.add('Hello World!');
197
+ * ```
198
+ *
171
199
  * @template T the type of the java class to import as a js type
172
200
  * @param classname the name of the class to resolve
201
+ * @param config the config to use when importing the class
173
202
  * @return the java class constructor
174
203
  */
175
- export declare function importClass<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string): T;
204
+ export declare function importClass<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string, config?: ClassConfiguration): T;
176
205
  /**
177
206
  * @inheritDoc importClass
178
207
  */
179
- export declare function importClassAsync<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string): Promise<T>;
208
+ export declare function importClassAsync<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string, config?: ClassConfiguration): Promise<T>;
180
209
  /**
181
210
  * Append a single or multiple jars to the class path.
182
211
  *
@@ -463,7 +492,14 @@ export interface JavaInterfaceProxy<T extends ProxyRecord<T> = AnyProxyRecord> {
463
492
  * @return the value to pass back to the java process
464
493
  */
465
494
  export type ProxyMethod = (...args: any[]) => any;
495
+ /**
496
+ * A record of methods to implement.
497
+ * Useful for creating a proxy for a specific interface.
498
+ */
466
499
  export type ProxyRecord<T> = Partial<Record<keyof T, ProxyMethod>>;
500
+ /**
501
+ * A generic proxy record.
502
+ */
467
503
  export type AnyProxyRecord = Record<string, ProxyMethod>;
468
504
  /**
469
505
  * Create a new java interface proxy.
@@ -558,6 +594,9 @@ export type AnyProxyRecord = Record<string, ProxyMethod>;
558
594
  * ## Notes
559
595
  * * Keep this instance in scope to not destroy the interface proxy.
560
596
  * * Call {@link JavaInterfaceProxy.reset} to instantly destroy this instance.
597
+ * Please note that calling {@link JavaInterfaceProxy.reset} is not necessary,
598
+ * the proxy instance will be automatically destroyed when it is garbage collected.
599
+ * Calling {@link JavaInterfaceProxy.reset} will just speed up the process.
561
600
  * * If any method is queried by the java process and not implemented in here,
562
601
  * an exception will be thrown in the java process.
563
602
  * * Any errors thrown in the javascript process will be rethrown in the java process.
@@ -570,7 +609,7 @@ export type AnyProxyRecord = Record<string, ProxyMethod>;
570
609
  *
571
610
  * If you still want to call everything in a synchronous manner, make sure to enable
572
611
  * running the event loop while waiting for a java method to return by setting
573
- * {@link config.runEventLoopWhenInterfaceProxyIsActive} to true.
612
+ * {@link JavaConfig.runEventLoopWhenInterfaceProxyIsActive} to true.
574
613
  * **This may cause application crashes, so it is strongly recommended to just use async methods.**
575
614
  *
576
615
  * ### Keeping the proxy alive
@@ -623,31 +662,6 @@ export declare function newProxy<T extends ProxyRecord<T> = AnyProxyRecord>(inte
623
662
  */
624
663
  export declare function getJavaInstance(): Java | null;
625
664
  /**
626
- * Configuration options for the java bridge.
665
+ * @inheritDoc JavaConfig
627
666
  */
628
- export declare class config {
629
- /**
630
- * **Experimental Feature**
631
- *
632
- * Set whether to run the event loop when an interface proxy is active.
633
- * This is disabled by default. Enabling this will cause the bridge
634
- * to run the event loop when an interface proxy either as direct
635
- * proxy or as daemon proxy is active. This is only required if the
636
- * proxied method calls back into the javascript process in the same thread.
637
- * If the proxy is used either in an async method or in a different thread,
638
- * this is not required.
639
- *
640
- * **Note:** Enabling this may cause the application to crash. Use with caution.
641
- *
642
- * @experimental
643
- * @param value whether to run the event loop when an interface proxy is active
644
- */
645
- static set runEventLoopWhenInterfaceProxyIsActive(value: boolean);
646
- /**
647
- * **Experimental Feature**
648
- *
649
- * Get whether to run the event loop when an interface proxy is active.
650
- * @experimental
651
- */
652
- static get runEventLoopWhenInterfaceProxyIsActive(): boolean;
653
- }
667
+ export declare const config: JavaConfig;
package/dist/native.d.ts CHANGED
@@ -3,6 +3,76 @@
3
3
 
4
4
  /* auto-generated by NAPI-RS */
5
5
 
6
+ /**
7
+ * Configuration for the Java class proxy.
8
+ *
9
+ * @since 2.4.0
10
+ */
11
+ export interface ClassConfiguration {
12
+ /**
13
+ * If true, the event loop will be run when an interface proxy is active.
14
+ * If not specified, the value from the global configuration will be used.
15
+ */
16
+ runEventLoopWhenInterfaceProxyIsActive?: boolean
17
+ /**
18
+ * If true, the custom inspect method will be used to display the object in the console.
19
+ * If not specified, the value from the global configuration will be used.
20
+ */
21
+ customInspect?: boolean
22
+ /**
23
+ * The suffix to use for synchronous methods.
24
+ * Set this value to an empty string to disable the suffix.
25
+ * The default value is "Sync".
26
+ * Setting this value to the same value as asyncSuffix will result in an error.
27
+ * If not specified, the value from the global configuration will be used.
28
+ */
29
+ syncSuffix?: string
30
+ /**
31
+ * The suffix to use for asynchronous methods.
32
+ * Set this value to an empty string to disable the suffix.
33
+ * The default value is an empty string.
34
+ * Setting this value to the same value as syncSuffix will result in an error.
35
+ * If not specified, the value from the global configuration will be used.
36
+ */
37
+ asyncSuffix?: string
38
+ }
39
+ /**
40
+ * Configuration for the Java class proxy.
41
+ *
42
+ * @since 2.4.0
43
+ */
44
+ export interface Config {
45
+ /**
46
+ * If true, the event loop will be run when an interface proxy is active.
47
+ *
48
+ * @since 2.2.3
49
+ */
50
+ runEventLoopWhenInterfaceProxyIsActive: boolean
51
+ /**
52
+ * If true, the custom inspect method will be used to display the object in the console.
53
+ *
54
+ * @since 2.4.0
55
+ */
56
+ customInspect: boolean
57
+ /**
58
+ * The suffix to use for synchronous methods.
59
+ * Set this value to an empty string to disable the suffix.
60
+ * The default value is "Sync".
61
+ * Setting this value to the same value as asyncSuffix will result in an error.
62
+ *
63
+ * @since 2.4.0
64
+ */
65
+ syncSuffix?: string
66
+ /**
67
+ * The suffix to use for asynchronous methods.
68
+ * Set this value to an empty string to disable the suffix.
69
+ * The default value is an empty string.
70
+ * Setting this value to the same value as syncSuffix will result in an error.
71
+ *
72
+ * @since 2.4.0
73
+ */
74
+ asyncSuffix?: string
75
+ }
6
76
  /** Options for the interface proxies */
7
77
  export interface InterfaceProxyOptions {
8
78
  /**
@@ -13,6 +83,14 @@ export interface InterfaceProxyOptions {
13
83
  }
14
84
  /** Clears the list of daemon proxies. */
15
85
  export function clearDaemonProxies(): void
86
+ /**
87
+ * Clear the class proxy cache.
88
+ * Use this method in order to reset the config for all class proxies.
89
+ * The new config will be applied once the class is imported again.
90
+ *
91
+ * @since 2.4.0
92
+ */
93
+ export function clearClassProxies(): void
16
94
  /**
17
95
  * Options for the Java VM.
18
96
  * Not the same as vm arguments.
@@ -54,13 +132,13 @@ export class Java {
54
132
  * Will import the class and parse all of its methods and fields.
55
133
  * The imported class will be cached for future use.
56
134
  */
57
- importClass(className: string): object
135
+ importClass(className: string, config?: ClassConfiguration | undefined | null): object
58
136
  /**
59
137
  * Import a java class (async)
60
138
  * Will return a promise that resolves to the class instance.
61
139
  * @see importClass
62
140
  */
63
- importClassAsync(className: string): Promise<object>
141
+ importClassAsync(className: string, config?: ClassConfiguration | undefined | null): Promise<object>
64
142
  /**
65
143
  * Get the wanted JVM version.
66
144
  * This may not match the actual JVM version.
@@ -83,11 +161,189 @@ export class Java {
83
161
  get classLoader(): object
84
162
  set classLoader(classLoader: object)
85
163
  }
164
+ /**
165
+ * Configuration options for the java bridge.
166
+ *
167
+ * As of version 2.4.0, the options are cached inside the class proxy cache.
168
+ * This means that changing the options will not affect any class proxies
169
+ * that have already been created by importing a class using {@link importClass}
170
+ * or {@link importClassAsync}. You must clear the class proxy cache using the
171
+ * {@link clearClassProxies} method in order to apply the new options to all
172
+ * classes imported at a later date. This does not affect already instantiated
173
+ * or imported classes.
174
+ *
175
+ * Do not instantiate this class. Use the {@link default.config} instance instead.
176
+ *
177
+ * @since 2.2.3
178
+ */
86
179
  export class JavaConfig {
87
- static setRunEventLoopWhenInterfaceProxyIsActive(value: boolean): void
88
- static getRunEventLoopWhenInterfaceProxyIsActive(): boolean
180
+ /**
181
+ * Do not instantiate this class.
182
+ * Use the {@link default.config} instance instead.
183
+ */
184
+ constructor()
185
+ /**
186
+ * **Experimental Feature**
187
+ *
188
+ * Set whether to run the event loop when an interface proxy is active.
189
+ * This is disabled by default. Enabling this will cause the bridge
190
+ * to run the event loop when an interface proxy either as direct
191
+ * proxy or as daemon proxy is active. This is only required if the
192
+ * proxied method calls back into the javascript process in the same thread.
193
+ * If the proxy is used either in an async method or in a different thread,
194
+ * this is not required.
195
+ *
196
+ * **Note:** Enabling this may cause the application to crash. Use with caution.
197
+ *
198
+ * @since 2.2.3
199
+ * @experimental
200
+ * @param value whether to run the event loop when an interface proxy is active
201
+ */
202
+ set runEventLoopWhenInterfaceProxyIsActive(value: boolean)
203
+ /**
204
+ * **Experimental Feature**
205
+ *
206
+ * Get whether to run the event loop when an interface proxy is active.
207
+ * @since 2.2.3
208
+ * @experimental
209
+ */
210
+ get runEventLoopWhenInterfaceProxyIsActive(): boolean
211
+ /**
212
+ * Whether to add custom inspect methods to java objects.
213
+ * This is disabled by default.
214
+ * This allows console.log to print java objects in a more readable way
215
+ * using the `toString` method of the java object.
216
+ *
217
+ * @since 2.4.0
218
+ * @param value whether to add custom inspect methods to java objects
219
+ */
220
+ set customInspect(value: boolean)
221
+ /**
222
+ * Get whether to add custom inspect methods to java objects.
223
+ *
224
+ * @since 2.4.0
225
+ * @returns whether to add custom inspect methods to java objects
226
+ */
227
+ get customInspect(): boolean
228
+ /**
229
+ * Set the suffix for synchronous methods.
230
+ * This is `Sync` by default.
231
+ * Pass `null` or an empty string to disable the suffix.
232
+ * This must not be the same as the {@link asyncSuffix}.
233
+ *
234
+ * This option does not affect standard methods of java classes
235
+ * like `toString`, `toStringSync`, `toStringAsync` and `newInstanceAsync`.
236
+ *
237
+ * ## Example
238
+ * ```ts
239
+ * import { config, clearClassProxies } from 'java-bridge';
240
+ *
241
+ * // Set the async suffix in order to prevent errors
242
+ * config.asyncSuffix = 'Async';
243
+ * // Set the sync suffix to an empty string
244
+ * config.syncSuffix = '';
245
+ * // This would do the same
246
+ * config.syncSuffix = null;
247
+ *
248
+ * // Clear the class proxy cache
249
+ * clearClassProxies();
250
+ *
251
+ * // Import the class
252
+ * const ArrayList = importClass('java.util.ArrayList');
253
+ *
254
+ * // Create a new instance
255
+ * const list = new ArrayList();
256
+ *
257
+ * // Call the method
258
+ * list.add('Hello World!');
259
+ *
260
+ * // Async methods now have the 'Async' suffix
261
+ * await list.addAsync('Hello World!');
262
+ * ```
263
+ *
264
+ * @see asyncSuffix
265
+ * @since 2.4.0
266
+ * @param value the suffix to use for synchronous methods
267
+ */
268
+ set syncSuffix(value: string | undefined | null)
269
+ /**
270
+ * Get the suffix for synchronous methods.
271
+ *
272
+ * @since 2.4.0
273
+ */
274
+ get syncSuffix(): string | null
275
+ /**
276
+ * Set the suffix for asynchronous methods.
277
+ * This is `Async` by default.
278
+ * Pass `null` or an empty string to disable the suffix.
279
+ * This must not be the same as the {@link syncSuffix}.
280
+ *
281
+ * This option does not affect standard methods of java classes
282
+ * like `toString`, `toStringSync`, `toStringAsync` and `newInstanceAsync`.
283
+ *
284
+ * @see syncSuffix
285
+ * @since 2.4.0
286
+ * @param value the suffix to use for asynchronous methods
287
+ */
288
+ set asyncSuffix(value: string | undefined | null)
289
+ /**
290
+ * Get the suffix for asynchronous methods.
291
+ *
292
+ * @since 2.4.0
293
+ */
294
+ get asyncSuffix(): string | null
295
+ /**
296
+ * Override the whole config.
297
+ * If you want to change only a single field, use the static setters instead.
298
+ *
299
+ * @since 2.4.0
300
+ * @param value the config to use
301
+ */
302
+ set config(value: Config)
303
+ /**
304
+ * Get the current config.
305
+ *
306
+ * @since 2.4.0
307
+ */
308
+ get config(): Config
309
+ /**
310
+ * Reset the config to the default values.
311
+ *
312
+ * @since 2.4.0
313
+ */
314
+ reset(): void
89
315
  }
90
316
  export class StdoutRedirect {
91
317
  on(event: string, callback?: ((...args: any[]) => any) | null): void
92
318
  reset(): void
93
319
  }
320
+ export namespace logging {
321
+ /**
322
+ * This method is not supported in this build.
323
+ * It will print a warning to stderr when called.
324
+ *
325
+ * Re-compile the native module with the `log` feature to enable logging.
326
+ */
327
+ export function setLogCallbacks(out: ((data: string | null) => void) | null | undefined, err: ((data: string | null) => void) | null | undefined): void
328
+ /**
329
+ * This method is not supported in this build.
330
+ * It will print a warning to stderr when called.
331
+ *
332
+ * Re-compile the native module with the `log` feature to enable logging.
333
+ */
334
+ export function initLogger(path: string): void
335
+ /**
336
+ * This method is not supported in this build.
337
+ * It will print a warning to stderr when called.
338
+ *
339
+ * Re-compile the native module with the `log` feature to enable logging.
340
+ */
341
+ export function resetLogCallbacks(): void
342
+ /**
343
+ * Whether logging is supported.
344
+ * Logging is disabled by default.
345
+ * This constant currently is set to `false`
346
+ * as logging is not supported in this build.
347
+ */
348
+ export const LOGGING_SUPPORTED: boolean
349
+ }
@@ -1,2 +1,2 @@
1
1
  export declare function getNativeLibPath(isPackagedElectron: boolean): string;
2
- export declare function getJavaLibPath(isPackagedElectron: boolean): string;
2
+ export declare function getJavaLibPath(): string;