java-bridge 2.1.1 → 2.1.4

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.
@@ -4,45 +4,39 @@
4
4
  * may differ if you use a different
5
5
  * version of the jvm shared library.
6
6
  */
7
- export enum JavaVersion {
7
+ export declare enum JavaVersion {
8
8
  /** Java version 1.1 */
9
- VER_1_1 = '1.1',
9
+ VER_1_1 = "1.1",
10
10
  /** Java version 1.2 */
11
- VER_1_2 = '1.2',
11
+ VER_1_2 = "1.2",
12
12
  /** Java version 1.4 */
13
- VER_1_4 = '1.4',
13
+ VER_1_4 = "1.4",
14
14
  /** Java version 1.6 */
15
- VER_1_6 = '1.6',
15
+ VER_1_6 = "1.6",
16
16
  /** Java version 1.8 */
17
- VER_1_8 = '1.8',
17
+ VER_1_8 = "1.8",
18
18
  /** Java version 9 */
19
- VER_9 = '9',
19
+ VER_9 = "9",
20
20
  /** Java version 10 */
21
- VER_10 = '10',
21
+ VER_10 = "10"
22
22
  }
23
-
24
- Object.freeze(JavaVersion);
25
-
26
23
  /**
27
24
  * Any basic javascript type accepted by this library.
28
25
  */
29
26
  export declare type BasicType = string | number | boolean | BigInt | null;
30
-
31
27
  /**
32
28
  * Any java type accepted by this library, except arrays.
33
29
  */
34
- export type BasicOrJavaType = BasicType | JavaObject | JavaConstructor;
35
-
30
+ export declare type BasicOrJavaType = BasicType | JavaObject | JavaClass | JavaClassType;
36
31
  /**
37
32
  * All types accepted by java
38
33
  */
39
- export type JavaType = BasicOrJavaType | BasicOrJavaType[];
40
-
34
+ export declare type JavaType = BasicOrJavaType | BasicOrJavaType[];
41
35
  /**
42
36
  * A dummy java object class
43
37
  */
44
- export abstract class JavaObject {}
45
-
38
+ export declare abstract class JavaObject {
39
+ }
46
40
  /**
47
41
  * A java class proxy class.
48
42
  * This only exists for temporarily storing
@@ -55,90 +49,71 @@ export declare class JavaClassProxy {
55
49
  /**
56
50
  * The class name
57
51
  */
58
- public 'class.name': string;
59
-
52
+ 'class.name': string;
60
53
  /**
61
54
  * Get the class's constructor
62
55
  *
63
56
  * @return the java instance proxy constructor
64
57
  */
65
- public getClassConstructor<
66
- T extends JavaClassType = JavaClassType
67
- >(): JavaConstructor<T>;
58
+ getClassConstructor<T extends JavaClassType = UnknownJavaClassType>(): T;
68
59
  }
69
-
70
- export type JavaClassType = typeof JavaClassInstance;
71
-
60
+ export declare type JavaClassType = typeof JavaClass;
61
+ export declare type UnknownJavaClassType = typeof UnknownJavaClass;
62
+ export declare type JavaClassConstructorType = typeof JavaClassConstructor;
72
63
  /**
73
- * A java class's constructor
64
+ * @inheritDoc UnknownJavaClass
74
65
  */
75
- export type JavaConstructor<T extends JavaClassType = JavaClassType> = T &
76
- ImportedMembers;
77
-
66
+ export declare class JavaClassInstance extends UnknownJavaClass {
67
+ }
78
68
  /**
79
- * Any class member imported from java
69
+ * A java class constructor class
70
+ *
71
+ * @see JavaClass
80
72
  */
