该篇文章一起学习类的属性和方法。类的属性和方法一般分为静态和非静态,所以JNINativeInterface关于类的属性操作也分为对于静态和非静态的获取和设置。
jfieldID (*GetStaticFieldID)(JNIEnv*, jclass, const char*,const char*);
jobject (*GetStaticObjectField)(JNIEnv*, jclass, jfieldID);
jboolean (*GetStaticBooleanField)(JNIEnv*, jclass, jfieldID);
jbyte (*GetStaticByteField)(JNIEnv*, jclass, jfieldID);
jchar (*GetStaticCharField)(JNIEnv*, jclass, jfieldID);
jshort (*GetStaticShortField)(JNIEnv*, jclass, jfieldID);
jint (*GetStaticIntField)(JNIEnv*, jclass, jfieldID);
jlong (*GetStaticLongField)(JNIEnv*, jclass, jfieldID);
jfloat (*GetStaticFloatField)(JNIEnv*, jclass, jfieldID);
jdouble (*GetStaticDoubleField)(JNIEnv*, jclass, jfieldID);
void (*SetStaticObjectField)(JNIEnv*, jclass, jfieldID, jobject);
void (*SetStaticBooleanField)(JNIEnv*, jclass, jfieldID, jboolean);
void (*SetStaticByteField)(JNIEnv*, jclass, jfieldID, jbyte);
void (*SetStaticCharField)(JNIEnv*, jclass, jfieldID, jchar);
void (*SetStaticShortField)(JNIEnv*, jclass, jfieldID, jshort);
void (*SetStaticIntField)(JNIEnv*, jclass, jfieldID, jint);
void (*SetStaticLongField)(JNIEnv*, jclass, jfieldID, jlong);
void (*SetStaticFloatField)(JNIEnv*, jclass, jfieldID, jfloat);
void (*SetStaticDoubleField)(JNIEnv*, jclass, jfieldID, jdouble);
StaticFieldClass.java
package com.anniljing.jnidemo;
public class StaticFieldClass {
public static String name;
}
ClassStaticFieldOperator.java
package com.anniljing.jnidemo.ClassFieldOperator;
import com.anniljing.jnidemo.StaticFieldClass;
public class ClassStaticFieldOperator {
public static native void setClassStaticField(StaticFieldClass staticFieldClass);
public static native String getClassStaticField(StaticFieldClass staticFieldClass);
}
ClassStaticFieldOperator.cpp
#include
extern "C"
JNIEXPORT void JNICALL
Java_com_anniljing_jnidemo_ClassFieldOperator_ClassStaticFieldOperator_setClassStaticField(
JNIEnv *env, jclass clazz, jobject static_field_class) {
jclass staticField = env->GetObjectClass(static_field_class);
jfieldID staticFieldId = env->GetStaticFieldID(staticField, "name", "Ljava/lang/String;");
env->SetStaticObjectField(staticField, staticFieldId, env->NewStringUTF("我是静态属性"));
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_anniljing_jnidemo_ClassFieldOperator_ClassStaticFieldOperator_getClassStaticField(
JNIEnv *env, jclass clazz, jobject static_field_class) {
jclass staticClass = env->GetObjectClass(static_field_class);
jfieldID nameField = env->GetStaticFieldID(staticClass, "name", "Ljava/lang/String;");
return static_cast<jstring>(env->GetStaticObjectField(staticClass, nameField));
}
MainActivity.java
public void setStaticField(View view) {
StaticFieldClass staticFieldClass = new StaticFieldClass();
ClassStaticFieldOperator.setClassStaticField(staticFieldClass);
Log.d(TAG, "getStaticField:" + ClassStaticFieldOperator.getClassStaticField(staticFieldClass));
}
jfieldID (*GetFieldID)(JNIEnv*, jclass, const char*, const char*);
jobject GetObjectField(jobject obj, jfieldID fieldID)
{ return functions->GetObjectField(this, obj, fieldID); }
jboolean GetBooleanField(jobject obj, jfieldID fieldID)
{ return functions->GetBooleanField(this, obj, fieldID); }
jbyte GetByteField(jobject obj, jfieldID fieldID)
{ return functions->GetByteField(this, obj, fieldID); }
jchar GetCharField(jobject obj, jfieldID fieldID)
{ return functions->GetCharField(this, obj, fieldID); }
jshort GetShortField(jobject obj, jfieldID fieldID)
{ return functions->GetShortField(this, obj, fieldID); }
jint GetIntField(jobject obj, jfieldID fieldID)
{ return functions->GetIntField(this, obj, fieldID); }
jlong GetLongField(jobject obj, jfieldID fieldID)
{ return functions->GetLongField(this, obj, fieldID); }
jfloat GetFloatField(jobject obj, jfieldID fieldID)
{ return functions->GetFloatField(this, obj, fieldID); }
jdouble GetDoubleField(jobject obj, jfieldID fieldID)
{ return functions->GetDoubleField(this, obj, fieldID); }
void (*SetObjectField)(JNIEnv*, jobject, jfieldID, jobject);
void (*SetBooleanField)(JNIEnv*, jobject, jfieldID, jboolean);
void (*SetByteField)(JNIEnv*, jobject, jfieldID, jbyte);
void (*SetCharField)(JNIEnv*, jobject, jfieldID, jchar);
void (*SetShortField)(JNIEnv*, jobject, jfieldID, jshort);
void (*SetIntField)(JNIEnv*, jobject, jfieldID, jint);
void (*SetLongField)(JNIEnv*, jobject, jfieldID, jlong);
void (*SetFloatField)(JNIEnv*, jobject, jfieldID, jfloat);
void (*SetDoubleField)(JNIEnv*, jobject, jfieldID, jdouble);
JavaClass.java
package com.anniljing.jnidemo;
import android.util.Log;
public class JavaClass {
public static String name;
private String version="hello";
private static int sCode;
public JavaClass() {
Log.d("MainActivity","Init JavaClass");
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public static void setCode(int code){
sCode=code;
}
@Override
public String toString() {
return "JavaClass{" +
"version='" + version + '\''+"Code="+sCode+'\'' +
'}';
}
}
ClassFieldOperator.java
package com.anniljing.jnidemo.ClassFieldOperator;
import com.anniljing.jnidemo.JavaClass;
public class ClassFieldOperator {
public native JavaClass classFieldOperator();
}
ClassFieldOperator.cpp
#include
#include
extern "C"
JNIEXPORT jobject JNICALL
Java_com_anniljing_jnidemo_ClassFieldOperator_ClassFieldOperator_classFieldOperator(JNIEnv *env,
jobject thiz) {
jclass javaClass = env->FindClass("com/anniljing/jnidemo/JavaClass");
jmethodID constructor = env->GetMethodID(javaClass, "" , "()V");
jobject obj = env->NewObject(javaClass, constructor);
jfieldID versionId = env->GetFieldID(javaClass, "version", "Ljava/lang/String;");
jobject versionValue = env->GetObjectField(obj, versionId);
jstring value=((jstring) versionValue);
//不能使用%s打印jstring,需要做一下处理
const char *c_str = NULL;
char buff[128] = {0};
jboolean isCopy; // 返回JNI_TRUE表示原字符串的拷贝,返回JNI_FALSE表示返回原字符串的指针
c_str = env->GetStringUTFChars(value, &isCopy);
LOGD("Current version value: %s", c_str);
env->SetObjectField(obj, versionId, env->NewStringUTF("Version:1.00.00"));
return obj;
}
MainActivity.java
public void setNormalField(View view) {
ClassFieldOperator classFieldOperator = new ClassFieldOperator();
JavaClass javaClass = classFieldOperator.classFieldOperator();
Log.d(TAG, javaClass.toString());
}
jmethodID (*GetStaticMethodID)(JNIEnv*, jclass, const char*, const char*);
jobject (*CallStaticObjectMethod)(JNIEnv*, jclass, jmethodID, ...);
jobject (*CallStaticObjectMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jobject (*CallStaticObjectMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jboolean (*CallStaticBooleanMethod)(JNIEnv*, jclass, jmethodID, ...);
jboolean (*CallStaticBooleanMethodV)(JNIEnv*, jclass, jmethodID,
va_list);
jboolean (*CallStaticBooleanMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jbyte (*CallStaticByteMethod)(JNIEnv*, jclass, jmethodID, ...);
jbyte (*CallStaticByteMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jbyte (*CallStaticByteMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jchar (*CallStaticCharMethod)(JNIEnv*, jclass, jmethodID, ...);
jchar (*CallStaticCharMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jchar (*CallStaticCharMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jshort (*CallStaticShortMethod)(JNIEnv*, jclass, jmethodID, ...);
jshort (*CallStaticShortMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jshort (*CallStaticShortMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jint (*CallStaticIntMethod)(JNIEnv*, jclass, jmethodID, ...);
jint (*CallStaticIntMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jint (*CallStaticIntMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jlong (*CallStaticLongMethod)(JNIEnv*, jclass, jmethodID, ...);
jlong (*CallStaticLongMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jlong (*CallStaticLongMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jfloat (*CallStaticFloatMethod)(JNIEnv*, jclass, jmethodID, ...);
jfloat (*CallStaticFloatMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jfloat (*CallStaticFloatMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
jdouble (*CallStaticDoubleMethod)(JNIEnv*, jclass, jmethodID, ...);
jdouble (*CallStaticDoubleMethodV)(JNIEnv*, jclass, jmethodID, va_list);
jdouble (*CallStaticDoubleMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
void (*CallStaticVoidMethod)(JNIEnv*, jclass, jmethodID, ...);
void (*CallStaticVoidMethodV)(JNIEnv*, jclass, jmethodID, va_list);
void (*CallStaticVoidMethodA)(JNIEnv*, jclass, jmethodID, const jvalue*);
JavaClass.java
package com.anniljing.jnidemo;
import android.util.Log;
public class JavaClass {
public static String name;
private String version="hello";
private static int sCode;
public JavaClass() {
Log.d("MainActivity","Init JavaClass");
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public static void setCode(int code){
sCode=code;
}
@Override
public String toString() {
return "JavaClass{" +
"version='" + version + '\''+"Code="+sCode+'\'' +
'}';
}
}
ClassMethodOperator.java
package com.anniljing.jnidemo.classMethodOperator;
import com.anniljing.jnidemo.JavaClass;
public class ClassMethodOperator {
public static native JavaClass operatorClassStaticMethod();
}
ClassMethodOPerator.cpp
#include
extern "C"
JNIEXPORT jobject JNICALL
Java_com_anniljing_jnidemo_classMethodOperator_ClassMethodOperator_operatorClassStaticMethod(
JNIEnv *env, jclass clazz) {
jclass javaClass = env->FindClass("com/anniljing/jnidemo/JavaClass");
jmethodID constrcture = env->GetMethodID(javaClass, "" , "()V");
jobject javaClassObj = env->NewObject(javaClass, constrcture);
jmethodID staticCode=env->GetStaticMethodID(javaClass,"setCode", "(I)V");
env->CallStaticVoidMethod(javaClass,staticCode,11);
return javaClassObj;
}
jmethodID (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
jobject (*CallObjectMethod)(JNIEnv*, jobject, jmethodID, ...);
jobject (*CallObjectMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jobject (*CallObjectMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jboolean (*CallBooleanMethod)(JNIEnv*, jobject, jmethodID, ...);
jboolean (*CallBooleanMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jboolean (*CallBooleanMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jbyte (*CallByteMethod)(JNIEnv*, jobject, jmethodID, ...);
jbyte (*CallByteMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jbyte (*CallByteMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jchar (*CallCharMethod)(JNIEnv*, jobject, jmethodID, ...);
jchar (*CallCharMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jchar (*CallCharMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jshort (*CallShortMethod)(JNIEnv*, jobject, jmethodID, ...);
jshort (*CallShortMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jshort (*CallShortMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jint (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...);
jint (*CallIntMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jint (*CallIntMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jlong (*CallLongMethod)(JNIEnv*, jobject, jmethodID, ...);
jlong (*CallLongMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jlong (*CallLongMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jfloat (*CallFloatMethod)(JNIEnv*, jobject, jmethodID, ...);
jfloat (*CallFloatMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jfloat (*CallFloatMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
jdouble (*CallDoubleMethod)(JNIEnv*, jobject, jmethodID, ...);
jdouble (*CallDoubleMethodV)(JNIEnv*, jobject, jmethodID, va_list);
jdouble (*CallDoubleMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
void (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
void (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list);
void (*CallVoidMethodA)(JNIEnv*, jobject, jmethodID, const jvalue*);
JavaClass.java
package com.anniljing.jnidemo;
import android.util.Log;
public class JavaClass {
public static String name;
private String version="hello";
private static int sCode;
public JavaClass() {
Log.d("MainActivity","Init JavaClass");
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public static void setCode(int code){
sCode=code;
}
@Override
public String toString() {
return "JavaClass{" +
"version='" + version + '\''+"Code="+sCode+'\'' +
'}';
}
}
ClassMethodOperator.java
package com.anniljing.jnidemo.classMethodOperator;
import com.anniljing.jnidemo.JavaClass;
public class ClassMethodOperator {
public static native JavaClass operatorClassMethod();
}
ClassMethodOPerator.cpp
#include
extern "C"
JNIEXPORT jobject JNICALL
Java_com_anniljing_jnidemo_classMethodOperator_ClassMethodOperator_operatorClassMethod(JNIEnv *env,
jclass clazz) {
jclass javaClass = env->FindClass("com/anniljing/jnidemo/JavaClass");
jmethodID constrcture = env->GetMethodID(javaClass, "" , "()V");
jobject javaClassObj = env->NewObject(javaClass, constrcture);
jmethodID setVersion = env->GetMethodID(javaClass, "setVersion", "(Ljava/lang/String;)V");
env->CallVoidMethod(javaClassObj, setVersion, env->NewStringUTF("1.1.1"));
return javaClassObj;
}