|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
| 18 | +#include <cstring> |
18 | 19 | #include <jni.h>
|
19 | 20 | #include <lib/support/CHIPJNIError.h>
|
20 | 21 | #include <lib/support/CodeUtils.h>
|
@@ -79,8 +80,17 @@ CHIP_ERROR JniReferences::GetClassRef(JNIEnv * env, const char * clsType, jclass
|
79 | 80 | CHIP_ERROR err = CHIP_NO_ERROR;
|
80 | 81 | jclass cls = nullptr;
|
81 | 82 |
|
82 |
| - cls = env->FindClass(clsType); |
83 |
| - env->ExceptionClear(); |
| 83 | + // Try `j$/util/Optional` when enabling Java8. |
| 84 | + if (strcmp(clsType, "java/util/Optional") == 0) |
| 85 | + { |
| 86 | + cls = env->FindClass("j$/util/Optional"); |
| 87 | + } |
| 88 | + |
| 89 | + if (cls == nullptr) |
| 90 | + { |
| 91 | + env->ExceptionClear(); |
| 92 | + cls = env->FindClass(clsType); |
| 93 | + } |
84 | 94 |
|
85 | 95 | if (cls == nullptr)
|
86 | 96 | {
|
@@ -121,6 +131,20 @@ CHIP_ERROR JniReferences::FindMethod(JNIEnv * env, jobject object, const char *
|
121 | 131 | VerifyOrReturnError(javaClass != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
|
122 | 132 |
|
123 | 133 | *methodId = env->GetMethodID(javaClass, methodName, methodSignature);
|
| 134 | + |
| 135 | + // Try `j$` when enabling Java8. |
| 136 | + std::string methodSignature_java8_str(methodSignature); |
| 137 | + if (*methodId == nullptr && methodSignature_java8_str.find("java/util/Optional") != std::string::npos) |
| 138 | + { |
| 139 | + // Replace all "java/util/Optional" with "j$/util/Optional". |
| 140 | + while (methodSignature_java8_str.find("java/util/Optional") != std::string::npos) |
| 141 | + { |
| 142 | + size_t pos = methodSignature_java8_str.find("java/util/Optional"); |
| 143 | + methodSignature_java8_str.replace(pos, strlen("java/util/Optional"), "j$/util/Optional"); |
| 144 | + } |
| 145 | + *methodId = env->GetMethodID(javaClass, methodName, methodSignature_java8_str.c_str()); |
| 146 | + } |
| 147 | + |
124 | 148 | VerifyOrReturnError(*methodId != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND);
|
125 | 149 |
|
126 | 150 | return err;
|
@@ -193,6 +217,13 @@ CHIP_ERROR JniReferences::CreateOptional(jobject objectToWrap, jobject & outOpti
|
193 | 217 | chip::JniClass jniClass(optionalCls);
|
194 | 218 |
|
195 | 219 | jmethodID ofMethod = env->GetStaticMethodID(optionalCls, "ofNullable", "(Ljava/lang/Object;)Ljava/util/Optional;");
|
| 220 | + |
| 221 | + // Try `Lj$/util/Optional;` when enabling Java8. |
| 222 | + if (ofMethod == nullptr) |
| 223 | + { |
| 224 | + ofMethod = env->GetStaticMethodID(optionalCls, "ofNullable", "(Ljava/lang/Object;)Lj$/util/Optional;"); |
| 225 | + } |
| 226 | + |
196 | 227 | VerifyOrReturnError(ofMethod != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND);
|
197 | 228 | outOptional = env->CallStaticObjectMethod(optionalCls, ofMethod, objectToWrap);
|
198 | 229 |
|
|
0 commit comments