81
- export interface ImportedMembers {
82
- /**
83
- * Get the java class instance
84
- */
85
- get class(): JavaClassInstance;
86
-
87
- /**
88
- * Any class member imported.
89
- * We'll need to use 'any' as any is callable.
90
- * The actual type would be JavaType | ((...args: JavaType[]) => JavaType | Promise<JavaType>)
91
- */
92
- [member: string]: any;
73
+ export declare class JavaClassConstructor extends JavaClass {
74
+ constructor(...args: BasicOrJavaType[]);
93
75
  }
94
-
95
76
  /**
96
77
  * A constructor type.
97
78
  */
98
- export type Constructor<T> = { new (): T };
99
-
79
+ export declare type Constructor<T> = {
80
+ new (): T;
81
+ };
100
82
  /**
101
- * The java instance proxy class.
102
- * This class actually does all the magic.
103
- * After it is created, this will just be a constructor
104
- * with all static methods and properties (the accessible ones)
105
- * stored in it and ready for use. Once the actual instance
106
- * using the new operator is created, a new
107
- * java_instance_proxy instance is created, containing
108
- * the actual java instance (that thing isn't visible though)
109
- * and all (visible) non-static class member methods and properties.
83
+ * A class to be extended for custom class definitions.
84
+ * This does not allow for any methods to be called if not
85
+ * defined in the class definition.
86
+ *
87
+ * ## Example
88
+ * ```ts
89
+ * import { importClass } from 'java-bridge';
90
+ *
91
+ * declare class PersonClass extends JavaClass {
92
+ * public constructor(name: string, age: number);
93
+ * public newInstanceAsync(name: string, age: number): Promise<Person>;
94
+ *
95
+ * public getName(): Promise<string>;
96
+ * public getNameSync(): string;
97
+ * public getAge(): Promise<number>;
98
+ * public getAgeSync(): number;
99
+ * }
100
+ *
101
+ * class Person extends importClass<typeof PersonClass>('com.test.Person') {}
102
+ *
103
+ * const person = new Person('John', 20);
104
+ * console.log(person.getNameSync()); // John
105
+ * console.log(person.getAgeSync()); // 20
106
+ * ```
110
107
  */
111
- export declare class JavaClassInstance extends JavaObject {
108
+ export declare class JavaClass extends JavaObject {
112
109
  /**
113
- * The class proxy class instance
114
- */
115
- public static readonly 'class.proxy': JavaClassProxy;
116
-
117
- /**
118
- * Create a new java class instance.
119
- * Async version.
120
- *
121
- * @template T the type of this class as a new instance of this class will be returned
122
- * @param args the arguments to create the instance
123
- * @return the java_instance_proxy instance
110
+ * Get the java class instance
124
111
  */
125
- public static newInstanceAsync(
126
- this: never,
127
- ...args: BasicOrJavaType[]
128
- ): Promise<unknown>;
129
- public static newInstanceAsync<T extends JavaClassInstance>(
130
- this: Constructor<T>,
131
- ...args: BasicOrJavaType[]
132
- ): Promise<T>;
133
-
112
+ static get class(): UnknownJavaClass;
134
113
  /**
135
- * Create a new java instance of type
136
- * java_instance_proxy["class.proxy.instance"]
137
- *
138
- * @param args the arguments to create the instance
114
+ * The class proxy class instance
139
115
  */
140
- public constructor(...args: BasicOrJavaType[]);
141
-
116
+ static readonly 'class.proxy': JavaClassProxy;
142
117
  /**
143
118
  * Check if this is an instance of another class.
144
119
  * Pass either the name of the other class or the class itself
@@ -162,10 +137,7 @@ export declare class JavaClassInstance extends JavaObject {
162
137
  * @param other the class to check if this is an instance of
163
138
  * @return true if this is instance of `other`
164
139
  */
165
- public instanceOf<T extends typeof JavaClassInstance>(
166
- other: string | T
167
- ): boolean;
168
-
140
+ instanceOf<T extends JavaClassConstructorType>(other: string | T): boolean;
169
141
  /**
170
142
  * Default java equals implementation.
171
143
  * Async call.
@@ -173,8 +145,7 @@ export declare class JavaClassInstance extends JavaObject {
173
145
  * @param o the object to compare this to
174
146
  * @returns true if this matches o
175
147
  */
176
- public equals(o: JavaClassInstance): Promise<boolean>;
177
-
148
+ equals(o: JavaClass): Promise<boolean>;
178
149
  /**
179
150
  * Default java equals implementation.
180
151
  * Sync call.
@@ -182,24 +153,51 @@ export declare class JavaClassInstance extends JavaObject {
182
153
  * @param o the object to compare this to
183
154
  * @returns true if this matches o
184
155
  */
185
- public equalsSync(o: JavaClassInstance): boolean;
186
-
156
+ equalsSync(o: JavaClass): boolean;
187
157
  /**
188
158
  * Java default toString method.
189
159
  * Async call.
190
160
  *
191
161
  * @returns this as a string
192
162
  */
193
- public toString(): Promise<string>;
194
-
163
+ toString(): Promise<string>;
195
164
  /**
196
165
  * Java default toString method.
197
166
  * Sync call.
198
167
  *
199
168
  * @returns this as a string
200
169
  */
201
- public toStringSync(): string;
202
-
170
+ toStringSync(): string;
171
+ }
172
+ /**
173
+ * The java instance proxy class.
174
+ * This class actually does all the magic.
175
+ * After it is created, this will just be a constructor
176
+ * with all static methods and properties (the accessible ones)
177
+ * stored in it and ready for use. Once the actual instance
178
+ * using the new operator is created, a new
179
+ * java_instance_proxy instance is created, containing
180
+ * the actual java instance (that thing isn't visible though)
181
+ * and all (visible) non-static class member methods and properties.
182
+ */
183
+ export declare class UnknownJavaClass extends JavaClass {
184
+ /**
185
+ * Create a new java class instance.
186
+ * Async version.
187
+ *
188
+ * @template T the type of this class as a new instance of this class will be returned
189
+ * @param args the arguments to create the instance
190
+ * @return the java_instance_proxy instance
191
+ */
192
+ static newInstanceAsync(this: never, ...args: BasicOrJavaType[]): Promise<unknown>;
193
+ static newInstanceAsync<T extends JavaClass>(this: Constructor<T>, ...args: BasicOrJavaType[]): Promise<T>;
194
+ /**
195
+ * Create a new java instance of type
196
+ * java_instance_proxy["class.proxy.instance"]
197
+ *
198
+ * @param args the arguments to create the instance
199
+ */
200
+ constructor(...args: BasicOrJavaType[]);
203
201
  /**
204
202
  * Any class member imported.
205
203
  * We'll need to use 'any' as any is callable.
@@ -207,4 +205,10 @@ export declare class JavaClassInstance extends JavaObject {
207
205
  * Just throwing it out there.
208
206
  */
209
207
  [member: string]: any;
208
+ /**
209
+ * Any static class member imported.
210
+ * We'll need to use `any` as `any` is callable.
211
+ * The actual type would be JavaType | ((...args: JavaType[]) => JavaType | Promise<JavaType>)
212
+ */
213
+ static [member: string]: any;
210
214
  }
@@ -0,0 +1,15 @@
1
+ export { JavaVersion, JavaObject, JavaClassInstance, JavaClassProxy, JavaClass, JavaClassConstructor, JavaType, BasicOrJavaType, BasicType, JavaClassType, Constructor, UnknownJavaClassType, JavaClassConstructorType, } from './definitions';
2
+ import type * as internal from '../native';
3
+ /**
4
+ * A namespace containing any internal type definitions.
5
+ * Do not actually use anything from this namespace
6
+ * as it only exports types.
7
+ */
8
+ export type { internal };
9
+ export * from './java';
10
+ import * as java from './java';
11
+ export default java;
12
+ export { getJavaLibPath } from '../native';
13
+ import TypescriptDefinitionGenerator from './TypescriptDefinitionGenerator';
14
+ export { TypescriptDefinitionGenerator };
15
+ export { ModuleDeclaration, MethodDeclaration, ProgressCallback, } from './TypescriptDefinitionGenerator';
@@ -1,30 +1,5 @@
1
- import {
2
- getClassFields,
3
- getField,
4
- getStaticField,
5
- Java,
6
- JavaOptions,
7
- setField,
8
- setStaticField,
9
- } from '../native';
10
- import {
11
- JavaClassInstance,
12
- JavaClassType,
13
- JavaConstructor,
14
- JavaVersion,
15
- } from './definitions';
16
- import { getJavaLibPath, getNativeLibPath } from './nativeLib';
17
-
18
- /**
19
- * The static java instance
20
- */
21
- let javaInstance: Java | null = null;
22
-
23
- interface ImportedJavaClass {
24
- 'class.proxy': object;
25
- new (...args: any[]): any;
26
- }
27
-
1
+ import { Java, JavaOptions } from '../native';
2
+ import { JavaClass, JavaClassConstructorType, JavaVersion, UnknownJavaClassType } from './definitions';
28
3
  /**
29
4
  * Options for creating the Java VM.
30
5
  */
@@ -42,7 +17,6 @@ export interface JVMOptions extends JavaOptions {
42
17
  */
43
18
  opts?: Array<string> | null;
44
19
  }
45
-
46
20
  /**
47
21
  * Ensure the java vm is created.
48
22
  * If the jvm is already created, this does nothing.
@@ -78,43 +52,7 @@ export interface JVMOptions extends JavaOptions {
78
52
  *
79
53
  * @param options the options to use when creating the jvm
80
54
  */
81
- export function ensureJvm(options?: JVMOptions): void {
82
- if (!javaInstance) {
83
- javaInstance = new Java(
84
- options?.libPath,
85
- options?.version,
86
- options?.opts,
87
- options,
88
- getJavaLibPath(),
89
- getNativeLibPath()
90
- );
91
- }
92
- }
93
-
94
- function defineFields(object: Record<string, any>, getStatic: boolean): void {
95
- for (const field of getClassFields(object['class.proxy'], getStatic)) {
96
- const getter = (): any =>
97
- getStatic
98
- ? getStaticField(object, field.name)
99
- : getField(object, field.name);
100
- if (field.isFinal) {
101
- Object.defineProperty(object, field.name, {
102
- get: getter,
103
- enumerable: true,
104
- });
105
- } else {
106
- Object.defineProperty(object, field.name, {
107
- get: getter,
108
- set: (value: any) =>
109
- getStatic
110
- ? setStaticField(object, field.name, value)
111
- : setField(object, field.name, value),
112
- enumerable: true,
113
- });
114
- }
115
- }
116
- }
117
-
55
+ export declare function ensureJvm(options?: JVMOptions): void;
118
56
  /**
119
57
  * Import a class.
120
58
  * Returns the constructor of the class to be created.
@@ -180,47 +118,11 @@ function defineFields(object: Record<string, any>, getStatic: boolean): void {
180
118
  * @param classname the name of the class to resolve
181
119
  * @return the java class constructor
182
120
  */
183
- export function importClass<T extends JavaClassType = JavaClassType>(
184
- classname: string
185
- ): JavaConstructor<T> {
186
- ensureJvm();
187
- const constructor = javaInstance!.importClass(
188
- classname
189
- ) as ImportedJavaClass;
190
- defineFields(constructor, true);
191
-
192
- constructor.constructor = function (...args: any[]) {
193
- const object = new constructor.prototype.constructor(...args);
194
- defineFields(object, false);
195
-
196
- return object;
197
- };
198
-
199
- return constructor as unknown as JavaConstructor<T>;
200
- }
201
-
121
+ export declare function importClass<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string): T;
202
122
  /**
203
123
  * @inheritDoc importClass
204
124
  */
205
- export async function importClassAsync<T extends JavaClassType = JavaClassType>(
206
- classname: string
207
- ): Promise<JavaConstructor<T>> {
208
- ensureJvm();
209
- const constructor = (await javaInstance!.importClassAsync(
210
- classname
211
- )) as ImportedJavaClass;
212
- defineFields(constructor, true);
213
-
214
- constructor.constructor = function (...args: any[]) {
215
- const object = new constructor.prototype.constructor(...args);
216
- defineFields(object, false);
217
-
218
- return object;
219
- };
220
-
221
- return constructor as unknown as JavaConstructor<T>;
222
- }
223
-
125
+ export declare function importClassAsync<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string): Promise<T>;
224
126
  /**
225
127
  * Append a single or multiple jars to the class path.
226
128
  *
@@ -248,15 +150,11 @@ export async function importClassAsync<T extends JavaClassType = JavaClassType>(
248
150
  *
249
151
  * @param path the path(s) to add
250
152
  */
