libcommonc++  0.7
JavaContext.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_JavaContext_hxx
24 #define __ccxx_JavaContext_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/Exception.h++>
29 #include <commonc++/String.h++>
30 
31 #include <jni.h>
32 #include <cstdarg>
33 
34 namespace ccxx {
35 
36 class JavaVirtualMachine; // fwd decl
37 
44 {
47 
49  void* function;
50 };
51 
68 {
69  friend class JavaVirtualMachine;
70 
71  public:
72 
78  JavaContext(JNIEnv* env);
79 
81  ~JavaContext();
82 
90  static String encodeType(const String& type);
91 
108  static bool parseSignature(const String& signature, String& method,
109  String& descriptor, bool& isStatic);
110 
120  jclass findClass(const String& name);
121 
130  jclass defineClass(const String& name, const byte_t* buf, size_t length);
131 
144  jmethodID findMethod(jclass clazz, const String& signature);
145 
154  jobject createObject(jclass clazz, jmethodID constructor, ...);
155 
165  jobject createObjectVA(jclass clazz, jmethodID constructor, va_list args);
166 
175  void callVoidMethod(jobject object, jmethodID method, ...);
176 
186  jobject callObjectMethod(jobject object, jmethodID method, ...);
187 
197  String callStringMethod(jobject object, jmethodID method, ...);
198 
208  bool callBooleanMethod(jobject object, jmethodID method, ...);
209 
219  byte_t callByteMethod(jobject object, jmethodID method, ...);
220 
230  char16_t callCharMethod(jobject object, jmethodID method, ...);
231 
241  int16_t callShortMethod(jobject object, jmethodID method, ...);
242 
252  int32_t callIntMethod(jobject object, jmethodID method, ...);
253 
263  int64_t callLongMethod(jobject object, jmethodID method, ...);
264 
274  float callFloatMethod(jobject object, jmethodID method, ...);
275 
285  double callDoubleMethod(jobject object, jmethodID method, ...);
286 
296  void callNonvirtualVoidMethod(jobject object, jclass clazz, jmethodID method,
297  ...);
298 
309  jobject callNonvirtualObjectMethod(jobject object, jclass clazz,
310  jmethodID method, ...);
311 
322  bool callNonvirtualBooleanMethod(jobject object, jclass clazz,
323  jmethodID method, ...);
324 
335  byte_t callNonvirtualByteMethod(jobject object, jclass clazz,
336  jmethodID method, ...);
337 
348  char16_t callNonvirtualCharMethod(jobject object, jclass clazz,
349  jmethodID method, ...);
350 
361  int16_t callNonvirtualShortMethod(jobject object, jclass clazz,
362  jmethodID method, ...);
363 
374  int32_t callNonvirtualIntMethod(jobject object, jclass clazz,
375  jmethodID method, ...);
376 
387  int64_t callNonvirtualLongMethod(jobject object, jclass clazz,
388  jmethodID method, ...);
389 
400  float callNonvirtualFloatMethod(jobject object, jclass clazz,
401  jmethodID method, ...);
402 
413  double callNonvirtualDoubleMethod(jobject object, jclass clazz,
414  jmethodID method, ...);
415 
425  void callStaticVoidMethod(jclass clazz, jmethodID method, ...);
426 
436  jobject callStaticObjectMethod(jclass clazz, jmethodID method, ...);
437 
447  String callStaticStringMethod(jclass clazz, jmethodID method, ...);
448 
458  bool callStaticBooleanMethod(jclass clazz, jmethodID method, ...);
459 
469  byte_t callStaticByteMethod(jclass clazz, jmethodID method, ...);
470 
480  char16_t callStaticCharMethod(jclass clazz, jmethodID method, ...);
481 
491  int16_t callStaticShortMethod(jclass clazz, jmethodID method, ...);
492 
502  int32_t callStaticIntMethod(jclass clazz, jmethodID method, ...);
503 
513  int64_t callStaticLongMethod(jclass clazz, jmethodID method, ...);
514 
524  float callStaticFloatMethod(jclass clazz, jmethodID method, ...);
525 
535  double callStaticDoubleMethod(jclass clazz, jmethodID method, ...);
536 
545  bool checkException(bool verbose = false);
546 
551  void clearException();
552 
559  jthrowable getException();
560 
568  bool throwException(jclass exception, const String& message);
569 
580  jfieldID findField(jclass clazz, const String& signature);
581 
590  jobject getObjectField(jobject object, jfieldID field);
591 
600  bool getBooleanField(jobject object, jfieldID field);
601 
610  byte_t getByteField(jobject object, jfieldID field);
611 
620  char16_t getCharField(jobject object, jfieldID field);
621 
630  int16_t getShortField(jobject object, jfieldID field);
631 
640  int32_t getIntField(jobject object, jfieldID field);
641 
650  int64_t getLongField(jobject object, jfieldID field);
651 
660  float getFloatField(jobject object, jfieldID field);
661 
670  double getDoubleField(jobject object, jfieldID field);
671 
680  String getStringField(jobject object, jfieldID field);
681 
690  jobject getStaticObjectField(jclass clazz, jfieldID field);
691 
700  bool getStaticBooleanField(jclass clazz, jfieldID field);
701 
710  byte_t getStaticByteField(jclass clazz, jfieldID field);
711 
720  char16_t getStaticCharField(jclass clazz, jfieldID field);
721 
730  int16_t getStaticShortField(jclass clazz, jfieldID field);
731 
740  int32_t getStaticIntField(jclass clazz, jfieldID field);
741 
750  int64_t getStaticLongField(jclass clazz, jfieldID field);
751 
760  float getStaticFloatField(jclass clazz, jfieldID field);
761 
770  double getStaticDoubleField(jclass clazz, jfieldID field);
771 
780  String getStaticStringField(jclass clazz, jfieldID field);
781 
790  void setObjectField(jobject object, jfieldID field, jobject value);
791 
800  void setBooleanField(jobject object, jfieldID field, bool value);
801 
810  void setByteField(jobject object, jfieldID field, byte_t value);
811 
820  void setCharField(jobject object, jfieldID field, char16_t value);
821 
830  void setShortField(jobject object, jfieldID field, int16_t value);
831 
840  void setIntField(jobject object, jfieldID field, int32_t value);
841 
850  void setLongField(jobject object, jfieldID field, int64_t value);
851 
860  void setFloatField(jobject object, jfieldID field, float value);
861 
870  void setDoubleField(jobject object, jfieldID field, double value);
871 
880  void setStringField(jobject object, jfieldID field, const String& value);
881 
890  void setStaticObjectField(jclass clazz, jfieldID field, jobject value);
891 
900  void setStaticBooleanField(jclass clazz, jfieldID field, bool value);
901 
910  void setStaticByteField(jclass clazz, jfieldID field, byte_t value);
911 
920  void setStaticCharField(jclass clazz, jfieldID field, char16_t value);
921 
930  void setStaticShortField(jclass clazz, jfieldID field, int16_t value);
931 
940  void setStaticIntField(jclass clazz, jfieldID field, int32_t value);
941 
950  void setStaticLongField(jclass clazz, jfieldID field, int64_t value);
951 
960  void setStaticFloatField(jclass clazz, jfieldID field, float value);
961 
970  void setStaticDoubleField(jclass clazz, jfieldID field, double value);
971 
979  jbyteArray createByteArray(uint_t length);
980 
989  void setObjectArrayElement(jobjectArray array, uint_t index, jobject value);
990 
999  jobject getObjectArrayElement(jobjectArray array, uint_t index);
1000 
1008  uint_t getArrayLength(jarray array);
1009 
1019  jobject createDirectByteBuffer(byte_t* buf, size_t size);
1020 
1031  void* getDirectBufferRegion(jobject buf, uint64_t& size);
1032 
1040  jobject createLocalRef(jobject object);
1041 
1047  void deleteLocalRef(jobject ref);
1048 
1055  void ensureLocalCapacity(uint_t capacity);
1056 
1064  jobject createGlobalRef(jobject object);
1065 
1071  void deleteGlobalRef(jobject ref);
1072 
1080  String decodeString(jstring str);
1081 
1089  jstring encodeString(const String& str);
1090 
1097  uint_t getStringLength(jstring str);
1098 
1113  void* getArrayData(jarray array, uint_t &length, bool* isCopy = NULL);
1114 
1122  void releaseArrayData(jarray array, void* data);
1123 
1132  jbyteArray createByteArray(const byte_t* data, size_t length);
1133 
1144  jarray createObjectArray(jclass type, uint_t length,
1145  jobject initialValue = NULL);
1146 
1155  bool isSameObject(jobject object1, jobject object2);
1156 
1166  bool isInstanceOf(jobject object, jclass clazz);
1167 
1176  bool isAssignableFrom(jclass clazz1, jclass clazz2);
1177 
1184  jclass getClassForObject(jobject object);
1185 
1193  jclass getSuperclass(jclass clazz);
1194 
1202  void enterMonitor(jobject object);
1203 
1210  void exitMonitor(jobject object);
1211 
1220  void registerNativeMethods(jclass clazz, const JavaNativeMethod methods[]);
1221 
1227  void unregisterNativeMethods(jclass clazz);
1228 
1235  void pushLocalFrame(uint_t capacity = 8);
1236 
1241  void popLocalFrame();
1242 
1244  inline bool isVerbose() const
1245  { return(_verbose); }
1246 
1247  private:
1248 
1249  void attach(JavaVirtualMachine* jvm, JNIEnv* env, bool verbose);
1250  void detach();
1251  inline bool isAttached() const
1252  { return(_env != NULL); }
1253 
1254  inline JNIEnv* getEnv() const
1255  { return(_env); }
1256 
1257  JavaVirtualMachine* _jvm;
1258  JNIEnv* _env;
1259  bool _verbose;
1260 };
1261 
1262 } // namespace ccxx
1263 
1264 #endif // __ccxx_JavaContext_hxx
#define COMMONCPPJVM_API
Definition: Common.h++:128
String signature
The signature of the native method.
Definition: JavaContext.h++:46
An object that encapsulates (most of) the Java Native Interface (JNI) to a Java Virtual Machine...
Definition: JavaVirtualMachine.h++:53
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
When the Invocation Interface is being used (that is, when a C++ program calls into Java code)...
Definition: JavaContext.h++:67
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
bool isVerbose() const
Determine if verbose mode is enabled for this context.
Definition: JavaContext.h++:1244
uint16_t char16_t
A UTF-16 character.
Definition: Common.h++:201
A structure representing a Java native method.
Definition: JavaContext.h++:43
Definition: AllocationMap.c++:25
unsigned char byte_t
An unsigned 8-bit value.
Definition: Integers.h++:68