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. jclass cls = env->FindClass(name.c_str());
  60. if(env->ExceptionCheck())
  61. {
  62. LOGE("Error finding class: %s", name.c_str());
  63. return NULL;
  64. }
  65. return cls;
  66. }
  67. jmethodID JniLookup::findMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
  68. {
  69. if(!m_jvm)
  70. return NULL;
  71. JNIEnv *env = NULL;
  72. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  73. {
  74. return NULL;
  75. }
  76. jmethodID mid = env->GetMethodID(cls, methodName.c_str(), methodSig.c_str());
  77. if(env->ExceptionCheck())
  78. {
  79. return NULL;
  80. }
  81. return mid;
  82. }
  83. jmethodID JniLookup::findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
  84. {
  85. if(!m_jvm)
  86. return NULL;
  87. JNIEnv *env = NULL;
  88. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  89. {
  90. return NULL;
  91. }
  92. jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), methodSig.c_str());
  93. if(env->ExceptionCheck())
  94. {
  95. return NULL;
  96. }
  97. return mid;
  98. }
  99. jfieldID JniLookup::findField(jclass cls, const std::string &fieldName, const std::string &typeStr)
  100. {
  101. if(!m_jvm)
  102. return NULL;
  103. JNIEnv *env = NULL;
  104. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  105. {
  106. return NULL;
  107. }
  108. jfieldID fid = env->GetFieldID(cls, fieldName.c_str(), typeStr.c_str());
  109. if(env->ExceptionCheck())
  110. {
  111. return NULL;
  112. }
  113. return fid;
  114. }
  115. jfieldID JniLookup::findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr)
  116. {
  117. if(!m_jvm)
  118. return NULL;
  119. JNIEnv *env = NULL;
  120. if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
  121. {
  122. return NULL;
  123. }
  124. jfieldID fid = env->GetStaticFieldID(cls, fieldName.c_str(), typeStr.c_str());
  125. if(env->ExceptionCheck())
  126. {
  127. return NULL;
  128. }
  129. return fid;
  130. }