Created
March 4, 2020 15:04
-
-
Save fish2000/8170c62853f1c9f79cb91db2a3ad340e to your computer and use it in GitHub Desktop.
JNI Method Signatures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* DO NOT EDIT THIS FILE - it is machine generated */ | |
#include <jni.h> | |
/* Header for class HelloJNI */ | |
#ifndef _Included_HelloJNI | |
#define _Included_HelloJNI | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
/* | |
* Class: HelloJNI | |
* Method: sayHello | |
* Signature: ()V | |
*/ | |
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *, jobject); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
// Save as "HelloJNI.cc" | |
#include <jni.h> // JNI header provided by JDK | |
#include <iostream> // C++ standard IO header | |
#include "HelloJNI.h" // Generated | |
using namespace std; | |
// Implementation of the native method sayHello() | |
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) { | |
cout << "Hello World from C++!" << endl; | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include "com_marakana_jniexamples_InstanceAccess.h" | |
JNIEXPORT void JNICALL Java_com_marakana_jniexamples_InstanceAccess_propertyAccess(JNIEnv *env, jobject object){ | |
jfieldID fieldId; | |
jstring jstr; | |
const char *cString; | |
/* Getting a reference to object class */ | |
jclass class = (*env)->GetObjectClass(env, object); /* 1 */ | |
/* Getting the field id in the class */ | |
fieldId = (*env)->GetFieldID(env, class, "name", "Ljava/lang/String;"); /* 2 */ | |
if (fieldId == NULL) { | |
return; /* Error while getting field id */ | |
} | |
/* Getting a jstring */ | |
jstr = (*env)->GetObjectField(env, object, fieldId); /* 3 */ | |
/* From that jstring we are getting a C string: char* */ | |
cString = (*env)->GetStringUTFChars(env, jstr, NULL); /* 4 */ | |
if (cString == NULL) { | |
return; /* Out of memory */ | |
} | |
printf("C: value of name before property modification = \"%s\"\n", cString); | |
(*env)->ReleaseStringUTFChars(env, jstr, cString); | |
/* Creating a new string containing the new name */ | |
jstr = (*env)->NewStringUTF(env, "Brian"); /* 5 */ | |
if (jstr == NULL) { | |
return; /* Out of memory */ | |
} | |
/* Overwrite the value of the name property */ | |
(*env)->SetObjectField(env, object, fieldId, jstr); /* 6 */ | |
} | |
JNIEXPORT void JNICALL Java_com_marakana_jniexamples_InstanceAccess_methodAccess(JNIEnv *env, jobject object){ | |
jclass class = (*env)->GetObjectClass(env, object); /* 7 */ | |
jmethodID methodId = (*env)->GetMethodID(env, class, "setName", "(Ljava/lang/String;)V"); /* 8 */ | |
jstring jstr; | |
if (methodId == NULL) { | |
return; /* method not found */ | |
} | |
/* Creating a new string containing the new name */ | |
jstr = (*env)->NewStringUTF(env, "Nick"); /* 9 */ | |
(*env)->CallVoidMethod(env, object, methodId, jstr); /* 10 */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
#include <stdio.h> | |
#include "TestJNIInstanceVariable.h" | |
JNIEXPORT void JNICALL Java_TestJNIInstanceVariable_modifyInstanceVariable | |
(JNIEnv *env, jobject thisObj) { | |
// Get a reference to this object's class | |
jclass thisClass = (*env)->GetObjectClass(env, thisObj); | |
// int | |
// Get the Field ID of the instance variables "number" | |
jfieldID fidNumber = (*env)->GetFieldID(env, thisClass, "number", "I"); | |
if (NULL == fidNumber) return; | |
// Get the int given the Field ID | |
jint number = (*env)->GetIntField(env, thisObj, fidNumber); | |
printf("In C, the int is %d\n", number); | |
// Change the variable | |
number = 99; | |
(*env)->SetIntField(env, thisObj, fidNumber, number); | |
// Get the Field ID of the instance variables "message" | |
jfieldID fidMessage = (*env)->GetFieldID(env, thisClass, "message", "Ljava/lang/String;"); | |
if (NULL == fidMessage) return; | |
// String | |
// Get the object given the Field ID | |
jstring message = (*env)->GetObjectField(env, thisObj, fidMessage); | |
// Create a C-string with the JNI String | |
const char *cStr = (*env)->GetStringUTFChars(env, message, NULL); | |
if (NULL == cStr) return; | |
printf("In C++, the string is %s\n", cStr); | |
(*env)->ReleaseStringUTFChars(env, message, cStr); | |
// Create a new C-string and assign to the JNI string | |
message = (*env)->NewStringUTF(env, "Hello from C++"); | |
if (NULL == message) return; | |
// modify the instance variables | |
(*env)->SetObjectField(env, thisObj, fidMessage, message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Type erasure notes: