Ver Fonte

Add JNI long getter method support

Patrick-Christopher Mattulat há 1 ano atrás
pai
commit
578da8b9ba

+ 1 - 0
include/ls-std/core/interface/IJniApi.hpp

@@ -27,6 +27,7 @@ namespace ls::std::core::experimental::interface_type
       virtual jbyte callByteMethod(jobject _javaObject, jmethodID _methodId) = 0;
       virtual jchar callCharMethod(jobject _javaObject, jmethodID _methodId) = 0;
       virtual jint callIntMethod(jobject _javaObject, jmethodID _methodId) = 0;
+      virtual jlong callLongMethod(jobject _javaObject, jmethodID _methodId) = 0;
       virtual jshort callShortMethod(jobject _javaObject, jmethodID _methodId) = 0;
       virtual jclass findClass(const ::std::string &_classPath) = 0;
       virtual jmethodID getMethodId(jclass _javaClass, const char *_methodIdentifier, const char *_methodSignature) = 0;

+ 1 - 0
include/ls-std/core/jni/JniApi.hpp

@@ -26,6 +26,7 @@ namespace ls::std::core::experimental
       jbyte callByteMethod(jobject _javaObject, jmethodID _methodId) override;
       jchar callCharMethod(jobject _javaObject, jmethodID _methodId) override;
       jint callIntMethod(jobject _javaObject, jmethodID _methodId) override;
+      jlong callLongMethod(jobject _javaObject, jmethodID _methodId) override;
       jshort callShortMethod(jobject _javaObject, jmethodID _methodId) override;
       jclass findClass(const ::std::string &_classPath) override;
       jmethodID getMethodId(jclass _javaClass, const char *_methodIdentifier, const char *_methodSignature) override;

+ 1 - 0
include/ls-std/core/jni/JniClass.hpp

@@ -45,6 +45,7 @@ namespace ls::std::core::experimental
       void _callByteMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue);
       void _callCharMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue);
       void _callIntMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue);
+      void _callLongMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue);
       void _callShortMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue);
       void _createJniApi();
       [[nodiscard]] bool _hasMethod(const ::std::string &_methodIdentifier);

+ 3 - 0
include/ls-std/core/jni/JniReturnValue.hpp

@@ -26,11 +26,13 @@ namespace ls::std::core::experimental
       [[nodiscard]] jbyte getByteValue() const;
       [[nodiscard]] jchar getCharValue() const;
       [[nodiscard]] jint getIntegerValue() const;
+      [[nodiscard]] jlong getLongValue() const;
       [[nodiscard]] jshort getShortValue() const;
       void setBooleanValue(jboolean _booleanValue);
       void setByteValue(jbyte _byteValue);
       void setCharValue(jchar _charValue);
       void setIntegerValue(jint _integerValue);
+      void setLongValue(jlong _longValue);
       void setShortValue(jshort _shortValue);
 
     private:
@@ -39,6 +41,7 @@ namespace ls::std::core::experimental
       jbyte byteValue{};
       jchar charValue{};
       jint integerValue{};
+      jlong longValue{};
       jshort shortValue{};
   };
 }

+ 5 - 0
source/ls-std/core/jni/JniApi.cpp

@@ -42,6 +42,11 @@ jint JniApi::callIntMethod(jobject _javaObject, jmethodID _methodId)
   return this->environment->CallIntMethod(_javaObject, _methodId);
 }
 
+jlong JniApi::callLongMethod(jobject _javaObject, jmethodID _methodId)
+{
+  return this->environment->CallLongMethod(_javaObject, _methodId);
+}
+
 jshort JniApi::callShortMethod(jobject _javaObject, jmethodID _methodId)
 {
   return this->environment->CallShortMethod(_javaObject, _methodId);

+ 14 - 0
source/ls-std/core/jni/JniClass.cpp

@@ -52,6 +52,7 @@ JniReturnValue JniClass::callMethod(const string &_methodIdentifier)
     this->_callByteMethod(_methodIdentifier, returnValue);
     this->_callCharMethod(_methodIdentifier, returnValue);
     this->_callIntMethod(_methodIdentifier, returnValue);
+    this->_callLongMethod(_methodIdentifier, returnValue);
     this->_callShortMethod(_methodIdentifier, returnValue);
   }
 
@@ -137,6 +138,19 @@ void JniClass::_callIntMethod(const string &_methodIdentifier, JniReturnValue &_
   }
 }
 
