JniClass.cpp 7.6 KB

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