JniClass.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-04-07
  6. * Changed: 2023-05-24
  7. *
  8. * */
  9. #include <ls-std/core/ConditionalFunctionExecutor.hpp>
  10. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  11. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  12. #include <ls-std/core/evaluator/RawNullPointerArgumentEvaluator.hpp>
  13. #include <ls-std/core/evaluator/RawNullPointerEvaluator.hpp>
  14. #include <ls-std/core/jni/JniApi.hpp>
  15. #include <ls-std/core/jni/JniClass.hpp>
  16. #include <memory>
  17. using ls::std::core::ConditionalFunctionExecutor;
  18. using ls::std::core::EmptyStringArgumentEvaluator;
  19. using ls::std::core::NullPointerArgumentEvaluator;
  20. using ls::std::core::RawNullPointerArgumentEvaluator;
  21. using ls::std::core::RawNullPointerEvaluator;
  22. using ls::std::core::experimental::JniApi;
  23. using ls::std::core::experimental::JniClass;
  24. using ls::std::core::experimental::JniClassParameter;
  25. using ls::std::core::experimental::JniMethod;
  26. using ls::std::core::experimental::JniReturnValue;
  27. using ls::std::core::type::RawPointer;
  28. using std::make_pair;
  29. using std::make_shared;
  30. using std::pair;
  31. using std::shared_ptr;
  32. using std::string;
  33. JniClass::JniClass(const shared_ptr<JniClassParameter> &_parameter, const string &_path) : parameter(_parameter), path(_path)
  34. {
  35. NullPointerArgumentEvaluator{_parameter, "no provided reference to JNI class parameter!"}.evaluate();
  36. EmptyStringArgumentEvaluator{_path, "path to associated Java class is empty!"}.evaluate();
  37. RawNullPointerArgumentEvaluator<JNIEnv>{RawPointer<JNIEnv>{_parameter->getJavaEnvironment()}, "Java environment is not being provided!"}.evaluate();
  38. ConditionalFunctionExecutor{_parameter->getJniApi() == nullptr}.execute([this]() { _createJniApi(); });
  39. }
  40. JniClass::~JniClass() = default;
  41. JniReturnValue JniClass::callMethod(const string &_methodIdentifier)
  42. {
  43. JniReturnValue returnValue{};
  44. if (this->_hasMethod(_methodIdentifier))
  45. {
  46. this->_callBooleanMethod(_methodIdentifier, returnValue);
  47. this->_callByteMethod(_methodIdentifier, returnValue);
  48. this->_callCharMethod(_methodIdentifier, returnValue);
  49. this->_callDoubleMethod(_methodIdentifier, returnValue);
  50. this->_callFloatMethod(_methodIdentifier, returnValue);
  51. this->_callIntMethod(_methodIdentifier, returnValue);
  52. this->_callLongMethod(_methodIdentifier, returnValue);
  53. this->_callShortMethod(_methodIdentifier, returnValue);
  54. }
  55. return returnValue;
  56. }
  57. bool JniClass::hasMethod(const string &_methodIdentifier)
  58. {
  59. return this->_hasMethod(_methodIdentifier);
  60. }
  61. bool JniClass::load()
  62. {
  63. this->javaClass = this->parameter->getJniApi()->findClass(this->path);
  64. return this->javaClass != nullptr;
  65. }
  66. bool JniClass::loadMethod(const string &_methodIdentifier, const string &_methodSignature)
  67. {
  68. RawNullPointerEvaluator<_jclass>{RawPointer<_jclass>{this->javaClass}, "no Java class reference available for loading class method!"}.evaluate();
  69. jmethodID methodId = this->parameter->getJniApi()->getMethodId(this->javaClass, _methodIdentifier.c_str(), _methodSignature.c_str());
  70. bool succeeded = methodId != nullptr && !this->_hasMethod(_methodIdentifier);
  71. if (succeeded)
  72. {
  73. JniMethod method{_methodIdentifier, _methodSignature};
  74. method.setMethodId(methodId);
  75. succeeded = this->methods.insert(make_pair<string, JniMethod>(string{_methodIdentifier}, JniMethod{method})).second;
  76. }
  77. return succeeded;
  78. }
  79. void JniClass::_callBooleanMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  80. {
  81. JniMethod method = this->methods.at(_methodIdentifier);
  82. string searchString = ")Z";
  83. string methodSignature = method.getMethodSignature();
  84. bool hasBooleanReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  85. if (hasBooleanReturnType)
  86. {
  87. _returnValue.setBooleanValue(this->parameter->getJniApi()->callBooleanMethod(this->parameter->getJavaObject(), method.getMethodId()));
  88. }
  89. }
  90. void JniClass::_callByteMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  91. {
  92. JniMethod method = this->methods.at(_methodIdentifier);
  93. string searchString = ")B";
  94. string methodSignature = method.getMethodSignature();
  95. bool hasByteReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  96. if (hasByteReturnType)
  97. {
  98. _returnValue.setByteValue(this->parameter->getJniApi()->callByteMethod(this->parameter->getJavaObject(), method.getMethodId()));
  99. }
  100. }
  101. void JniClass::_callCharMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  102. {
  103. JniMethod method = this->methods.at(_methodIdentifier);
  104. string searchString = ")C";
  105. string methodSignature = method.getMethodSignature();
  106. bool hasCharReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  107. if (hasCharReturnType)
  108. {
  109. _returnValue.setCharValue(this->parameter->getJniApi()->callCharMethod(this->parameter->getJavaObject(), method.getMethodId()));
  110. }
  111. }
  112. void JniClass::_callDoubleMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  113. {
  114. JniMethod method = this->methods.at(_methodIdentifier);
  115. string searchString = ")D";
  116. string methodSignature = method.getMethodSignature();
  117. bool hasDoubleReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  118. if (hasDoubleReturnType)
  119. {
  120. _returnValue.setDoubleValue(this->parameter->getJniApi()->callDoubleMethod(this->parameter->getJavaObject(), method.getMethodId()));
  121. }
  122. }
  123. void JniClass::_callFloatMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  124. {
  125. JniMethod method = this->methods.at(_methodIdentifier);
  126. string searchString = ")F";
  127. string methodSignature = method.getMethodSignature();
  128. bool hasFloatReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  129. if (hasFloatReturnType)
  130. {
  131. _returnValue.setFloatValue(this->parameter->getJniApi()->callFloatMethod(this->parameter->getJavaObject(), method.getMethodId()));
  132. }
  133. }
  134. void JniClass::_callIntMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  135. {
  136. JniMethod method = this->methods.at(_methodIdentifier);
  137. string searchString = ")I";
  138. string methodSignature = method.getMethodSignature();
  139. bool hasIntegerReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  140. if (hasIntegerReturnType)
  141. {
  142. _returnValue.setIntegerValue(this->parameter->getJniApi()->callIntMethod(this->parameter->getJavaObject(), method.getMethodId()));
  143. }
  144. }
  145. void JniClass::_callLongMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  146. {
  147. JniMethod method = this->methods.at(_methodIdentifier);
  148. string searchString = ")J";
  149. string methodSignature = method.getMethodSignature();
  150. bool hasLongReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  151. if (hasLongReturnType)
  152. {
  153. _returnValue.setLongValue(this->parameter->getJniApi()->callLongMethod(this->parameter->getJavaObject(), method.getMethodId()));
  154. }
  155. }
  156. void JniClass::_callShortMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  157. {
  158. JniMethod method = this->methods.at(_methodIdentifier);
  159. string searchString = ")S";
  160. string methodSignature = method.getMethodSignature();
  161. bool hasShortReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  162. if (hasShortReturnType)
  163. {
  164. _returnValue.setShortValue(this->parameter->getJniApi()->callShortMethod(this->parameter->getJavaObject(), method.getMethodId()));
  165. }
  166. }
  167. void JniClass::_createJniApi() const
  168. {
  169. this->parameter->setJniApi(make_shared<JniApi>(this->parameter->getJavaEnvironment()));
  170. }
  171. bool JniClass::_hasMethod(const string &_methodIdentifier)
  172. {
  173. return this->methods.find(_methodIdentifier) != this->methods.end();
  174. }