This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
128 lines (105 loc) · 3.48 KB
/
utils.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
const log = require('./log')('utils');
const HashSet = Java.type("java.util.HashSet");
const ArrayList = Java.type("java.util.ArrayList");
function getAllPropertyNames (obj) {
const proto = Object.getPrototypeOf(obj);
const inherited = (proto) ? getAllPropertyNames(proto) : [];
return [...new Set(Object.getOwnPropertyNames(obj).concat(inherited))];
}
let jsSetToJavaSet = function(set) {
let rv = new HashSet();
set.forEach(e => rv.add(e));
return rv;
}
let jsArrayToJavaSet = function (arr) {
let set = new HashSet();
for (let i of arr) {
set.add(i);
}
return set;
};
let jsArrayToJavaList = function (arr) {
let list = new ArrayList();
for (let i of arr) {
list.add(i);
}
return list;
}
let javaSetToJsArray = function(set) {
return Java.from(new ArrayList(set));
}
let javaSetToJsSet = function(set) {
return new Set(exports.javaSetToJsArray(set));
}
let randomUUID = () => Java.type("java.util.UUID").randomUUID();
let dumpObject = function (obj) {
try {
log.info("Dumping object...");
log.info(" typeof obj = {}", (typeof obj));
let isJavaObject = Java.isJavaObject(obj);
log.info(" Java.isJavaObject(obj) = {}", isJavaObject)
let isJavaType = Java.isType(obj);
log.info(" Java.isType(obj) = {}", isJavaType)
if (isJavaObject) {
if (isJavaType) {
log.info(" Java.typeName(obj) = {}", Java.typeName(obj));
} else {
log.info(" Java.typeName(obj.class) = {}", Java.typeName(obj.class));
if(Java.typeName(obj.class) == 'java.util.HashMap'){
log.info("Dumping contents...");
let keys = obj.keySet().toArray();
for (var key in keys) {
log.info(`${keys[key]}(${typeof keys[key]}) = ${obj.get(keys[key])}(${typeof obj.get(keys[key])})`);
if(typeof keys[key] === 'object') {
log.info("Dumping key...");
exports.dumpObject(keys[key]);
}
}
}
}
} else if (typeof obj === 'string'){
log.info("String value = " + obj);
}
log.info("getAllPropertyNames(obj) = {}", getAllPropertyNames(obj));
// log.info("obj.toString() = {}", obj.toString());
// log.info("JSON.stringify(obj) = {}", JSON.stringify(obj));
} catch(e) {
log.info("Failed to dump object: " + e.message);
}
}
let typeBySuffix = function(typeSuffix) {
return typeWithFallback(`org.eclipse.smarthome.${typeSuffix}`, `org.openhab.${typeSuffix}`);
}
let typeWithFallback = function(type, fallbackType) {
let rv;
try {
rv = Java.type(type);
if (rv === null) {
throw error("not found");
}
} catch(e) {
rv = Java.type(fallbackType);
}
return rv;
}
let isJsInstanceOfJava = function(instance, type) {
if(!Java.isType(type)) {
throw error("type is not a java class");
}
if(instance === null || instance === undefined || instance.class === null || instance.class === undefined) {
return false;
}
return type.class.isAssignableFrom(instance.class);
}
module.exports = {
jsSetToJavaSet,
jsArrayToJavaSet,
jsArrayToJavaList,
javaSetToJsArray,
javaSetToJsSet,
randomUUID,
dumpObject,
typeWithFallback,
typeBySuffix,
isJsInstanceOfJava
}