251
- export function appendClasspath(path: string | string[]): void {
252
- ensureJvm();
253
- javaInstance!.appendClasspath(path);
254
- }
255
-
153
+ export declare function appendClasspath(path: string | string[]): void;
256
154
  /**
257
155
  * Check if `this_obj` is instance of `other`.
258
156
  * This uses the native java `instanceof` operator.
259
- * You may want to use this if {@link JavaClassInstance.instanceOf}
157
+ * You may want to use this if {@link JavaClass.instanceOf}
260
158
  * is overridden, as that method itself does not override
261
159
  * any method defined in the specific java class named 'instanceOf'.
262
160
  *
@@ -287,14 +185,7 @@ export function appendClasspath(path: string | string[]): void {
287
185
  * @param other the class or class name to check against
288
186
  * @return true if `this_obj` is an instance of `other`
289
187
  */
290
- export function isInstanceOf<T extends typeof JavaClassInstance>(
291
- this_obj: JavaClassInstance,
292
- other: string | T
293
- ): boolean {
294
- ensureJvm();
295
- return javaInstance!.isInstanceOf(this_obj, other);
296
- }
297
-
188
+ export declare function isInstanceOf<T extends JavaClassConstructorType>(this_obj: JavaClass, other: string | T): boolean;
298
189
  /**
299
190
  * Methods for altering and querying the class path.
300
191
  * @example
@@ -306,25 +197,18 @@ export function isInstanceOf<T extends typeof JavaClassInstance>(
306
197
  * assert.equal(classpath.get().length, 1);
307
198
  * assert.equal(classpath.get()[0], '/path/to/jar.jar');
308
199
  */
