From 941f49b0188c6f396a6dd6174c791029a0bed6ab Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 12 Sep 2023 11:37:37 +0200 Subject: [PATCH] JNI: treat enums as their underlying types Android APIs use integer constants like enum values, so they are mapped to one of the integeral types (jint, jshort, jlong etc) on the C++ side. Enable C++ code to declare an equivalent enum (scoped or unscoped), and to use that enum as a type in JNI calls by treating it as the underlying type in the signature() generator. Add a helper type trait that maps enums to their underlying type and other integral types to themselves (we can't use std::underlying_type_t on a non-enum type). Add tests. Note: Java Enums are special classes with fields; this change does not add any special support for those. Change-Id: Iec430a1553152dcf7a24209aaebbeceb1c6e38a8 Reviewed-by: Petri Virkkunen Reviewed-by: Zoltan Gera Reviewed-by: Juha Vuolle --- src/corelib/kernel/qjniobject.h | 135 ++++++++++-------- src/corelib/kernel/qjnitypes_impl.h | 2 + .../kernel/qjniobject/tst_qjniobject.cpp | 47 +++++- .../kernel/qjnitypes/tst_qjnitypes.cpp | 17 +++ 4 files changed, 137 insertions(+), 64 deletions(-) diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index e5212d36ff8..d469c339216 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -531,6 +531,21 @@ private: friend bool operator==(const QJniObject &, const QJniObject &); friend bool operator!=(const QJniObject&, const QJniObject&); + template + struct IntegerTypeDetail + { + using type = T; + }; + template + struct IntegerTypeDetail>> + { + using type = std::underlying_type_t; + }; + + template + static constexpr bool likeIntegerType = std::is_same_v + || std::is_same_v::type, Want>; + template static constexpr void callMethodForType(JNIEnv *env, T &res, jobject obj, jmethodID id, ...) @@ -540,16 +555,16 @@ private: if constexpr (std::is_same_v) res = env->CallBooleanMethodV(obj, id, args); - else if constexpr (std::is_same_v) - res = env->CallByteMethodV(obj, id, args); - else if constexpr (std::is_same_v) - res = env->CallCharMethodV(obj, id, args); - else if constexpr (std::is_same_v) - res = env->CallShortMethodV(obj, id, args); - else if constexpr (std::is_same_v) - res = env->CallIntMethodV(obj, id, args); - else if constexpr (std::is_same_v) - res = env->CallLongMethodV(obj, id, args); + else if constexpr (likeIntegerType) + res = T{env->CallByteMethodV(obj, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallCharMethodV(obj, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallShortMethodV(obj, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallIntMethodV(obj, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallLongMethodV(obj, id, args)}; else if constexpr (std::is_same_v) res = env->CallFloatMethodV(obj, id, args); else if constexpr (std::is_same_v) @@ -569,16 +584,16 @@ private: va_start(args, id); if constexpr (std::is_same_v) res = env->CallStaticBooleanMethodV(clazz, id, args); - else if constexpr (std::is_same_v) - res = env->CallStaticByteMethodV(clazz, id, args); - else if constexpr (std::is_same_v) - res = env->CallStaticCharMethodV(clazz, id, args); - else if constexpr (std::is_same_v) - res = env->CallStaticShortMethodV(clazz, id, args); - else if constexpr (std::is_same_v) - res = env->CallStaticIntMethodV(clazz, id, args); - else if constexpr (std::is_same_v) - res = env->CallStaticLongMethodV(clazz, id, args); + else if constexpr (likeIntegerType) + res = T{env->CallStaticByteMethodV(clazz, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallStaticCharMethodV(clazz, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallStaticShortMethodV(clazz, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallStaticIntMethodV(clazz, id, args)}; + else if constexpr (likeIntegerType) + res = T{env->CallStaticLongMethodV(clazz, id, args)}; else if constexpr (std::is_same_v) res = env->CallStaticFloatMethodV(clazz, id, args); else if constexpr (std::is_same_v) @@ -605,16 +620,16 @@ private: { if constexpr (std::is_same_v) res = env->GetBooleanField(obj, id); - else if constexpr (std::is_same_v) - res = env->GetByteField(obj, id); - else if constexpr (std::is_same_v) - res = env->GetCharField(obj, id); - else if constexpr (std::is_same_v) - res = env->GetShortField(obj, id); - else if constexpr (std::is_same_v) - res = env->GetIntField(obj, id); - else if constexpr (std::is_same_v) - res = env->GetLongField(obj, id); + else if constexpr (likeIntegerType) + res = T{env->GetByteField(obj, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetCharField(obj, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetShortField(obj, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetIntField(obj, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetLongField(obj, id)}; else if constexpr (std::is_same_v) res = env->GetFloatField(obj, id); else if constexpr (std::is_same_v) @@ -629,16 +644,16 @@ private: { if constexpr (std::is_same_v) res = env->GetStaticBooleanField(clazz, id); - else if constexpr (std::is_same_v) - res = env->GetStaticByteField(clazz, id); - else if constexpr (std::is_same_v) - res = env->GetStaticCharField(clazz, id); - else if constexpr (std::is_same_v) - res = env->GetStaticShortField(clazz, id); - else if constexpr (std::is_same_v) - res = env->GetStaticIntField(clazz, id); - else if constexpr (std::is_same_v) - res = env->GetStaticLongField(clazz, id); + else if constexpr (likeIntegerType) + res = T{env->GetStaticByteField(clazz, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetStaticCharField(clazz, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetStaticShortField(clazz, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetStaticIntField(clazz, id)}; + else if constexpr (likeIntegerType) + res = T{env->GetStaticLongField(clazz, id)}; else if constexpr (std::is_same_v) res = env->GetStaticFloatField(clazz, id); else if constexpr (std::is_same_v) @@ -653,16 +668,16 @@ private: { if constexpr (std::is_same_v) env->SetBooleanField(obj, id, value); - else if constexpr (std::is_same_v) - env->SetByteField(obj, id, value); - else if constexpr (std::is_same_v) - env->SetCharField(obj, id, value); - else if constexpr (std::is_same_v) - env->SetShortField(obj, id, value); - else if constexpr (std::is_same_v) - env->SetIntField(obj, id, value); - else if constexpr (std::is_same_v) - env->SetLongField(obj, id, value); + else if constexpr (likeIntegerType) + env->SetByteField(obj, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetCharField(obj, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetShortField(obj, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetIntField(obj, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetLongField(obj, id, static_cast(value)); else if constexpr (std::is_same_v) env->SetFloatField(obj, id, value); else if constexpr (std::is_same_v) @@ -679,16 +694,16 @@ private: { if constexpr (std::is_same_v) env->SetStaticBooleanField(clazz, id, value); - else if constexpr (std::is_same_v) - env->SetStaticByteField(clazz, id, value); - else if constexpr (std::is_same_v) - env->SetStaticCharField(clazz, id, value); - else if constexpr (std::is_same_v) - env->SetStaticShortField(clazz, id, value); - else if constexpr (std::is_same_v) - env->SetStaticIntField(clazz, id, value); - else if constexpr (std::is_same_v) - env->SetStaticLongField(clazz, id, value); + else if constexpr (likeIntegerType) + env->SetStaticByteField(clazz, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetStaticCharField(clazz, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetStaticShortField(clazz, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetStaticIntField(clazz, id, static_cast(value)); + else if constexpr (likeIntegerType) + env->SetStaticLongField(clazz, id, static_cast(value)); else if constexpr (std::is_same_v) env->SetStaticFloatField(clazz, id, value); else if constexpr (std::is_same_v) diff --git a/src/corelib/kernel/qjnitypes_impl.h b/src/corelib/kernel/qjnitypes_impl.h index 7b8bd1819af..5f87c97e5b6 100644 --- a/src/corelib/kernel/qjnitypes_impl.h +++ b/src/corelib/kernel/qjnitypes_impl.h @@ -237,6 +237,8 @@ struct Traits { return CTString("D"); } else if constexpr (std::is_same_v) { return CTString("V"); + } else if constexpr (std::is_enum_v) { + return Traits>::signature(); } // else: return void -> not implemented } diff --git a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp index b524997961f..cc3ebc658eb 100644 --- a/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp +++ b/tests/auto/corelib/kernel/qjniobject/tst_qjniobject.cpp @@ -847,6 +847,10 @@ void tst_QJniObject::getStaticIntField() jint i = QJniObject::getStaticField(cls, "SIZE"); QCOMPARE(i, 64); + + enum class Enum { SIZE = 64 }; + Enum e = QJniObject::getStaticField(cls, "SIZE"); + QCOMPARE(e, Enum::SIZE); } void tst_QJniObject::getStaticByteFieldClassName() @@ -863,6 +867,10 @@ void tst_QJniObject::getStaticByteField() jbyte i = QJniObject::getStaticField(cls, "MAX_VALUE"); QCOMPARE(i, jbyte(127)); + + enum class Enum : jbyte { MAX_VALUE = 127 }; + Enum e = QJniObject::getStaticField(cls, "MAX_VALUE"); + QCOMPARE(e, Enum::MAX_VALUE); } void tst_QJniObject::getStaticLongFieldClassName() @@ -879,6 +887,10 @@ void tst_QJniObject::getStaticLongField() jlong i = QJniObject::getStaticField(cls, "MAX_VALUE"); QCOMPARE(i, jlong(9223372036854775807L)); + + enum class Enum : jlong { MAX_VALUE = 9223372036854775807L }; + Enum e = QJniObject::getStaticField(cls, "MAX_VALUE"); + QCOMPARE(e, Enum::MAX_VALUE); } void tst_QJniObject::getStaticDoubleFieldClassName() @@ -931,6 +943,9 @@ void tst_QJniObject::getStaticShortField() jshort i = QJniObject::getStaticField(cls, "MAX_VALUE"); QCOMPARE(i, jshort(32767)); + enum class Enum : jshort { MAX_VALUE = 32767 }; + Enum e = QJniObject::getStaticField(cls, "MAX_VALUE"); + QCOMPARE(e, Enum::MAX_VALUE); } void tst_QJniObject::getStaticCharFieldClassName() @@ -947,6 +962,10 @@ void tst_QJniObject::getStaticCharField() jchar i = QJniObject::getStaticField(cls, "MAX_VALUE"); QCOMPARE(i, jchar(0xffff)); + + enum class Enum : jchar { MAX_VALUE = 0xffff }; + Enum e = QJniObject::getStaticField(cls, "MAX_VALUE"); + QCOMPARE(e, Enum::MAX_VALUE); } @@ -982,16 +1001,22 @@ void setField(const char *fieldName, T testValue) void tst_QJniObject::setIntField() { setField("INT_VAR", 555); + enum class Enum : jint { VALUE = 555 }; + setField("INT_VAR", Enum::VALUE); } void tst_QJniObject::setByteField() { - setField("BYTE_VAR", jbyte(555)); + setField("BYTE_VAR", jbyte(123)); + enum class Enum : jbyte { VALUE = 123 }; + setField("BYTE_VAR", Enum::VALUE); } void tst_QJniObject::setLongField() { setField("LONG_VAR", jlong(9223372036847758232L)); + enum class Enum : jlong { VALUE = 9223372036847758232L }; + setField("LONG_VAR", Enum::VALUE); } void tst_QJniObject::setDoubleField() @@ -1006,12 +1031,16 @@ void tst_QJniObject::setFloatField() void tst_QJniObject::setShortField() { - setField("SHORT_VAR", jshort(123)); + setField("SHORT_VAR", jshort(555)); + enum class Enum : jshort { VALUE = 555 }; + setField("SHORT_VAR", Enum::VALUE); } void tst_QJniObject::setCharField() { setField("CHAR_VAR", jchar('A')); + enum class Enum : jchar { VALUE = 'A' }; + setField("CHAR_VAR", Enum::VALUE); } void tst_QJniObject::setBooleanField() @@ -1049,16 +1078,22 @@ void setStaticField(const char *fieldName, T testValue) void tst_QJniObject::setStaticIntField() { setStaticField("S_INT_VAR", 555); + enum class Enum : jint { VALUE = 555 }; + setStaticField("S_INT_VAR", Enum::VALUE); } void tst_QJniObject::setStaticByteField() { - setStaticField("S_BYTE_VAR", jbyte(555)); + setStaticField("S_BYTE_VAR", jbyte(123)); + enum class Enum : jbyte { VALUE = 123 }; + setStaticField("S_BYTE_VAR", Enum::VALUE); } void tst_QJniObject::setStaticLongField() { setStaticField("S_LONG_VAR", jlong(9223372036847758232L)); + enum class Enum : jlong { VALUE = 9223372036847758232L }; + setStaticField("S_LONG_VAR", Enum::VALUE); } void tst_QJniObject::setStaticDoubleField() @@ -1073,12 +1108,16 @@ void tst_QJniObject::setStaticFloatField() void tst_QJniObject::setStaticShortField() { - setStaticField("S_SHORT_VAR", jshort(123)); + setStaticField("S_SHORT_VAR", jshort(555)); + enum class Enum : jshort { VALUE = 555 }; + setStaticField("S_SHORT_VAR", Enum::VALUE); } void tst_QJniObject::setStaticCharField() { setStaticField("S_CHAR_VAR", jchar('A')); + enum class Enum : jchar { VALUE = 'A' }; + setStaticField("S_CHAR_VAR", Enum::VALUE); } void tst_QJniObject::setStaticBooleanField() diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp index b7685538f19..cbffb24ff88 100644 --- a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp +++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp @@ -115,6 +115,23 @@ static_assert(!QtJniTypes::CTString("ABCDE").endsWith("ABCDEF")); static_assert(QtJniTypes::CTString("ABCDE").endsWith('E')); static_assert(!QtJniTypes::CTString("ABCDE").endsWith('F')); +enum UnscopedEnum {}; +enum class ScopedEnum {}; +enum class IntEnum : int {}; +enum class UnsignedEnum : unsigned {}; +enum class Int8Enum : int8_t {}; +enum class ShortEnum : short {}; +enum class LongEnum : long {}; +enum class JIntEnum : jint {}; + +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "B"); +static_assert(QtJniTypes::Traits::signature() == "J"); +static_assert(QtJniTypes::Traits::signature() == "I"); + void tst_QJniTypes::initTestCase() {