+void JniClass::_callLongMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
+{
+  JniMethod method = this->methods.at(_methodIdentifier);
+  string searchString = ")J";
+  string methodSignature = method.getMethodSignature();
+  bool hasLongReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
+
+  if (hasLongReturnType)
+  {
+    _returnValue.setLongValue(this->parameter->getJniApi()->callLongMethod(this->parameter->getJavaObject(), method.getMethodId()));
+  }
+}
+
 void JniClass::_callShortMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
 {
   JniMethod method = this->methods.at(_methodIdentifier);

+ 10 - 0
source/ls-std/core/jni/JniReturnValue.cpp

@@ -35,6 +35,11 @@ jint JniReturnValue::getIntegerValue() const
   return this->integerValue;
 }
 
+jlong JniReturnValue::getLongValue() const
+{
+  return this->longValue;
+}
+
 jshort JniReturnValue::getShortValue() const
 {
   return this->shortValue;
@@ -60,6 +65,11 @@ void JniReturnValue::setIntegerValue(jint _integerValue)
   this->integerValue = _integerValue;
 }
 
+void JniReturnValue::setLongValue(jlong _longValue)
+{
+  this->longValue = _longValue;
+}
+
 void JniReturnValue::setShortValue(jshort _shortValue)
 {
   this->shortValue = _shortValue;

+ 22 - 0
test/cases/core/jni/JniClassTest.cpp

@@ -197,6 +197,28 @@ namespace
     ASSERT_EQ(1989, javaClass.callMethod(methodIdentifier).getIntegerValue());
   }
 
+  TEST_F(JniClassTest, callMethod_long_return_value)
+  {
+    string classPath = "java.utils.String";
+    JniClass javaClass = this->createJniClass(classPath);
+
+    EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
+    ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
+
+    string methodIdentifier = "getAmountOfMoney";
+    string methodSignature = "()J";
+    EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
+    jmethodID methodId = (jmethodID) make_shared<int>().get();
+    ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
+
+    EXPECT_CALL(*this->jniApi, callLongMethod(testing::_, methodId)).Times(AtLeast(1));
+    ON_CALL(*this->jniApi, callLongMethod(testing::_, methodId)).WillByDefault(Return(18837828981));
+
+    ASSERT_TRUE(javaClass.load());
+    ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
+    ASSERT_EQ(18837828981, javaClass.callMethod(methodIdentifier).getLongValue());
+  }
+
   TEST_F(JniClassTest, callMethod_short_return_value)
   {
     string classPath = "java.utils.String";

+ 14 - 0
test/cases/core/jni/JniReturnValueTest.cpp

@@ -47,6 +47,12 @@ namespace
     ASSERT_EQ(0, returnValue.getIntegerValue());
   }
 
+  TEST_F(JniReturnValueTest, getLongValue)
+  {
+    JniReturnValue returnValue{};
+    ASSERT_EQ(0, returnValue.getLongValue());
+  }
+
   TEST_F(JniReturnValueTest, getShortValue)
   {
     JniReturnValue returnValue{};
@@ -85,6 +91,14 @@ namespace
     ASSERT_EQ(1989, returnValue.getIntegerValue());
   }
 
+  TEST_F(JniReturnValueTest, setLongValue)
+  {
+    JniReturnValue returnValue{};
+    returnValue.setLongValue(16992831);
+
+    ASSERT_EQ(16992831, returnValue.getLongValue());
+  }
+
   TEST_F(JniReturnValueTest, setShortValue)
   {
     JniReturnValue returnValue{};

+ 1 - 0
test/classes/core/jni/MockJniApi.hpp

@@ -26,6 +26,7 @@ namespace test::core::jni
       MOCK_METHOD(jbyte, callByteMethod, (jobject _javaObject, jmethodID _methodId), (override));
       MOCK_METHOD(jchar, callCharMethod, (jobject _javaObject, jmethodID _methodId), (override));
       MOCK_METHOD(jint, callIntMethod, (jobject _javaObject, jmethodID _methodId), (override));
+      MOCK_METHOD(jlong, callLongMethod, (jobject _javaObject, jmethodID _methodId), (override));
       MOCK_METHOD(jshort, callShortMethod, (jobject _javaObject, jmethodID _methodId), (override));
       MOCK_METHOD(jclass, findClass, (const ::std::string &_classPath), (override));
       MOCK_METHOD(jmethodID, getMethodId, (jclass _javaClass, const char *_methodIdentifier, const char *_methodSignature), (override));