309
- export namespace classpath {
200
+ export declare namespace classpath {
310
201
  /**
311
202
  * @inheritDoc appendClasspath
312
203
  */
313
- export function append(path: string | string[]): void {
314
- appendClasspath(path);
315
- }
316
-
204
+ function append(path: string | string[]): void;
317
205
  /**
318
206
  * Get the loaded jars in the class path
319
207
  *
320
208
  * @returns a list of the loaded jars
321
209
  */
322
- export function get(): string[] {
323
- ensureJvm();
324
- return javaInstance!.loadedJars;
325
- }
210
+ function get(): string[];
326
211
  }
327
-
328
212
  /**
329
213
  * A callback for any output redirected from stdout/stderr from the java process.
330
214
  *
@@ -332,8 +216,7 @@ export namespace classpath {
332
216
  * This is null if the output was valid. This will probably never be set.
333
217
  * @param data the data that was converted. This is unset if <code>err</code> is set.
334
218
  */
335
- export type StdoutCallback = (err: Error | null, data?: string) => void;
336
-
219
+ export declare type StdoutCallback = (err: Error | null, data?: string) => void;
337
220
  /**
338
221
  * The class guarding the stdout redirect.
339
222
  * Keep this instance in scope to not lose the redirect.
@@ -381,7 +264,6 @@ export interface StdoutRedirectGuard {
381
264
  * @param callback the callback
382
265
  */
383
266
  on(event: 'stdout' | 'stderr', callback: StdoutCallback | null): void;
384
-
385
267
  /**
386
268
  * Reset this <code>StdoutRedirectGuard</code> instance.
387
269
  * After this call, the stdout/stderr will no longer
@@ -390,7 +272,6 @@ export interface StdoutRedirectGuard {
390
272
  */
391
273
  reset(): void;
392
274
  }
