This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathjava.lcb
More file actions
248 lines (191 loc) · 7.05 KB
/
java.lcb
File metadata and controls
248 lines (191 loc) · 7.05 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* Copyright (C) 2016 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/**
This module provides utility handlers for converting to and from Java types.
Description:
> **Important:** This library is currently supported on Android, Mac and
> Linux. Binding to java classes requires the availability of a Java
> runtime and access to the appropriate libraries. On Mac,
> the `JAVA_HOME` environment variable must be set to the path to your
> Java installation (usually at
> `/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home`).
> On Linux, your `LD_LIBRARY_PATH` must be set to the folder containing
> the `libjvm.so` library (usually at `${JAVA_HOME}/jre/lib/amd64/server`
> on 64-bit Linux).
*/
module com.livecode.java
use com.livecode.foreign
public type JBoolean is Bool
public type JByte is Int8
public type JChar is UInt16
public type JShort is Int16
public type JInt is Int32
public type JLong is Int64
public type JFloat is Float32
public type JDouble is Float64
public foreign type JObject binds to "MCJavaObjectTypeInfo"
public type JString is JObject
public type JByteArray is JObject
foreign handler MCJavaStringFromJString(in pString as JObject, out rString as String) returns nothing binds to "<builtin>"
foreign handler MCJavaStringToJString(in pString as String, out rString as JObject) returns nothing binds to "<builtin>"
foreign handler MCJavaDataFromJByteArray(in pByteArray as JObject, out rData as Data) returns nothing binds to "<builtin>"
foreign handler MCJavaDataToJByteArray(in pData as Data, out rByteArray as JObject) returns nothing binds to "<builtin>"
foreign handler MCJavaGetClassName(in pObject as JObject, out rName as String) returns nothing binds to "<builtin>"
foreign handler MCJavaUnwrapJObject(in pObject as JObject, out rPointer as Pointer) returns nothing binds to "<builtin>"
foreign handler MCJavaWrapJObject(in pPointer as Pointer, out rObj as JObject) returns nothing binds to "<builtin>"
/**
Summary: Get Java class name of a Java object
Parameters:
pObj: A JObject
Example:
foreign handler CreateJavaObject() returns JObject binds to "java:java.lang.Object>new()"
public handler GetNewJavaObject() returns JObject
variable tObj as JObject
unsafe
put CreateJavaObject() into tObj
variable tClassName as String
put GetJavaClassName(pObj) into tClassName
-- tClassName contains "java.lang.Object"
end unsafe
return tObj
end handler
Description:
Use <GetJavaClassName> to find out what class a given Java object is an
instance of.
*/
public handler GetJavaClassName(in pObj as JObject) returns String
variable tString as String
unsafe
MCJavaGetClassName(pObj, tString)
end unsafe
return tString
end handler
/**
Summary: Convert a java string into a String
Parameters:
pObj: The JString to convert
Example:
foreign handler JavaGetDefaultLocale() returns JObject binds to "java:java.util.Locale>getDefault()Ljava/util/Locale;!static"
foreign handler JavaLocaleDisplayName(in pLocale as JObject) returns JObject binds to "java:java.util.Locale>getDisplayName()Ljava/lang/String;"
public handler GetDefaultLocaleDisplayName() returns String
unsafe
variable tLocale as JObject
put JavaGetDefaultLocale() into tLocale
variable tDisplay as JString
put JavaLocaleDisplayName(tLocale) into tDisplay
return StringFromJString(tDisplay)
end unsafe
end handler
Description:
Use <StringFromJString> to convert an instance of the class java.lang.String
to a variable of type String.
*/
public handler StringFromJString(in pObj as JString) returns String
variable tString as String
unsafe
MCJavaStringFromJString(pObj, tString)
end unsafe
return tString
end handler
/**
Summary: Convert a String into a java string
Parameters:
pString: The String to convert
Returns:
A JObject of type java.lang.String
Example:
foreign handler CreateJavaCurrencyWithCode(in pString as JString) returns JObject binds to "java:java.util.Currency>getInstance(Ljava/lang/String;)Ljava/util/Currency;!static"
-- Create a new Currency object
variable tCurrency as JObject
unsafe
variable tCode as JString
put StringToJString("AMD") into tCode
put CreateJavaCurrencyWithCode(tCode) into tCurrency
end unsafe
Description:
Use <StringToJString> to convert a variable of type String to an instance
of the class java.lang.String.
*/
public handler StringToJString(in pString as String) returns JString
variable tString as JString
unsafe
MCJavaStringToJString(pString, tString)
end unsafe
return tString
end handler
public handler DataToJByteArray(in pData as Data) returns JByteArray
variable tBytes as JByteArray
unsafe
MCJavaDataToJByteArray(pData, tBytes)
end unsafe
return tBytes
end handler
public handler DataFromJByteArray(in pBytes as JByteArray) returns Data
variable tData as Data
unsafe
MCJavaDataFromJByteArray(pBytes, tData)
end unsafe
return tData
end handler
/**
Summary: Convert a JObject into a Pointer
Parameters:
pObj: The JObject to convert
Returns:
The jobject Pointer wrapped by the JObject type
Example:
handler SetNativeLayerToView(in pView as JObject)
variable tViewPtr as Pointer
put PointerFromJObject(pView) into tViewPtr
set my native layer to tViewPtr
end handler
Description:
Use <PointerFromJObject> to convert a variable of type JObject to one of
type Pointer, i.e. to extract the underlying jobject pointer from a JObject
*/
public handler PointerFromJObject(in pObj as JObject) returns Pointer
variable tPointer as Pointer
unsafe
MCJavaUnwrapJObject(pObj, tPointer)
end unsafe
return tPointer
end handler
/**
Summary: Convert a Pointer into a JObject
Parameters:
pPointer: The Pointer to convert
Returns:
A JObject wrapping the jobject Pointer
Example:
foreign handler _JNI_SetTextViewText(in pView as JObject, in pValue as JString) returns nothing binds to "java:android.widget.TextView>setText(Ljava/lang/CharSequence;)V"
-- set the text of a view
unsafe handler ViewSetText(in pString as String)
variable tViewPtr as Pointer
put my native layer into tPointer
variable tView as JObject
put PointerToJObject into tView
_JNI_SetTextViewText(tView, StringToJString(pString))
end handler
Description:
Use <PointerToJObject> to convert a variable of type Pointer to one of
type JObject.
> *Important:* Your application will likely crash if the underlying type
> of the Pointer is not actually jobject.
*/
public handler PointerToJObject(in pPointer as Pointer) returns JObject
variable tObj as JObject
unsafe
MCJavaWrapJObject(pPointer, tObj)
end unsafe
return tObj
end handler
end module