ZT_jnilookup.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "ZT_jnilookup.h"
  28. #include "ZT_jniutils.h"
  29. JniLookup::JniLookup()
  30. : m_jvm(NULL)
  31. {
  32. LOGV("JNI Cache Created");
  33. }
  34. JniLookup::JniLookup(JavaVM *jvm)
  35. : m_jvm(jvm)
  36. {
  37. LOGV("JNI Cache Created");
  38. }
  39. JniLookup::~JniLookup()
  40. {
  41. LOGV("JNI Cache Destroyed");
  42. }
  43. void JniLookup::setJavaVM(JavaVM *jvm)
  44. {
  45. LOGV("Assigned JVM to object");
  46. m_jvm = jvm;
  47. }
  48. jclass JniLookup::findClass(const std::string &name)
  49. {
  50. if(!m_jvm)
  51. return NULL;
  52. // get the class from the JVM
  53. JNIEnv *env = NULL;
  54. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  55. {
  56. LOGE("Error retreiving JNI Environment");
  57. return NULL;
  58. }
  59. const char *c = name.c_str();
  60. jclass cls = env->FindClass(c);
  61. if(env->ExceptionCheck())
  62. {
  63. LOGE("Error finding class: %s", name.c_str());
  64. return NULL;
  65. }
  66. return cls;
  67. }
  68. jmethodID JniLookup::findMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
  69. {
  70. if(!m_jvm)
  71. return NULL;
  72. JNIEnv *env = NULL;
  73. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  74. {
  75. return NULL;
  76. }
  77. jmethodID mid = env->GetMethodID(cls, methodName.c_str(), methodSig.c_str());
  78. if(env->ExceptionCheck())
  79. {
  80. return NULL;
  81. }
  82. return mid;
  83. }
  84. jmethodID JniLookup::findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
  85. {
  86. if(!m_jvm)
  87. return NULL;
  88. JNIEnv *env = NULL;
  89. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  90. {
  91. return NULL;
  92. }
  93. jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), methodSig.c_str());
  94. if(env->ExceptionCheck())
  95. {
  96. return NULL;
  97. }
  98. return mid;
  99. }
  100. jfieldID JniLookup::findField(jclass cls, const std::string &fieldName, const std::string &typeStr)
  101. {
  102. if(!m_jvm)
  103. return NULL;
  104. JNIEnv *env = NULL;
  105. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  106. {
  107. return NULL;
  108. }
  109. jfieldID fid = env->GetFieldID(cls, fieldName.c_str(), typeStr.c_str());
  110. if(env->ExceptionCheck())
  111. {
  112. return NULL;
  113. }
  114. return fid;
  115. }
  116. jfieldID JniLookup::findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr)
  117. {
  118. if(!m_jvm)
  119. return NULL;
  120. JNIEnv *env = NULL;
  121. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  122. {
  123. return NULL;
  124. }
  125. jfieldID fid = env->GetStaticFieldID(cls, fieldName.c_str(), typeStr.c_str());
  126. if(env->ExceptionCheck())
  127. {
  128. return NULL;
  129. }
  130. return fid;
  131. }