Forráskód Böngészése

Add JNI char getter method support

Patrick-Christopher Mattulat 1 éve
szülő
commit
e55b27d11f

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

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-07
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -25,6 +25,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 jint callIntMethod(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;

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

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-07
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -24,6 +24,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;
       jint callIntMethod(jobject _javaObject, jmethodID _methodId) override;
       jclass findClass(const ::std::string &_classPath) override;
       jmethodID getMethodId(jclass _javaClass, const char *_methodIdentifier, const char *_methodSignature) override;

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

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-07
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -43,6 +43,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 _callIntMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue);
       void _createJniApi();
       [[nodiscard]] bool _hasMethod(const ::std::string &_methodIdentifier);

+ 4 - 1
include/ls-std/core/jni/JniReturnValue.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-08
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -24,15 +24,18 @@ namespace ls::std::core::experimental
 
       [[nodiscard]] jboolean getBooleanValue() const;
       [[nodiscard]] jbyte getByteValue() const;
+      [[nodiscard]] jchar getCharValue() const;
       [[nodiscard]] jint getIntegerValue() const;
       void setBooleanValue(jboolean _booleanValue);
       void setByteValue(jbyte _byteValue);
+      void setCharValue(jchar _charValue);
       void setIntegerValue(jint _integerValue);
 
     private:
 
       jboolean booleanValue{};
       jbyte byteValue{};
+      jchar charValue{};
       jint integerValue{};
   };
 }

+ 6 - 1
source/ls-std/core/jni/JniApi.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-07
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -32,6 +32,11 @@ jbyte JniApi::callByteMethod(jobject _javaObject, jmethodID _methodId)
   return this->environment->CallByteMethod(_javaObject, _methodId);
 }
 
+jchar JniApi::callCharMethod(jobject _javaObject, jmethodID _methodId)
+{
+  return this->environment->CallCharMethod(_javaObject, _methodId);
+}
+
 jint JniApi::callIntMethod(jobject _javaObject, jmethodID _methodId)
 {
   return this->environment->CallIntMethod(_javaObject, _methodId);

+ 17 - 3
source/ls-std/core/jni/JniClass.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-07
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -50,6 +50,7 @@ JniReturnValue JniClass::callMethod(const string &_methodIdentifier)
   {
     this->_callBooleanMethod(_methodIdentifier, returnValue);
     this->_callByteMethod(_methodIdentifier, returnValue);
+    this->_callCharMethod(_methodIdentifier, returnValue);
     this->_callIntMethod(_methodIdentifier, returnValue);
   }
 
@@ -83,7 +84,7 @@ bool JniClass::loadMethod(const string &_methodIdentifier, const string &_method
   return succeeded;
 }
 
-void JniClass::_callBooleanMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue)
+void JniClass::_callBooleanMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
 {
   JniMethod method = this->methods.at(_methodIdentifier);
   string searchString = ")Z";
@@ -96,7 +97,7 @@ void JniClass::_callBooleanMethod(const ::std::string &_methodIdentifier, ls::st
   }
 }
 
-void JniClass::_callByteMethod(const ::std::string &_methodIdentifier, ls::std::core::experimental::JniReturnValue &_returnValue)
+void JniClass::_callByteMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
 {
   JniMethod method = this->methods.at(_methodIdentifier);
   string searchString = ")B";
@@ -109,6 +110,19 @@ void JniClass::_callByteMethod(const ::std::string &_methodIdentifier, ls::std::
   }
 }
 
+void JniClass::_callCharMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
+{
+  JniMethod method = this->methods.at(_methodIdentifier);
+  string searchString = ")C";
+  string methodSignature = method.getMethodSignature();
+  bool hasCharReturnType = methodSignature.rfind(searchString) == (methodSignature.size() - searchString.size());
+
+  if (hasCharReturnType)
+  {
+    _returnValue.setCharValue(this->parameter->getJniApi()->callCharMethod(this->parameter->getJavaObject(), method.getMethodId()));
+  }
+}
+
 void JniClass::_callIntMethod(const string &_methodIdentifier, JniReturnValue &_returnValue)
 {
   JniMethod method = this->methods.at(_methodIdentifier);

+ 11 - 1
source/ls-std/core/jni/JniReturnValue.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-08
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -25,6 +25,11 @@ jbyte JniReturnValue::getByteValue() const
   return this->byteValue;
 }
 
+jchar JniReturnValue::getCharValue() const
+{
+  return this->charValue;
+}
+
 jint JniReturnValue::getIntegerValue() const
 {
   return this->integerValue;
@@ -40,6 +45,11 @@ void JniReturnValue::setByteValue(jbyte _byteValue)
   this->byteValue = _byteValue;
 }
 
+void JniReturnValue::setCharValue(jchar _charValue)
+{
+  this->charValue = _charValue;
+}
+
 void JniReturnValue::setIntegerValue(jint _integerValue)
 {
   this->integerValue = _integerValue;

+ 23 - 1
test/cases/core/jni/JniClassTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-07
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -153,6 +153,28 @@ namespace
     ASSERT_EQ(22, javaClass.callMethod(methodIdentifier).getByteValue());
   }
 
+  TEST_F(JniClassTest, callMethod_char_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 = "getLetter";
+    string methodSignature = "()C";
+    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, callCharMethod(testing::_, methodId)).Times(AtLeast(1));
+    ON_CALL(*this->jniApi, callCharMethod(testing::_, methodId)).WillByDefault(Return('P'));
+
+    ASSERT_TRUE(javaClass.load());
+    ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
+    ASSERT_EQ('P', javaClass.callMethod(methodIdentifier).getCharValue());
+  }
+
   TEST_F(JniClassTest, callMethod_integer_return_value)
   {
     string classPath = "java.utils.String";

+ 15 - 1
test/cases/core/jni/JniReturnValueTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-08
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -35,6 +35,12 @@ namespace
     ASSERT_EQ(0, returnValue.getByteValue());
   }
 
+  TEST_F(JniReturnValueTest, getCharValue)
+  {
+    JniReturnValue returnValue{};
+    ASSERT_EQ(0, (int) returnValue.getCharValue());
+  }
+
   TEST_F(JniReturnValueTest, getIntegerValue)
   {
     JniReturnValue returnValue{};
@@ -57,6 +63,14 @@ namespace
     ASSERT_EQ(22, returnValue.getByteValue());
   }
 
+  TEST_F(JniReturnValueTest, setCharValue)
+  {
+    JniReturnValue returnValue{};
+    returnValue.setCharValue('P');
+
+    ASSERT_EQ('P', returnValue.getCharValue());
+  }
+
   TEST_F(JniReturnValueTest, setIntegerValue)
   {
     JniReturnValue returnValue{};

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

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-04-07
-* Changed:         2023-04-10
+* Changed:         2023-04-11
 *
 * */
 
@@ -24,6 +24,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(jint, callIntMethod, (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));