瀏覽代碼

Add JNI short getter method support

Patrick-Christopher Mattulat 1 年之前
父節點
當前提交
a35aea592c

+ 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 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;
+      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 _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,10 +26,12 @@ namespace ls::std::core::experimental
       [[nodiscard]] jbyte getByteValue() const;
       [[nodiscard]] jchar getCharValue() const;
       [[nodiscard]] jint getIntegerValue() const;
+      [[nodiscard]] jshort getShortValue() const;
       void setBooleanValue(jboolean _booleanValue);
       void setByteValue(jbyte _byteValue);
       void setCharValue(jchar _charValue);
       void setIntegerValue(jint _integerValue);
+      void setShortValue(jshort _shortValue);
 
     private:
 
@@ -37,6 +39,7 @@ namespace ls::std::core::experimental
       jbyte byteValue{};
       jchar charValue{};
       jint integerValue{};
+      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);
 }
 
+jshort JniApi::callShortMethod(jobject _javaObject, jmethodID _methodId)
+{
+  return this->environment->CallShortMethod(_javaObject, _methodId);
+}
+
 jclass JniApi::findClass(const string &_classPath)
 {
   return this->environment->FindClass(_classPath.c_str());

+ 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->_callShortMethod(_methodIdentifier, returnValue);
   }
 
   return returnValue;
@@ -136,6 +137,19 @@ void JniClass::_callIntMethod(const string &_methodIdentifier, JniReturnValue &_
   }
 }
 
+void JniClass::_callShortMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
+{
+  JniMethod method = this->methods.at(_methodIdentifier);
+  string searchString = ")S";
+  string methodSignature = method.getMethodSignature();
+  bool hasShortReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
+
+  if (hasShortReturnType)
+  {
+    _returnValue.setShortValue(this->parameter->getJniApi()->callShortMethod(this->parameter->getJavaObject(), method.getMethodId()));
+  }
+}
+
 void JniClass::_createJniApi()
 {
   this->parameter->setJniApi(make_shared<JniApi>(this->parameter->getJavaEnvironment()));

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

@@ -35,6 +35,11 @@ jint JniReturnValue::getIntegerValue() const
   return this->integerValue;
 }
 
+jshort JniReturnValue::getShortValue() const
+{
+  return this->shortValue;
+}
+
 void JniReturnValue::setBooleanValue(jboolean _booleanValue)
 {
   this->booleanValue = _booleanValue;
@@ -54,3 +59,8 @@ void JniReturnValue::setIntegerValue(jint _integerValue)
 {
   this->integerValue = _integerValue;
 }
+
+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_short_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 = "getDay";
+    string methodSignature = "()S";
+    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, callShortMethod(testing::_, methodId)).Times(AtLeast(1));
+    ON_CALL(*this->jniApi, callShortMethod(testing::_, methodId)).WillByDefault(Return(55));
+
+    ASSERT_TRUE(javaClass.load());
+    ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
+    ASSERT_EQ(55, javaClass.callMethod(methodIdentifier).getShortValue());
+  }
+
   TEST_F(JniClassTest, hasMethod)
   {
     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, getShortValue)
+  {
+    JniReturnValue returnValue{};
+    ASSERT_EQ(0, returnValue.getShortValue());
+  }
+
   TEST_F(JniReturnValueTest, setBooleanValue)
   {
     JniReturnValue returnValue{};
@@ -78,4 +84,12 @@ namespace
 
     ASSERT_EQ(1989, returnValue.getIntegerValue());
   }
+
+  TEST_F(JniReturnValueTest, setShortValue)
+  {
+    JniReturnValue returnValue{};
+    returnValue.setShortValue(13);
+
+    ASSERT_EQ(13, returnValue.getShortValue());
+  }
 }

+ 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(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));
   };