JniClass.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-08
  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::JniApi;
  19. using ls::std::core::JniClass;
  20. using ls::std::core::JniClassParameter;
  21. using ls::std::core::JniMethod;
  22. using ls::std::core::JniReturnValue;
  23. using ls::std::core::NullPointerArgumentEvaluator;
  24. using ls::std::core::NullPointerEvaluator;
  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}.evaluate();
  33. EmptyStringArgumentEvaluator{_path}.evaluate();
  34. NullPointerArgumentEvaluator{_parameter->getJavaEnvironment()}.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->_callByteMethod(_methodIdentifier, returnValue);
  46. this->_callIntMethod(_methodIdentifier, returnValue);
  47. }
  48. return returnValue;
  49. }
  50. bool JniClass::hasMethod(const string &_methodIdentifier)
  51. {
  52. return this->_hasMethod(_methodIdentifier);
  53. }
  54. bool JniClass::load()
  55. {
  56. this->javaClass = this->parameter->getJniApi()->findClass(this->path);
  57. return this->javaClass != nullptr;
  58. }
  59. bool JniClass::loadMethod(const string &_methodIdentifier, const string &_methodSignature)
  60. {
  61. NullPointerEvaluator{this->javaClass}.evaluate();
  62. jmethodID methodId = this->parameter->getJniApi()->getMethodId(this->javaClass, _methodIdentifier.c_str(), _methodSignature.c_str());
  63. bool succeeded = methodId != nullptr && !this->_hasMethod(_methodIdentifier);
  64. if (succeeded)
  65. {
  66. JniMethod method{_methodIdentifier, _methodSignature};
  67. method.setMethodId(methodId);
  68. succeeded = this->methods.insert(make_pair<string, JniMethod>(string{_methodIdentifier}, JniMethod{method})).second;
  69. }
  70. return succeeded;
  71. }
  72. void JniClass::_callByteMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  73. {
  74. JniMethod method = this->methods.at(_methodIdentifier);
  75. string searchString = ")B";
  76. string methodSignature = method.getMethodSignature();
  77. bool hasByteReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  78. if (hasByteReturnType)
  79. {
  80. _returnValue.setByteValue(this->parameter->getJniApi()->callByteMethod(this->parameter->getJavaObject(), method.getMethodId()));
  81. }
  82. }
  83. void JniClass::_callIntMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
  84. {
  85. JniMethod method = this->methods.at(_methodIdentifier);
  86. string searchString = ")I";
  87. string methodSignature = method.getMethodSignature();
  88. bool hasIntegerReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
  89. if (hasIntegerReturnType)
  90. {
  91. _returnValue.setIntegerValue(this->parameter->getJniApi()->callIntMethod(this->parameter->getJavaObject(), method.getMethodId()));
  92. }
  93. }
  94. void JniClass::_createJniApi()
  95. {
  96. this->parameter->setJniApi(make_shared<JniApi>(this->parameter->getJavaEnvironment()));
  97. }
  98. bool JniClass::_hasMethod(const string &_methodIdentifier)
  99. {
  100. return this->methods.find(_methodIdentifier) != this->methods.end();
  101. }