java-bridge 2.1.3 → 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.
@@ -0,0 +1,81 @@
1
+ export interface MethodDeclaration {
2
+ returnType: string;
3
+ parameters: string[];
4
+ isStatic: boolean;
5
+ }
6
+ /**
7
+ * A java class declaration converted to typescript
8
+ */
9
+ export interface ModuleDeclaration {
10
+ /**
11
+ * The fully-qualified class name
12
+ */
13
+ name: string;
14
+ /**
15
+ * The generated typescript code
16
+ */
17
+ contents: string;
18
+ }
19
+ /**
20
+ * A TypescriptDefinitionGenerator progress callback method
21
+ */
22
+ export declare type ProgressCallback = (classname: string) => void;
23
+ /**
24
+ * A class to generate typescript definitions for java classes.
25
+ * Converts the given class and all of its dependencies to typescript.
26
+ *
27
+ * ## Example
28
+ * ```ts
29
+ * import { TypescriptDefinitionGenerator } from 'java-bridge';
30
+ *
31
+ * const generator = new TypescriptDefinitionGenerator('java.lang.String');
32
+ * // Generate the typescript definitions
33
+ * const definitions = await generator.generate();
34
+ *
35
+ * // Save the definitions to a directory
36
+ * await TypescriptDefinitionGenerator.save(definitions, './project');
37
+ * ```
38
+ */
39
+ export default class TypescriptDefinitionGenerator {
40
+ private readonly classname;
41
+ private readonly progressCallback;
42
+ private readonly resolvedImports;
43
+ private usesBasicOrJavaType;
44
+ private readonly additionalImports;
45
+ private readonly importsToResolve;
46
+ /**
47
+ * Create a new `TypescriptDefinitionGenerator` instance
48
+ *
49
+ * @param classname the fully-qualified name of the class to generate a typescript definition for
50
+ * @param progressCallback a callback method to be called every time a java class is
51
+ * converted to typescript
52
+ * @param resolvedImports a list of imports that have already been resolved.
53
+ * This is used to prevent converting a class multiple times
54
+ */
55
+ constructor(classname: string, progressCallback?: ProgressCallback | null, resolvedImports?: string[]);
56
+ private static convertMethods;
57
+ private convertConstructors;
58
+ private javaTypeToTypescriptType;
59
+ private convertParameter;
60
+ private convertParameters;
61
+ private static createMethodComment;
62
+ private createMethod;
63
+ private convertMethod;
64
+ private getAdditionalImports;
65
+ private getImports;
66
+ private getExportStatement;
67
+ private getText;
68
+ /**
69
+ * Generates the typescript definition for the given class.
70
+ *
71
+ * @returns the generated typescript definitions
72
+ */
73
+ generate(): Promise<ModuleDeclaration[]>;
74
+ /**
75
+ * Save the converted classes to the given directory.
76
+ *
77
+ * @param declarations the declarations to save
78
+ * @param sourceDir the directory to save the files to
79
+ */
80
+ static save(declarations: ModuleDeclaration[], sourceDir: string): Promise<void>;
81
+ }
@@ -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
  }
@@ -1,16 +1,4 @@
1
- export {
2
- JavaVersion,
3
- JavaObject,
4
- JavaClassInstance,
5
- JavaClassProxy,
6
- JavaType,
7
- JavaConstructor,
8
- BasicOrJavaType,
9
- BasicType,
10
- ImportedMembers,
11
- JavaClassType,
12
- Constructor,
13
- } from './definitions';
1
+ export { JavaVersion, JavaObject, JavaClassInstance, JavaClassProxy, JavaClass, JavaClassConstructor, JavaType, BasicOrJavaType, BasicType, JavaClassType, Constructor, UnknownJavaClassType, JavaClassConstructorType, } from './definitions';
14
2
  import type * as internal from '../native';
15
3
  /**
16
4
  * A namespace containing any internal type definitions.
@@ -24,8 +12,4 @@ export default java;
24
12
  export { getJavaLibPath } from '../native';
25
13
  import TypescriptDefinitionGenerator from './TypescriptDefinitionGenerator';
26
14
  export { TypescriptDefinitionGenerator };
27
- export {
28
- ModuleDeclaration,
29
- MethodDeclaration,
30
- ProgressCallback,
31
- } from './TypescriptDefinitionGenerator';
15
+ export { ModuleDeclaration, MethodDeclaration, ProgressCallback, } from './TypescriptDefinitionGenerator';