| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CJNIHandler.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Control { |
| 27 |
|
| 28 |
namespace Interface { |
| 29 |
|
| 30 |
namespace JNI { |
| 31 |
|
| 32 |
#ifdef KONCLUDE_COMPILE_JNI_INTERFACE |
| 33 |
|
| 34 |
jfieldID CJNIHandler::mBaseBridgeFieldID = nullptr; |
| 35 |
|
| 36 |
CJNIHandler::CJNIHandler(JNIEnv* jenv) : mJenv(jenv), |
| 37 |
mAxiomExpressionBuildingBridgeData(jenv,"[Lcom/konclude/jnibridge/AxiomExpressionBuildingBridge;"), |
| 38 |
mQueryingBridgeData(jenv,"[Lcom/konclude/jnibridge/QueryingBridge;"), |
| 39 |
mObjectSetCallbackListener(jenv,"[Lcom/konclude/jnibridge/ObjectSetCallbackListener;"), |
| 40 |
mSetOfObjectSetCallbackListener(jenv,"[Lcom/konclude/jnibridge/SetOfObjectSetCallbackListener;") { |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
CJNIHandler::~CJNIHandler() { |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
CJNIInstanceManager* CJNIHandler::getJNIInstanceManager(JNIEnv* jenv, jobject obj) { |
| 49 |
CJNIInstanceManager* instanceManger = (CJNIInstanceManager*)jenv->GetLongField(obj,getJNIInstanceManagerBaseFieldID(jenv)); |
| 50 |
return instanceManger; |
| 51 |
} |
| 52 |
|
| 53 |
bool CJNIHandler::setJNIInstanceManager(JNIEnv* jenv, jobject obj, CJNIInstanceManager* instanceManger) { |
| 54 |
jfieldID baseFieldID = getJNIInstanceManagerBaseFieldID(jenv); |
| 55 |
if (baseFieldID) { |
| 56 |
jenv->SetLongField(obj,baseFieldID,(jlong)instanceManger); |
| 57 |
return true; |
| 58 |
} |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
jfieldID CJNIHandler::getJNIInstanceManagerBaseFieldID(JNIEnv* jenv) { |
| 64 |
if (!mBaseBridgeFieldID) { |
| 65 |
jclass baseClass = jenv->FindClass("Lcom/konclude/jnibridge/KoncludeReasonerBridge;"); |
| 66 |
if (!baseClass) { |
| 67 |
throwGeneralException(jenv,"Cannot find Java class com.konclude.jnibridge.KoncludeReasonerBridge to initialize JNI Konclude Reasoner Bridge"); |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
mBaseBridgeFieldID = jenv->GetFieldID(baseClass,"mNativeDataPointer","J"); |
| 72 |
if (!mBaseBridgeFieldID) { |
| 73 |
throwGeneralException(jenv,"Cannot find field mNativeDataPointer for class com.konclude.jnibridge.KoncludeReasonerBridge to initialize JNI Konclude Reasoner Bridge"); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
} |
| 77 |
return mBaseBridgeFieldID; |
| 78 |
} |
| 79 |
|
| 80 |
void CJNIHandler::throwException(JNIEnv* jenv, const char* className, const char* reason) { |
| 81 |
jclass cls = jenv->FindClass(className); |
| 82 |
jenv->ThrowNew(cls,reason); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void CJNIHandler::throwException(JNIEnv* jenv, const char* className) { |
| 87 |
jclass cls = jenv->FindClass(className); |
| 88 |
jmethodID ctorID = jenv->GetMethodID(cls,"<init>","()V"); |
| 89 |
jobject obj = jenv->NewObject(cls,ctorID); |
| 90 |
jenv->Throw((jthrowable)obj); |
| 91 |
} |
| 92 |
|
| 93 |
void CJNIHandler::throwGeneralException(JNIEnv* jenv, const char* reason) { |
| 94 |
throwException(jenv,"Ljava/lang/Exception;",reason); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
void CJNIHandler::throwKoncludeException(JNIEnv* jenv, const char* reason) { |
| 99 |
throwException(jenv,"Lcom/konclude/jnibridge/KoncludeReasonerException;",reason); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
void CJNIHandler::throwKoncludeException(JNIEnv* jenv, const QString& reason) { |
| 104 |
throwException(jenv,"Lcom/konclude/jnibridge/KoncludeReasonerException;",reason.toLocal8Bit().constData()); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
CJNIHandlingClassData* CJNIHandler::getAxiomExpressionBuildingBridgeJNIData() { |
| 109 |
return &mAxiomExpressionBuildingBridgeData; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
CJNIHandlingClassData* CJNIHandler::getQueryingBridgeJNIData() { |
| 114 |
return &mQueryingBridgeData; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
CJNIHandlingClassData* CJNIHandler::getObjectSetCallbackListenerJNIData() { |
| 119 |
return &mObjectSetCallbackListener; |
| 120 |
} |
| 121 |
|
| 122 |
CJNIHandlingClassData* CJNIHandler::getSetOfObjectSetCallbackListenerJNIData() { |
| 123 |
return &mSetOfObjectSetCallbackListener; |
| 124 |
} |
| 125 |
|
| 126 |
QString CJNIHandler::getString(JNIEnv* jenv, jstring string) { |
| 127 |
const char* stringChrs = jenv->GetStringUTFChars(string,0); |
| 128 |
return QString::fromUtf8(stringChrs); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
CJNIHandler* CJNIHandler::setAxiomExpressionBuildingBridgeNativeData(JNIEnv* jenv, jobject obj, CJNIAxiomExpressionVisitingLoader* loader) { |
| 133 |
jenv->SetLongField(obj,getAxiomExpressionBuildingBridgeJNIData()->getNativeDataFieldID(),(jlong)loader); |
| 134 |
return this; |
| 135 |
} |
| 136 |
|
| 137 |
CJNIAxiomExpressionVisitingLoader* CJNIHandler::getAxiomExpressionBuildingBridgeNativeData(JNIEnv* jenv, jobject obj) { |
| 138 |
return (CJNIAxiomExpressionVisitingLoader*)jenv->GetLongField(obj,getAxiomExpressionBuildingBridgeJNIData()->getNativeDataFieldID()); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
CJNIHandler* CJNIHandler::setQueryingBridgeNativeData(JNIEnv* jenv, jobject obj, CJNIQueryProcessor* processor) { |
| 144 |
jenv->SetLongField(obj,getQueryingBridgeJNIData()->getNativeDataFieldID(),(jlong)processor); |
| 145 |
return this; |
| 146 |
} |
| 147 |
|
| 148 |
CJNIQueryProcessor* CJNIHandler::getQueryingBridgeNativeData(JNIEnv* jenv, jobject obj) { |
| 149 |
return (CJNIQueryProcessor*)jenv->GetLongField(obj,getQueryingBridgeJNIData()->getNativeDataFieldID()); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
#endif |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
}; // end namespace JNI |
| 159 |
|
| 160 |
}; // end namespace Interface |
| 161 |
|
| 162 |
}; // end namespace Control |
| 163 |
|
| 164 |
}; // end namespace Konclude |