-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjvm.js
188 lines (147 loc) · 4.17 KB
/
jvm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.JVM = void 0;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* JVM Wrapper
* @private
*/
class JVM {
/**
* Reflection된 Java Class 보관소
* @type {Object.<string, Object>}
* @private
*/
/**
* 시스템에서 사용하는 JVM Instance
*/
/**
* 불러온 패키지 목록.
*/
/**
* Node-java에서 사용하는 JVM interface
* @private
* @type {Object}
*/
static init(java, packages) {
if (typeof java === 'undefined' || java === null || JVM.java) throw TypeError('java는 null일 수 없고, JVM.init()은 한 번만 실행되어야 합니다.');
JVM.PACKAGES = packages;
JVM.java = java;
}
/**
* Java class 반환
* @param {!string} path Class package path
* @returns {Object} Java class
*/
static classOf(...path) {
let clsName = path.join('.');
if (!JVM._classes.hasOwnProperty(clsName)) JVM._classes[clsName] = JVM.java.import(clsName);
return JVM._classes[clsName];
}
/**
* Koala Class 반환
* @param {!string} path Class package path
* @returns {Object} Java class
*/
static koalaClassOf(...path) {
return JVM.classOf('kr.bydelta.koala', ...path);
}
/**
* Koala Enum 반환
* @param {!string} tagset 표지자 집합 클래스 이름
* @param {?string} tag 표지자 이름
* @returns {?Object} 표지자 이름에 맞는 Java Enum
*/
static koalaEnumOf(tagset, tag) {
if (tag !== null && typeof tag !== 'undefined') return JVM.koalaClassOf(tagset).valueOf(tag);else return null;
}
/**
* Java Iterable -> JSON Array
* @param {Object} array
* @param itemConverter
* @returns {Array}
*/
static toJsArray(array, itemConverter = x => x, isIterable = false) {
if (typeof array === "undefined" || array === null) return [];
if (Array.isArray(array)) return array.map(itemConverter);
let result = [];
if (isIterable) {
let it = array.iterator();
while (it.hasNext()) {
result.push(itemConverter(it.next()));
}
} else {
let length = array.size();
for (let i = 0; i < length; ++i) {
result.push(itemConverter(array.get(i)));
}
}
return result;
}
/**
* JSON Array -> Java List
* @param {Object[]} array
*/
static listOf(array) {
let list = new (JVM.classOf('java.util.ArrayList'))();
for (const item of array) list.add(item);
return list;
}
/**
* Make Java Pair
* @param {Object} a First entry
* @param {Object} b Second entry
*/
static pair(a, b) {
return new (JVM.classOf('kotlin.Pair'))(a, b);
}
/**
* Make Java Char
* @param {?string} ch Character
* @returns {Object} Java Character
*/
static char(ch) {
if (ch !== null && typeof ch !== 'undefined') return JVM.java.newChar(ch.charCodeAt(0));else return null;
}
/**
* Make Java Set
* @param {Object[]} array Items
* @returns {Object} Java Set
*/
static setOf(array) {
let list = new (JVM.classOf('java.util.HashSet'))();
for (const item of array) list.add(item);
return list;
}
static posFilter(posSet) {
return JVM.java.newProxy('kotlin.jvm.functions.Function1', {
'invoke': function (tag) {
return posSet.includes(tag.name());
}
});
}
/**
* Check whether these packages are compatible.
* @param {Object.<string, string>} packages
*/
static canLoadPackages(packages) {
let result = {};
if (JVM.java && JVM.java.isJvmCreated()) {
for (let [pack, ver] of Object.entries(packages)) {
result[pack] = JVM.PACKAGES.hasOwnProperty(pack) && (ver.toUpperCase() === 'LATEST' || JVM.PACKAGES[pack] >= ver);
}
} else {
for (let pack of Object.keys(packages)) {
result[pack] = true;
}
}
return result;
}
}
exports.JVM = JVM;
_defineProperty(JVM, "_classes", {});
_defineProperty(JVM, "INSTANCE", void 0);
_defineProperty(JVM, "PACKAGES", void 0);
_defineProperty(JVM, "java", void 0);