393
-
394
275
  /**
395
276
  * A namespace containing methods for redirecting the stdout/stderr of the java process.
396
277
  *
@@ -398,7 +279,7 @@ export interface StdoutRedirectGuard {
398
279
  * * {@link StdoutRedirectGuard}
399
280
  * * {@link stdout.enableRedirect}
400
281
  */
401
- export namespace stdout {
282
+ export declare namespace stdout {
402
283
  /**
403
284
  * Enable stdout/stderr redirection.
404
285
  *
@@ -452,15 +333,8 @@ export namespace stdout {
452
333
  * @param stderr the callback to be called when stderr is received
453
334
  * @returns a <code>StdoutRedirectGuard</code> instance. Keep this instance in scope to not lose the redirect.
454
335
  */
455
- export function enableRedirect(
456
- stdout?: StdoutCallback | null,
457
- stderr?: StdoutCallback | null
458
- ): StdoutRedirectGuard {
459
- ensureJvm();
460
- return javaInstance!.setStdoutCallbacks(stdout, stderr);
461
- }
336
+ function enableRedirect(stdout?: StdoutCallback | null, stderr?: StdoutCallback | null): StdoutRedirectGuard;
462
337
  }
463
-
464
338
  /**
465
339
  * The class for implementing java interfaces.
466
340
  * Keep this instance in scope to not destroy the java object.
@@ -505,7 +379,6 @@ export interface JavaInterfaceProxy {
505
379
  */
506
380
  reset(): void;
507
381
  }
