Can't instantiate enum objects. Fix createResultObject() function so that it still works properly

Signed-off-by: Grant Limberg <glimberg@gmail.com>
This commit is contained in:
Grant Limberg 2015-04-22 21:14:55 -07:00
parent 1308f02b93
commit a1a35e0279

View file

@ -30,6 +30,7 @@
#include <ZeroTierOne.h> #include <ZeroTierOne.h>
#include <map> #include <map>
#include <string>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -125,12 +126,11 @@ namespace {
jobject createResultObject(JNIEnv *env, ZT1_ResultCode code) jobject createResultObject(JNIEnv *env, ZT1_ResultCode code)
{ {
// cache the class and constructor so we don't have to // cache the class so we don't have to
// look them up every time we need to create a java // look it up every time we need to create a java
// ResultCode object // ResultCode object
static jclass resultClass = NULL; static jclass resultClass = NULL;
static jmethodID constructorId = NULL;
jobject resultObject = NULL; jobject resultObject = NULL;
if(resultClass == NULL) if(resultClass == NULL)
@ -142,16 +142,30 @@ namespace {
} }
} }
if(constructorId = NULL) std::string fieldName;
switch(code)
{ {
constructorId = env->GetMethodID(resultClass, "<init>", "(I)V"); case ZT1_RESULT_OK:
if(constructorId == NULL) fieldName = "ZT1_RESULT_OK";
{ break;
return NULL; // exception thrown case ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY:
} fieldName = "ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY";
break;
case ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED:
fieldName = "ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED";
break;
case ZT1_RESULT_ERROR_NETWORK_NOT_FOUND:
fieldName = "ZT1_RESULT_ERROR_NETWORK_NOT_FOUND";
break;
case ZT1_RESULT_FATAL_ERROR_INTERNAL:
default:
fieldName = "ZT1_RESULT_FATAL_ERROR_INTERNAL";
break;
} }
resultObject = env->NewObject(resultClass, constructorId, (jlong)code); jfieldID enumField = env->GetStaticFieldID(resultClass, fieldName.c_str(), "Lcom/zerotierone/sdk/ResultCode");
resultObject = env->GetStaticObjectField(resultClass, enumField);
return resultObject; return resultObject;
} }