github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/jvm/gojvm_wrap.h (about)

     1  //
     2  // Created by rarnu on 2022/3/29.
     3  //
     4  
     5  #ifndef GOJVM_GOJVM_WRAP_H
     6  #define GOJVM_GOJVM_WRAP_H
     7  
     8  #define _GO_EXPORT __attribute__((__visibility__("default")))
     9  
    10  #define WRAP_RETURN_STRING \
    11      const char* str = (*env)->GetStringUTFChars(env, jret, NULL); \
    12      (*env)->DeleteLocalRef(env, jret);                            \
    13      return (char*)str;
    14  
    15  #define WRAP_STATIC_VOID(JNINAME) \
    16      jmethodID m = (*env)->GetStaticMethodID(env, clazz, methodName, sig);   \
    17      jvalue *v = makeParams(env, len, types, args);                          \
    18      (*env)->JNINAME(env, clazz, m, v);                                      \
    19      freeParams(env, len, types, v);
    20  
    21  #define WRAP_VOID(JNINAME) \
    22      jmethodID m = (*env)->GetMethodID(env, clazz, methodName, sig);   \
    23      jvalue *v = makeParams(env, len, types, args);                    \
    24      (*env)->JNINAME(env, obj, m, v);                                  \
    25      freeParams(env, len, types, v);
    26  
    27  #define WRAP_STATIC_RET(JNINAME, JNIRET) \
    28      jmethodID m = (*env)->GetStaticMethodID(env, clazz, methodName, sig);   \
    29      jvalue *v = makeParams(env, len, types, args);                          \
    30      JNIRET jret = (*env)->JNINAME(env, clazz, m, v);                        \
    31      freeParams(env, len, types, v);
    32  
    33  #define WRAP_RET(JNINAME, JNIRET) \
    34      jmethodID m = (*env)->GetMethodID(env, clazz, methodName, sig);   \
    35      jvalue *v = makeParams(env, len, types, args);                    \
    36      JNIRET jret = (*env)->JNINAME(env, obj, m, v);                    \
    37      freeParams(env, len, types, v);
    38  
    39  #define WRAP_STATIC_VOID_METHOD(NAME) \
    40      void callStatic##NAME##Method(JNIEnv* env, jclass clazz, char* methodName, char* sig, int len, char** types, void** args) { \
    41          WRAP_STATIC_VOID(CallStatic##NAME##MethodA)                                                                             \
    42      }
    43  
    44  #define WRAP_VOID_METHOD(NAME) \
    45      void call##NAME##Method(JNIEnv* env, jclass clazz, jobject obj, char* methodName, char* sig, int len, char** types, void** args) { \
    46          WRAP_VOID(Call##NAME##MethodA)                                                                                                 \
    47      }
    48  
    49  #define WRAP_STATIC_METHOD(NAME, RET, JNIRET) \
    50      RET callStatic##NAME##Method(JNIEnv* env, jclass clazz, char* methodName, char* sig, int len, char** types, void** args) {  \
    51          WRAP_STATIC_RET(CallStatic##NAME##MethodA, JNIRET)                                                                      \
    52          return (RET)jret;                                                                                                       \
    53      }
    54  
    55  #define WRAP_METHOD(NAME, RET, JNIRET) \
    56      RET call##NAME##Method(JNIEnv* env, jclass clazz, jobject obj, char* methodName, char* sig, int len, char** types, void** args) {  \
    57          WRAP_RET(Call##NAME##MethodA, JNIRET)                                                                                          \
    58          return (RET)jret;                                                                                                              \
    59      }
    60  
    61  #define WRAP_STATIC_STRING_METHOD(NAME) \
    62      char* callStatic##NAME##Method(JNIEnv* env, jclass clazz, char* methodName, char* sig, int len, char** types, void** args) {  \
    63          WRAP_STATIC_RET(CallStaticObjectMethodA, jobject)                                                                         \
    64          WRAP_RETURN_STRING                                                                                                        \
    65      }
    66  
    67  #define WRAP_STRING_METHOD(NAME) \
    68      char* call##NAME##Method(JNIEnv* env, jclass clazz, jobject obj, char* methodName, char* sig, int len, char** types, void** args) {  \
    69          WRAP_RET(CallObjectMethodA, jobject)                                                                                             \
    70          WRAP_RETURN_STRING                                                                                                               \
    71      }
    72  
    73  #define WRAP_STATIC_FIELD_GET(NAME, RET, JNIRET) \
    74      RET getStatic##NAME(JNIEnv* env, jclass clazz, char* fieldName, char* sig) { \
    75          jfieldID f = (*env)->GetStaticFieldID(env, clazz, fieldName, sig);       \
    76          JNIRET jret = (*env)->GetStatic##NAME##Field(env, clazz, f);             \
    77          return (RET) jret;                                                      \
    78      }
    79  
    80  #define WRAP_STATIC_FIELD_SET(NAME, JNITYPE) \
    81      void setStatic##NAME(JNIEnv* env, jclass clazz, char* fieldName, char* sig, JNITYPE obj) { \
    82          jfieldID f = (*env)->GetStaticFieldID(env, clazz, fieldName, sig);                     \
    83          (*env)->SetStatic##NAME##Field(env, clazz, f, obj);                                    \
    84      }
    85  
    86  #define WRAP_STATIC_FIELD_GET_STRING(NAME) \
    87      char* getStatic##NAME(JNIEnv* env, jclass clazz, char* fieldName) {                 \
    88          jstring jret = getStaticObject(env, clazz, fieldName, "Ljava/lang/String;");    \
    89          WRAP_RETURN_STRING                                                              \
    90      }
    91  
    92  #define WRAP_STATIC_FIELD_SET_STRING(NAME) \
    93      void setStatic##NAME(JNIEnv* env, jclass clazz, char* fieldName, char* value) { \
    94          jstring jstr = (*env)->NewStringUTF(env, value);                            \
    95          setStaticObject(env, clazz, fieldName, "Ljava/lang/String;", jstr);         \
    96          (*env)->DeleteLocalRef(env, jstr);                                          \
    97      }
    98  
    99  #define WRAP_STATIC_FIELD_GET_SIG(NAME, RET, JNIRET, SIG) \
   100      RET getStatic##NAME(JNIEnv* env, jclass clazz, char* fieldName) {      \
   101          jfieldID f = (*env)->GetStaticFieldID(env, clazz, fieldName, SIG); \
   102          JNIRET jret = (*env)->GetStatic##NAME##Field(env, clazz, f);       \
   103          return (RET)jret;                                                  \
   104      }
   105  
   106  #define WRAP_STATIC_FIELD_SET_SIG(NAME, TYPE, JNITYPE, SIG) \
   107      void setStatic##NAME(JNIEnv* env, jclass clazz, char* fieldName, TYPE value) { \
   108          jfieldID f = (*env)->GetStaticFieldID(env, clazz, fieldName, SIG);         \
   109          (*env)->SetStatic##NAME##Field(env, clazz, f, (JNITYPE)value);             \
   110      }
   111  
   112  #define WRAP_FIELD_GET(NAME, RET, JNIRET) \
   113      RET getObject##NAME(JNIEnv* env, jclass clazz, jobject obj, char* fieldName, char* sig) { \
   114          jfieldID f = (*env)->GetFieldID(env, clazz, fieldName, sig);                          \
   115          JNIRET jret = (*env)->Get##NAME##Field(env, obj, f);                                  \
   116          return (RET) jret;                                                                    \
   117      }
   118  
   119  #define WRAP_FIELD_SET(NAME, JNITYPE) \
   120      void setObject##NAME(JNIEnv* env, jclass clazz, jobject obj, char* fieldName, char* sig, JNITYPE value) { \
   121          jfieldID f = (*env)->GetFieldID(env, clazz, fieldName, sig);                                          \
   122          (*env)->Set##NAME##Field(env, obj, f, value);                                                         \
   123      }
   124  
   125  #define WRAP_FIELD_GET_STRING(NAME) \
   126      char* getObject##NAME(JNIEnv* env, jclass clazz, jobject obj, char* fieldName) {      \
   127          jstring jret = getObjectObject(env, clazz, obj, fieldName, "Ljava/lang/String;"); \
   128          WRAP_RETURN_STRING                                                                \
   129      }
   130  
   131  #define WRAP_FIELD_SET_STRING(NAME) \
   132      void setObject##NAME(JNIEnv* env, jclass clazz, jobject obj, char* fieldName, char* value) { \
   133          jstring jstr = (*env)->NewStringUTF(env, value);                                         \
   134          setObjectObject(env, clazz, obj, fieldName, "Ljava/lang/String;", jstr);                 \
   135          (*env)->DeleteLocalRef(env, jstr);                                                       \
   136      }
   137  
   138  #define WRAP_FIELD_GET_SIG(NAME, RET, JNIRET, SIG) \
   139      RET getObject##NAME(JNIEnv* env, jclass clazz, jobject obj, char* fieldName) { \
   140          jfieldID f = (*env)->GetFieldID(env, clazz, fieldName, SIG);               \
   141          JNIRET jret = (*env)->Get##NAME##Field(env, obj, f);                       \
   142          return (RET)jret;                                                          \
   143      }
   144  
   145  #define WRAP_FIELD_SET_SIG(NAME, TYPE, JNITYPE, SIG) \
   146      void setObject##NAME(JNIEnv* env, jclass clazz, jobject obj, char* fieldName, TYPE value) { \
   147          jfieldID f = (*env)->GetFieldID(env, clazz, fieldName, SIG);                            \
   148          (*env)->Set##NAME##Field(env, obj, f, (JNITYPE)value);                                  \
   149      }
   150  
   151  #endif //GOJVM_GOJVM_WRAP_H