508
-
509
382
  /**
510
383
  * An interface proxy method.
511
384
  * Any arguments passed to this method are values converted from java values.
@@ -514,11 +387,7 @@ export interface JavaInterfaceProxy {
514
387
  * @param args the arguments passed from the java process
515
388
  * @return the value to pass back to the java process
516
389
  */
517
- export type ProxyMethod = (...args: any[]) => any;
518
- type InternalProxyRecord = Parameters<
519
- typeof Java.prototype.createInterfaceProxy
520
- >[1];
521
-
390
+ export declare type ProxyMethod = (...args: any[]) => any;
522
391
  /**
523
392
  * Create a new java interface proxy.
524
393
  * This allows you to implement java interfaces in javascript.
@@ -626,46 +495,9 @@ type InternalProxyRecord = Parameters<
626
495
  * @param methods the methods to implement.
627
496
  * @returns a proxy class to pass back to the java process
628
497
  */
629
- export function newProxy(
630
- interfaceName: string,
631
- methods: Record<string, ProxyMethod>
632
- ): JavaInterfaceProxy {
633
- ensureJvm();
634
- const proxyMethods: InternalProxyRecord = Object.create(null);
635
-
636
- for (const [name, method] of Object.entries(methods)) {
637
- proxyMethods[name] = (
638
- err: null | Error,
639
- callback: (err: Error | null, data?: any | null) => void,
640
- ...args: any[]
641
- ): void => {
642
- if (err) {
643
- throw err;
644
- }
645
-
646
- try {
647
- const res = method(...args);
648
- callback(null, res);
649
- } catch (e: any) {
650
- if (e instanceof Error) {
651
- callback(e);
652
- } else {
653
- callback(new Error(e.toString()));
654
- }
655
- }
656
- };
657
- }
658
-
659
- return javaInstance!.createInterfaceProxy(
660
- interfaceName,
661
- proxyMethods
662
- ) as JavaInterfaceProxy;
663
- }
664
-
498
+ export declare function newProxy(interfaceName: string, methods: Record<string, ProxyMethod>): JavaInterfaceProxy;
665
499
  /**
666
500
  * Get the static java instance.
667
501
  * This has no real use, all important methods are exported explicitly.
668
502
  */
669
- export function getJavaInstance(): Java | null {
670
- return javaInstance;
671
- }
503
+ export declare function getJavaInstance(): Java | null;
@@ -0,0 +1,2 @@
1
+ export declare function getNativeLibPath(): string;
2
+ export declare function getJavaLibPath(): string;
@@ -0,0 +1 @@
1
+ export {};