Debian Codegraph @

native/common/include/jp_javaframe.h

raw symbols SCIP
1 /*****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 *****************************************************************************/
16 #ifndef _JP_JAVA_FRAME_H_
17 #define _JP_JAVA_FRAME_H_
18
19 /** A Java Frame represents a memory managed scope in which
20 * java objects can be manipulated.
21 *
22 * Any resources created within a Java frame will be automatically
23 * deallocated at when the frame falls out of scope unless captured
24 * by a global reference.
25 *
26 * This should be used around all entry points from python that
27 * call java code. Failure may lead to local references not being
28 * released. Methods will large numbers of local references
29 * should allocate a local frame. At most one local reference
30 * from a local frame can be kept. Addition must use global referencing.
31 *
32 * JavaFrames are created to hold a certain number of items in
33 * scope at time. They will grow automatically if more items are
34 * created, but this has overhead. For most cases the default
35 * will be fine. However, when working with an array in which
36 * the number of items in scope can be known in advance, it is
37 * good to size the frame appropriately.
38 *
39 * A JavaFrame should not be used in a destructor as it can
40 * throw. The most common use of JavaFrame is to delete a
41 * global reference. See the ReleaseGlobalReference for
42 * this purpose.
43 */
44 static const int LOCAL_FRAME_DEFAULT = 8;
45
46 class JPJavaFrame
47 {
48 JNIEnv* m_Env;
49 bool m_Popped;
50 bool m_Outer;
51
52 private:
53 JPJavaFrame(JNIEnv* env, int size, bool outer);
54
55 public:
56
57 /** Create a new JavaFrame when called from Python.
58 *
59 * This method will automatically attach the thread
60 * if it is not already attached.
61 *
62 * @param size determines how many objects can be
63 * created in this scope without additional overhead.
64 *
65 * @throws JPypeException if the jpype cannot
66 * acquire an env handle to work with jvm.
67 */
68 static JPJavaFrame outer(int size = LOCAL_FRAME_DEFAULT)
69 {
70 return {nullptr, size, true};
71 }
72
73 /** Create a new JavaFrame when called internal when
74 * there is an existing frame.
75 *
76 * @param size determines how many objects can be
77 * created in this scope without additional overhead.
78 *
79 * @throws JPypeException if the jpype cannot
80 * acquire an env handle to work with jvm.
81 */
82 static JPJavaFrame inner(int size = LOCAL_FRAME_DEFAULT)
83 {
84 return {nullptr, size, false};
85 }
86
87 /** Create a new JavaFrame when called from Java.
88 *
89 * The thread was attached by definition.
90 *
91 * @param size determines how many objects can be
92 * created in this scope without additional overhead.
93 *
94 * @throws JPypeException if the jpype cannot
95 * acquire an env handle to work with jvm.
96 */
97 static JPJavaFrame external(JNIEnv* env, int size = LOCAL_FRAME_DEFAULT)
98 {
99 return {env, size, false};
100 }
101
102 JPJavaFrame(const JPJavaFrame& frame);
103
104 /** Exit the local scope and clean up all java
105 * objects.
106 *
107 * Only the one local object passed to outer scope
108 * by the keep method will be kept alive.
109 */
110 ~JPJavaFrame();
111
112 JPContext* getContext();
113
114 void check();
115
116 /** Exit the local frame and keep a local reference to an object
117 *
118 * This must be called only once when the frame is about to leave
119 * scope. Any local references other than the one that is kept
120 * are destroyed. If the next line is not "return", you are using
121 * this incorrectly.
122 *
123 * Further calls to the frame will still suceed as we do not
124 * check for operation on a closed frame, but is not advised.
125 */
126 jobject keep(jobject);
127
128 /** Create a new global reference to a java object.
129 *
130 * This java reference may be held in a class until the object is
131 * no longer needed. It should be deleted with DeleteGlobalRef
132 * or ReleaseGlobalRef.
133 */
134 jobject NewGlobalRef(jobject obj);
135
136 /** Delete a global reference.
137 */
138 void DeleteGlobalRef(jobject obj);
139
140 /** Create a new local reference.
141 *
142 * This is only used when promoting a WeakReference to
143 * a local reference.
144 */
145 jobject NewLocalRef(jobject obj);
146
147 /** Prematurely delete a local reference.
148 *
149 * This is used when processing an array to keep
150 * the objects in scope from growing.
151 */
152 void DeleteLocalRef(jobject obj);
153
154 jweak NewWeakGlobalRef(jobject obj);
155 void DeleteWeakGlobalRef(jweak obj);
156
157 JNIEnv* getEnv() const
158 {
159 return m_Env;
160 }
161
162 string toString(jobject o);
163 string toStringUTF8(jstring str);
164
165 bool equals(jobject o1, jobject o2);
166 jint hashCode(jobject o);
167 jobject collectRectangular(jarray obj);
168 jobject assemble(jobject dims, jobject parts);
169
170 jobject newArrayInstance(jclass c, jintArray dims);
171 jthrowable getCause(jthrowable th);
172 jstring getMessage(jthrowable th);
173 jint compareTo(jobject obj, jobject obj2);
174
175 /**
176 * Convert a UTF8 encoded string into Java.
177 *
178 * This returns a local reference.
179 * @param str
180 * @return
181 */
182 jstring fromStringUTF8(const string& str);
183 jobject callMethod(jobject method, jobject obj, jobject args);
184 jobject toCharArray(jstring jstr);
185 string getFunctional(jclass c);
186
187 JPClass *findClass(jclass obj);
188 JPClass *findClassByName(const string& name);
189 JPClass *findClassForObject(jobject obj);
190
191 // not implemented
192 JPJavaFrame& operator= (const JPJavaFrame& frame) = delete;
193
194 private:
195 jint PushLocalFrame(jint);
196 jobject PopLocalFrame(jobject);
197
198 public:
199
200 bool ExceptionCheck();
201 void ExceptionDescribe();
202 void ExceptionClear();
203 jthrowable ExceptionOccurred();
204
205 jint ThrowNew(jclass clazz, const char* msg);
206
207 jint Throw(jthrowable th);
208
209 jobject NewDirectByteBuffer(void* address, jlong capacity);
210
211 /** NewObjectA */
212 jobject NewObjectA(jclass a0, jmethodID a1, jvalue* a2);
213
214 // Monitor
215 int MonitorEnter(jobject a0);
216 int MonitorExit(jobject a0);
217
218 jclass FindClass(const string& a0);
219 jclass DefineClass(const char* a0, jobject a1, const jbyte* a2, jsize a3);
220 jint RegisterNatives(jclass a0, const JNINativeMethod* a1, jint a2);
221
222 jboolean IsInstanceOf(jobject a0, jclass a1);
223 jboolean IsAssignableFrom(jclass a0, jclass a1);
224
225 jsize GetArrayLength(jarray a0);
226 jobject GetObjectArrayElement(jobjectArray a0, jsize a1);
227
228 jfieldID FromReflectedField(jobject a0);
229 jfieldID GetFieldID(jclass a0, const char* a1, const char* a2);
230 jfieldID GetStaticFieldID(jclass a0, const char* a1, const char* a2);
231
232 jmethodID FromReflectedMethod(jobject a0);
233 jmethodID GetMethodID(jclass a0, const char* a1, const char* a2);
234 jmethodID GetStaticMethodID(jclass a0, const char* a1, const char* a2);
235
236 // Void
237 void CallStaticVoidMethodA(jclass a0, jmethodID a1, jvalue* a2);
238 void CallVoidMethodA(jobject a0, jmethodID a1, jvalue* a2);
239 void CallNonvirtualVoidMethodA(jobject a0, jclass a1, jmethodID a2, jvalue* a3);
240
241 // Bool
242 jboolean GetStaticBooleanField(jclass clazz, jfieldID fid);
243 jboolean GetBooleanField(jobject clazz, jfieldID fid);
244 void SetStaticBooleanField(jclass clazz, jfieldID fid, jboolean val);
245 void SetBooleanField(jobject clazz, jfieldID fid, jboolean val);
246 jboolean CallStaticBooleanMethodA(jclass clazz, jmethodID mid, jvalue* val);
247 jboolean CallBooleanMethodA(jobject obj, jmethodID mid, jvalue* val);
248 jboolean CallNonvirtualBooleanMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
249 jbooleanArray NewBooleanArray(jsize len);
250 void SetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, jboolean* vals);
251 void GetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, jboolean* vals);
252 jboolean* GetBooleanArrayElements(jbooleanArray array, jboolean* isCopy);
253 void ReleaseBooleanArrayElements(jbooleanArray, jboolean* v, jint mode);
254
255 // Byte
256 jbyte GetStaticByteField(jclass clazz, jfieldID fid);
257 jbyte GetByteField(jobject clazz, jfieldID fid);
258 void SetStaticByteField(jclass clazz, jfieldID fid, jbyte val);
259 void SetByteField(jobject clazz, jfieldID fid, jbyte val);
260 jbyte CallStaticByteMethodA(jclass clazz, jmethodID mid, jvalue* val);
261 jbyte CallByteMethodA(jobject obj, jmethodID mid, jvalue* val);
262 jbyte CallNonvirtualByteMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
263 jbyteArray NewByteArray(jsize len);
264 void SetByteArrayRegion(jbyteArray array, jsize start, jsize len, jbyte* vals);
265 void GetByteArrayRegion(jbyteArray array, jsize start, jsize len, jbyte* vals);
266 jbyte* GetByteArrayElements(jbyteArray array, jboolean* isCopy);
267 void ReleaseByteArrayElements(jbyteArray, jbyte* v, jint mode);
268
269 // Char
270 jchar GetStaticCharField(jclass clazz, jfieldID fid);
271 jchar GetCharField(jobject clazz, jfieldID fid);
272 void SetStaticCharField(jclass clazz, jfieldID fid, jchar val);
273 void SetCharField(jobject clazz, jfieldID fid, jchar val);
274 jchar CallStaticCharMethodA(jclass clazz, jmethodID mid, jvalue* val);
275 jchar CallCharMethodA(jobject obj, jmethodID mid, jvalue* val);
276 jchar CallNonvirtualCharMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
277 jcharArray NewCharArray(jsize len);
278 void SetCharArrayRegion(jcharArray array, jsize start, jsize len, jchar* vals);
279 void GetCharArrayRegion(jcharArray array, jsize start, jsize len, jchar* vals);
280 jchar* GetCharArrayElements(jcharArray array, jboolean* isCopy);
281 void ReleaseCharArrayElements(jcharArray, jchar* v, jint mode);
282
283 // Short
284 jshort GetStaticShortField(jclass clazz, jfieldID fid);
285 jshort GetShortField(jobject clazz, jfieldID fid);
286 void SetStaticShortField(jclass clazz, jfieldID fid, jshort val);
287 void SetShortField(jobject clazz, jfieldID fid, jshort val);
288 jshort CallStaticShortMethodA(jclass clazz, jmethodID mid, jvalue* val);
289 jshort CallShortMethodA(jobject obj, jmethodID mid, jvalue* val);
290 jshort CallNonvirtualShortMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
291 jshortArray NewShortArray(jsize len);
292 void SetShortArrayRegion(jshortArray array, jsize start, jsize len, jshort* vals);
293 void GetShortArrayRegion(jshortArray array, jsize start, jsize len, jshort* vals);
294 jshort* GetShortArrayElements(jshortArray array, jboolean* isCopy);
295 void ReleaseShortArrayElements(jshortArray, jshort* v, jint mode);
296
297 // Integer
298 jint GetStaticIntField(jclass clazz, jfieldID fid);
299 jint GetIntField(jobject clazz, jfieldID fid);
300 void SetStaticIntField(jclass clazz, jfieldID fid, jint val);
301 void SetIntField(jobject clazz, jfieldID fid, jint val);
302 jint CallStaticIntMethodA(jclass clazz, jmethodID mid, jvalue* val);
303 jint CallIntMethodA(jobject obj, jmethodID mid, jvalue* val);
304 jint CallNonvirtualIntMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
305 jintArray NewIntArray(jsize len);
306 void SetIntArrayRegion(jintArray array, jsize start, jsize len, jint* vals);
307 void GetIntArrayRegion(jintArray array, jsize start, jsize len, jint* vals);
308 jint* GetIntArrayElements(jintArray array, jboolean* isCopy);
309 void ReleaseIntArrayElements(jintArray, jint* v, jint mode);
310
311 // Long
312 jlong GetStaticLongField(jclass clazz, jfieldID fid);
313 jlong GetLongField(jobject clazz, jfieldID fid);
314 void SetStaticLongField(jclass clazz, jfieldID fid, jlong val);
315 void SetLongField(jobject clazz, jfieldID fid, jlong val);
316 jlong CallStaticLongMethodA(jclass clazz, jmethodID mid, jvalue* val);
317 jlong CallLongMethodA(jobject obj, jmethodID mid, jvalue* val);
318 jlong CallNonvirtualLongMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
319 jfloat GetStaticFloatField(jclass clazz, jfieldID fid);
320 jlongArray NewLongArray(jsize len);
321 void SetLongArrayRegion(jlongArray array, jsize start, jsize len, jlong* vals);
322 void GetLongArrayRegion(jlongArray array, jsize start, jsize len, jlong* vals);
323 jlong* GetLongArrayElements(jlongArray array, jboolean* isCopy);
324 void ReleaseLongArrayElements(jlongArray, jlong* v, jint mode);
325
326 // Float
327 jfloat GetFloatField(jobject clazz, jfieldID fid);
328 void SetStaticFloatField(jclass clazz, jfieldID fid, jfloat val);
329 void SetFloatField(jobject clazz, jfieldID fid, jfloat val);
330 jfloat CallStaticFloatMethodA(jclass clazz, jmethodID mid, jvalue* val);
331 jfloat CallFloatMethodA(jobject obj, jmethodID mid, jvalue* val);
332 jfloat CallNonvirtualFloatMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
333 jfloatArray NewFloatArray(jsize len);
334 void SetFloatArrayRegion(jfloatArray array, jsize start, jsize len, jfloat* vals);
335 void GetFloatArrayRegion(jfloatArray array, jsize start, jsize len, jfloat* vals);
336 jfloat* GetFloatArrayElements(jfloatArray array, jboolean* isCopy);
337 void ReleaseFloatArrayElements(jfloatArray, jfloat* v, jint mode);
338
339 // Double
340 jdouble GetStaticDoubleField(jclass clazz, jfieldID fid);
341 jdouble GetDoubleField(jobject clazz, jfieldID fid);
342 void SetStaticDoubleField(jclass clazz, jfieldID fid, jdouble val);
343 void SetDoubleField(jobject clazz, jfieldID fid, jdouble val);
344 jdouble CallStaticDoubleMethodA(jclass clazz, jmethodID mid, jvalue* val);
345 jdouble CallDoubleMethodA(jobject obj, jmethodID mid, jvalue* val);
346 jdouble CallNonvirtualDoubleMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
347 jdoubleArray NewDoubleArray(jsize len);
348 void SetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, jdouble* vals);
349 void GetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, jdouble* vals);
350 jdouble* GetDoubleArrayElements(jdoubleArray array, jboolean* isCopy);
351 void ReleaseDoubleArrayElements(jdoubleArray, jdouble* v, jint mode);
352
353 // Object
354 jclass GetObjectClass(jobject obj);
355 jobject GetStaticObjectField(jclass clazz, jfieldID fid);
356 jobject GetObjectField(jobject clazz, jfieldID fid);
357 void SetStaticObjectField(jclass clazz, jfieldID fid, jobject val);
358 void SetObjectField(jobject clazz, jfieldID fid, jobject val);
359 jobject CallStaticObjectMethodA(jclass clazz, jmethodID mid, jvalue* val);
360 jobject CallObjectMethodA(jobject obj, jmethodID mid, jvalue* val);
361 jobject CallNonvirtualObjectMethodA(jobject obj, jclass claz, jmethodID mid, jvalue* val);
362 jobjectArray NewObjectArray(jsize a0, jclass a1, jobject a2);
363 void SetObjectArrayElement(jobjectArray a0, jsize a1, jobject a2);
364
365 // String
366 jstring NewStringUTF(const char* a0);
367
368 void* GetDirectBufferAddress(jobject obj);
369 jlong GetDirectBufferCapacity(jobject obj);
370 jboolean isBufferReadOnly(jobject obj);
371 jobject asReadOnlyBuffer(jobject obj);
372 jboolean orderBuffer(jobject obj);
373 jclass getClass(jobject obj);
374
375 /** This returns a UTF16 surogate coded UTF-8 string.
376 */
377 const char* GetStringUTFChars(jstring a0, jboolean* a1);
378 void ReleaseStringUTFChars(jstring a0, const char* a1);
379 jsize GetStringUTFLength(jstring a0);
380
381 jboolean isPackage(const string& str);
382 jobject getPackage(const string& str);
383 jobject getPackageObject(jobject pkg, const string& str);
384 jarray getPackageContents(jobject pkg);
385
386 void newWrapper(JPClass* cls);
387 void registerRef(jobject obj, PyObject* hostRef);
388 void registerRef(jobject obj, void* ref, JCleanupHook cleanup);
389
390 void clearInterrupt(bool throws);
391
392 } ;
393
394 #endif // _JP_JAVA_FRAME_H_