JniClass.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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-07
  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::NullPointerArgumentEvaluator;
  22. using ls::std::core::NullPointerEvaluator;
  23. using std::make_pair;
  24. using std::make_shared;
  25. using std::pair;
  26. using std::shared_ptr;
  27. using std::string;
  28. JniClass::JniClass(const shared_ptr<JniClassParameter> &_parameter, const string &_path)
  29. {
  30. NullPointerArgumentEvaluator{_parameter}.evaluate();
  31. EmptyStringArgumentEvaluator{_path}.evaluate();
  32. NullPointerArgumentEvaluator{_parameter->getJavaEnvironment()}.evaluate();
  33. this->parameter = _parameter;
  34. this->path = _path;
  35. ConditionalFunctionExecutor{_parameter->getJniApi() == nullptr}.execute([this]() { _createJniApi(); });
  36. }
  37. JniClass::~JniClass() = default;
  38. bool JniClass::hasMethod(const string &_methodIdentifier)
  39. {
  40. return this->_hasMethod(_methodIdentifier);
  41. }
  42. bool JniClass::load()
  43. {
  44. this->javaClass = this->parameter->getJniApi()->findClass(this->path);
  45. return this->javaClass != nullptr;
  46. }
  47. bool JniClass::loadMethod(const string &_methodIdentifier, const string &_methodSignature)
  48. {
  49. NullPointerEvaluator{this->javaClass}.evaluate();
  50. jmethodID methodId = this->parameter->getJniApi()->getMethodId(this->javaClass, _methodIdentifier.c_str(), _methodSignature.c_str());
  51. bool succeeded = methodId != nullptr && !this->_hasMethod(_methodIdentifier);
  52. if (succeeded)
  53. {
  54. succeeded = this->methods.insert(make_pair<string, jmethodID>(string{_methodIdentifier}, jmethodID{methodId})).second;
  55. }
  56. return succeeded;
  57. }
  58. void JniClass::_createJniApi()
  59. {
  60. this->parameter->setJniApi(make_shared<JniApi>(this->parameter->getJavaEnvironment()));
  61. }
  62. bool JniClass::_hasMethod(const string &_methodIdentifier)
  63. {
  64. return this->methods.find(_methodIdentifier) != this->methods.end();
  65. }