Prechádzať zdrojové kódy

Add JNI float getter method support

Patrick-Christopher Mattulat 1 rok pred
rodič
commit
51411e8b3b

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

@@ -26,6 +26,7 @@ namespace ls::std::core::experimental::interface_type
       virtual jboolean callBooleanMethod(jobject _javaObject, jmethodID _methodId) = 0;
       virtual jbyte callByteMethod(jobject _javaObject, jmethodID _methodId) = 0;
       virtual jchar callCharMethod(jobject _javaObject, jmethodID _methodId) = 0;
+      virtual jfloat callFloatMethod(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;

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

@@ -25,6 +25,7 @@ namespace ls::std::core::experimental
       jboolean callBooleanMethod(jobject _javaObject, jmethodID _methodId) override;
       jbyte callByteMethod(jobject _javaObject, jmethodID _methodId) override;
       jchar callCharMethod(jobject _javaObject, jmethodID _methodId) override;
+      jfloat callFloatMethod(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;

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

@@ -44,6 +44,7 @@ namespace ls::std::core::experimental
       void _callBooleanMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue);
       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 _callFloatMethod(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);

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

@@ -25,12 +25,14 @@ namespace ls::std::core::experimental
       [[nodiscard]] jboolean getBooleanValue() const;
       [[nodiscard]] jbyte getByteValue() const;
       [[nodiscard]] jchar getCharValue() const;
+      [[nodiscard]] jfloat getFloatValue() 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 setFloatValue(jfloat _floatValue);
       void setIntegerValue(jint _integerValue);
       void setLongValue(jlong _longValue);
       void setShortValue(jshort _shortValue);
@@ -40,6 +42,7 @@ namespace ls::std::core::experimental
       jboolean booleanValue{};
       jbyte byteValue{};
       jchar charValue{};
+      jfloat floatValue{};
       jint integerValue{};
       jlong longValue{};
       jshort shortValue{};

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

@@ -37,6 +37,11 @@ jchar JniApi::callCharMethod(jobject _javaObject, jmethodID _methodId)
   return this->environment->CallCharMethod(_javaObject, _methodId);
 }
 
+jfloat JniApi::callFloatMethod(jobject _javaObject, jmethodID _methodId)
+{
+  return this->environment->CallFloatMethod(_javaObject, _methodId);
+}
+
 jint JniApi::callIntMethod(jobject _javaObject, jmethodID _methodId)
 {
   return this->environment->CallIntMethod(_javaObject, _methodId);

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

@@ -51,6 +51,7 @@ JniReturnValue JniClass::callMethod(const string &_methodIdentifier)
     this->_callBooleanMethod(_methodIdentifier, returnValue);
     this->_callByteMethod(_methodIdentifier, returnValue);
     this->_callCharMethod(_methodIdentifier, returnValue);
+    this->_callFloatMethod(_methodIdentifier, returnValue);
     this->_callIntMethod(_methodIdentifier, returnValue);
     this->_callLongMethod(_methodIdentifier, returnValue);
     this->_callShortMethod(_methodIdentifier, returnValue);
@@ -125,6 +126,19 @@ void JniClass::_callCharMethod(const string &_methodIdentifier, JniReturnValue &
   }
 }
 
+void JniClass::_callFloatMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
+{
+  JniMethod method = this->methods.at(_methodIdentifier);
+  string searchString = ")F";
+  string methodSignature = method.getMethodSignature();
+  bool hasFloatReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
+
+  if (hasFloatReturnType)
+  {
+    _returnValue.setFloatValue(this->parameter->getJniApi()->callFloatMethod(this->parameter->getJavaObject(), method.getMethodId()));
+  }
+}
+
 void JniClass::_callIntMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
 {
   JniMethod method = this->methods.at(_methodIdentifier);

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

@@ -30,6 +30,11 @@ jchar JniReturnValue::getCharValue() const
   return this->charValue;
 }
 
+jfloat JniReturnValue::getFloatValue() const
+{
+  return this->floatValue;
+}
+
 jint JniReturnValue::getIntegerValue() const
 {
   return this->integerValue;
@@ -60,6 +65,11 @@ void JniReturnValue::setCharValue(jchar _charValue)
   this->charValue = _charValue;
 }
 
+void JniReturnValue::setFloatValue(jfloat _floatValue)
+{
+  this->floatValue = _floatValue;
+}
+
 void JniReturnValue::setIntegerValue(jint _integerValue)
 {
   this->integerValue = _integerValue;

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

@@ -175,6 +175,28 @@ namespace
     ASSERT_EQ('P', javaClass.callMethod(methodIdentifier).getCharValue());
   }
 
+  TEST_F(JniClassTest, callMethod_float_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 = "getPi";
+    string methodSignature = "()F";
+    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, callFloatMethod(testing::_, methodId)).Times(AtLeast(1));
+    ON_CALL(*this->jniApi, callFloatMethod(testing::_, methodId)).WillByDefault(Return(3.14159f));
+
+    ASSERT_TRUE(javaClass.load());
+    ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
+    ASSERT_FLOAT_EQ(3.14159f, javaClass.callMethod(methodIdentifier).getFloatValue());
+  }
+
   TEST_F(JniClassTest, callMethod_integer_return_value)
   {
     string classPath = "java.utils.String";

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

@@ -41,6 +41,12 @@ namespace
     ASSERT_EQ(0, (int) returnValue.getCharValue());
   }
 
+  TEST_F(JniReturnValueTest, getFloatValue)
+  {
+    JniReturnValue returnValue{};
+    ASSERT_FLOAT_EQ(0.0f, returnValue.getFloatValue());
+  }
+
   TEST_F(JniReturnValueTest, getIntegerValue)
   {
     JniReturnValue returnValue{};
@@ -83,6 +89,14 @@ namespace
     ASSERT_EQ('P', returnValue.getCharValue());
   }
 
+  TEST_F(JniReturnValueTest, setFloatValue)
+  {
+    JniReturnValue returnValue{};
+    returnValue.setFloatValue(13.56f);
+
+    ASSERT_FLOAT_EQ(13.56f, returnValue.getFloatValue());
+  }
+
   TEST_F(JniReturnValueTest, setIntegerValue)
   {
     JniReturnValue returnValue{};

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

@@ -25,6 +25,7 @@ namespace test::core::jni
       MOCK_METHOD(jboolean, callBooleanMethod, (jobject _javaObject, jmethodID _methodId), (override));
       MOCK_METHOD(jbyte, callByteMethod, (jobject _javaObject, jmethodID _methodId), (override));
       MOCK_METHOD(jchar, callCharMethod, (jobject _javaObject, jmethodID _methodId), (override));
+      MOCK_METHOD(jfloat, callFloatMethod, (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));