mirror of
https://github.com/MinimaJack/JVM-plugin.git
synced 2024-11-21 13:15:56 +02:00
Cleanup code
This commit is contained in:
parent
e83d2f9381
commit
ff6f4b112d
@ -14,12 +14,13 @@
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include "JVMLauncher.h"
|
||||
#include "JavaThreadScope.h"
|
||||
|
||||
#define BASE_ERRNO 7
|
||||
|
||||
static jobject s_threadLock;
|
||||
|
||||
IAddInDefBase *gAsyncEvent = nullptr;
|
||||
|
||||
|
||||
static wchar_t *g_PropNames[] = { L"IsEnabled", L"javaHome", L"libraryDir" };
|
||||
static wchar_t *g_MethodNames[] = { L"LaunchInJVM", L"LaunchInJVMP", L"LaunchInJVMPP",
|
||||
@ -695,7 +696,8 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
if (!lSizeArray || !paParams)
|
||||
return false;
|
||||
|
||||
bool resultOk = true;
|
||||
bool resultOk;
|
||||
|
||||
std::string classFunctionName = getStdStringFrom1C(paParams);
|
||||
|
||||
if (classFunctionName.empty()) {
|
||||
@ -707,7 +709,7 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
}
|
||||
|
||||
int lastIndexOfParam = lSizeArray;
|
||||
bool classContainFunctionName = false;
|
||||
bool classContainMethodName = false;
|
||||
TYPEVAR returnType;
|
||||
|
||||
switch (lMethodNum)
|
||||
@ -736,7 +738,7 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
case eClassFunctionCallP:
|
||||
case eClassFunctionCallPP:
|
||||
{
|
||||
classContainFunctionName = true;
|
||||
classContainMethodName = true;
|
||||
lastIndexOfParam = lSizeArray - 1;
|
||||
returnType = TV_VT(&paParams[lastIndexOfParam]);
|
||||
}
|
||||
@ -745,27 +747,25 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
case eClassProcedureCallP:
|
||||
case eClassProcedureCallPP:
|
||||
{
|
||||
classContainFunctionName = true;
|
||||
classContainMethodName = true;
|
||||
lastIndexOfParam = lSizeArray;
|
||||
returnType = VTYPE_EMPTY;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
JavaThreadScope scope(this);
|
||||
|
||||
jint res = m_RunningJVMInstance->GetEnv((void**)&m_JVMEnv, NULL);
|
||||
res = m_RunningJVMInstance->AttachCurrentThread((void**)&m_JVMEnv, NULL);
|
||||
|
||||
if (res != JNI_OK) {
|
||||
if (!scope.isAttached()) {
|
||||
pAsyncEvent->AddError(ADDIN_E_FAIL, JVM_LAUNCHER, L"Could not attach to the JVM", 4);
|
||||
return false;
|
||||
}
|
||||
jclass findedClass;
|
||||
jmethodID methodID;
|
||||
|
||||
if (classContainFunctionName) {
|
||||
if (classContainMethodName) {
|
||||
auto value = HasClassAndMethodInCache(classFunctionName);
|
||||
if (value != std::nullopt) {
|
||||
if (value) {
|
||||
findedClass = value->first;
|
||||
methodID = value->second;
|
||||
}
|
||||
@ -775,7 +775,7 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
findedClass = findClassForCall(className);
|
||||
if (findedClass != nullptr) {
|
||||
auto methodName = classFunctionName.substr(pos + 1);
|
||||
std::string signature = buildSignature(this->m_JVMEnv, paParams, 1, lastIndexOfParam, returnType);
|
||||
std::string signature = this->buildSignature(this->m_JVMEnv, paParams, 1, lastIndexOfParam, returnType);
|
||||
methodID = JNI_getStaticMethodID(findedClass, methodName.c_str(), signature.c_str(), resultOk);
|
||||
if (resultOk) {
|
||||
m_cachedClassesMethod.insert(std::make_pair(classFunctionName, std::make_pair(findedClass, methodID)));
|
||||
@ -801,14 +801,12 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
|
||||
if (findedClass == nullptr) {
|
||||
pAsyncEvent->AddError(ADDIN_E_FAIL, JVM_LAUNCHER, L"Cannot find class", 10);
|
||||
resultOk = false;
|
||||
goto detach;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (methodID == nullptr) {
|
||||
pAsyncEvent->AddError(ADDIN_E_FAIL, JVM_LAUNCHER, L"Cannot find method", 10);
|
||||
resultOk = false;
|
||||
goto detach;
|
||||
return false;
|
||||
}
|
||||
|
||||
jvalue *values = getParams(this->m_JVMEnv, paParams, 1, lastIndexOfParam);
|
||||
@ -844,7 +842,7 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
delete[] values;
|
||||
|
||||
if (!resultOk) {
|
||||
goto detach;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (returnType != VTYPE_EMPTY) {
|
||||
@ -924,9 +922,6 @@ bool JVMLauncher::CallAsFunc(const long lMethodNum,
|
||||
|
||||
}
|
||||
|
||||
detach:
|
||||
m_RunningJVMInstance->DetachCurrentThread();
|
||||
|
||||
return resultOk;
|
||||
}
|
||||
|
||||
|
@ -26,8 +26,13 @@ typedef jint(JNICALL *CreateJavaVM)(JavaVM **pvm, void **penv, void *args);
|
||||
typedef jint(JNICALL * GetCreatedJavaVMs)(JavaVM**, jsize, jsize*);
|
||||
typedef std::pair<jclass, jmethodID> jclassMethodHolder;
|
||||
|
||||
static IAddInDefBase *gAsyncEvent;
|
||||
|
||||
|
||||
class JVMLauncher : public IComponentBase {
|
||||
public:
|
||||
JavaVM *m_RunningJVMInstance;
|
||||
JNIEnv *m_JVMEnv;
|
||||
|
||||
enum Props
|
||||
{
|
||||
@ -110,8 +115,6 @@ private:
|
||||
|
||||
CreateJavaVM m_JVMInstance = nullptr;
|
||||
GetCreatedJavaVMs m_GetCreatedJavaVMs = nullptr;
|
||||
JNIEnv *m_JVMEnv;
|
||||
JavaVM *m_RunningJVMInstance;
|
||||
|
||||
std::vector<std::string> m_listOfJars;
|
||||
std::vector<std::string> m_listOfClasses;
|
||||
|
@ -214,6 +214,7 @@
|
||||
<None Include="JVMLauncher.def" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="JavaThreadScope.h" />
|
||||
<ClInclude Include="Utils.h" />
|
||||
<ClInclude Include="include\AddInDefBase.h" />
|
||||
<ClInclude Include="include\addinlib.h" />
|
||||
|
@ -67,5 +67,8 @@
|
||||
<ClInclude Include="Utils.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="JavaThreadScope.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
34
JavaThreadScope.h
Normal file
34
JavaThreadScope.h
Normal file
@ -0,0 +1,34 @@
|
||||
|
||||
class JavaThreadScope {
|
||||
public:
|
||||
JavaThreadScope(JVMLauncher* launcher) : attached(false), launcher(launcher){
|
||||
jint res = launcher->m_RunningJVMInstance->GetEnv((void**)&(launcher->m_JVMEnv), NULL);
|
||||
res = launcher->m_RunningJVMInstance->AttachCurrentThread((void**)&(launcher->m_JVMEnv), NULL);
|
||||
|
||||
if (res != JNI_OK) {
|
||||
attached = false;
|
||||
}
|
||||
else {
|
||||
attached = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
JavaThreadScope(const JavaThreadScope&) = delete;
|
||||
JavaThreadScope& operator=(const JavaThreadScope&) = delete;
|
||||
|
||||
virtual ~JavaThreadScope() {
|
||||
if (attached) {
|
||||
launcher->m_RunningJVMInstance->DetachCurrentThread();
|
||||
attached = false;
|
||||
}
|
||||
}
|
||||
|
||||
JNIEnv *GetEnv() const { return launcher->m_JVMEnv; }
|
||||
bool isAttached() const { return attached; }
|
||||
|
||||
private:
|
||||
bool attached;
|
||||
JVMLauncher* launcher;
|
||||
|
||||
};
|
Loading…
Reference in New Issue
Block a user