Próbuję użyć funkcji JNI, aby utworzyć klasę Java i ustawić niektóre właściwości tej klasy, używając metody konstruktora DeviceId.java. Jestem w stanie uzyskać metodę konstruktora za pomocą metody GetMethodID, ale w jaki sposób utworzyć nową instancję Device.java, a następnie ustawić właściwości (setId i setCache). Celem jest zwrócenie całkowicie zapełnionej instancji Device.java Object do osoby dzwoniącej. Jakieś pomysły?Użyj JNI, aby utworzyć, wypełnić i zwrócić instancję klasy Java
JNI Funkcja:
JNIEXPORT jobject JNICALL Java_com_test_getID(JNIEnv *env, jclass cls)
{
jmethodID cnstrctr;
jclass c = (*env)->FindClass(env, "com/test/DeviceId");
if (c == 0) {
printf("Find Class Failed.\n");
}else{
printf("Found class.\n");
}
cnstrctr = (*env)->GetMethodID(env, c, "<init>", "(Ljava/lang/String;[B)V");
if (cnstrctr == 0) {
printf("Find method Failed.\n");
}else {
printf("Found method.\n");
}
return (*env)->NewObject(env, c, cnstrctr);
}
Java Class:
package com.test;
public class DeviceId {
private String id;
private byte[] cache;
public DeviceId(){}
public DeviceId(String id, byte[] cache){
this.id=id;
this.cache=cache;
}
public byte[] getCache() {
return cache;
}
public void setCache(byte[] cache) {
this.cache = cache;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Co to jest "" [B "' w '" (Ljava/lang/String; [B) V "'? –
@WilburWhateley, to [tablica bajtów] (http://stackoverflow.com/a/30815321/4440694) –