From bf9596758f539c628c915940086e6d0f519573ee Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Wed, 24 May 2023 09:49:55 -0700 Subject: [PATCH] Revert "Use controller exception in Java controller (#26708)" (#26799) This reverts commit e022057aea58fee7f855d8994c7f971c5ac2c875. --- .../PairOnNetworkLongImSubscribeCommand.kt | 2 +- .../jni/com/chip/casting/MatterError.java | 8 +-- src/controller/java/AndroidCallbacks.cpp | 56 ++++++++----------- .../java/AndroidClusterExceptions.cpp | 8 +-- .../java/AndroidClusterExceptions.h | 4 +- .../java/AndroidControllerExceptions.cpp | 43 -------------- .../java/AndroidControllerExceptions.h | 45 --------------- src/controller/java/BUILD.gn | 2 - .../ChipDeviceControllerException.java | 4 +- .../ResubscriptionAttemptCallback.java | 2 +- .../GetConnectedDeviceCallbackJniTest.java | 4 +- 11 files changed, 40 insertions(+), 138 deletions(-) delete mode 100644 src/controller/java/AndroidControllerExceptions.cpp delete mode 100644 src/controller/java/AndroidControllerExceptions.h diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt index b2faca8cf92b63..c4b870da4369c9 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt @@ -57,7 +57,7 @@ class PairOnNetworkLongImSubscribeCommand( } private inner class InternalResubscriptionAttemptCallback : ResubscriptionAttemptCallback { - override fun onResubscriptionAttempt(terminationCause: Long, nextResubscribeIntervalMsec: Long) { + override fun onResubscriptionAttempt(terminationCause: Int, nextResubscribeIntervalMsec: Int) { logger.log(Level.INFO, "ResubscriptionAttemptCallback"); } } diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/MatterError.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/MatterError.java index ac12eaccd28977..e669a7b61bc56b 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/MatterError.java +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/MatterError.java @@ -20,15 +20,15 @@ import java.util.Objects; public class MatterError { - private long errorCode; + private int errorCode; private String errorMessage; public static final MatterError DISCOVERY_SERVICE_LOST = - new MatterError(4L, "Discovery service was lost."); + new MatterError(4, "Discovery service was lost."); public static final MatterError NO_ERROR = new MatterError(0, null); - public MatterError(long errorCode, String errorMessage) { + public MatterError(int errorCode, String errorMessage) { this.errorCode = errorCode; this.errorMessage = errorMessage; } @@ -37,7 +37,7 @@ public boolean isNoError() { return this.equals(NO_ERROR); } - public long getErrorCode() { + public int getErrorCode() { return errorCode; } diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index a84bd92f65fdea..d004de149235cb 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -16,7 +16,6 @@ */ #include "AndroidCallbacks.h" #include -#include #include #include #include @@ -106,14 +105,16 @@ void GetConnectedDeviceCallback::OnDeviceConnectionFailureFn(void * context, con JniReferences::GetInstance().FindMethod(env, javaCallback, "onConnectionFailure", "(JLjava/lang/Exception;)V", &failureMethod); VerifyOrReturn(failureMethod != nullptr, ChipLogError(Controller, "Could not find onConnectionFailure method")); - jthrowable exception; - CHIP_ERROR err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, ErrorStr(error), - error.AsInteger(), exception); - VerifyOrReturn( - err == CHIP_NO_ERROR, - ChipLogError(Controller, - "Unable to create AndroidControllerException on GetConnectedDeviceCallback::OnDeviceConnectionFailureFn: %s", - ErrorStr(err))); + // Create the exception to return. + jclass controllerExceptionCls; + CHIP_ERROR err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ChipDeviceControllerException", + controllerExceptionCls); + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find exception type for onConnectionFailure")); + JniClass controllerExceptionJniCls(controllerExceptionCls); + + jmethodID exceptionConstructor = env->GetMethodID(controllerExceptionCls, "", "(ILjava/lang/String;)V"); + jobject exception = + env->NewObject(controllerExceptionCls, exceptionConstructor, error.AsInteger(), env->NewStringUTF(ErrorStr(error))); DeviceLayer::StackUnlock unlock; env->CallVoidMethod(javaCallback, failureMethod, peerId.GetNodeId(), exception); @@ -557,12 +558,12 @@ CHIP_ERROR ReportCallback::OnResubscriptionNeeded(app::ReadClient * apReadClient jmethodID onResubscriptionAttemptMethod; ReturnLogErrorOnFailure(JniReferences::GetInstance().FindMethod( - env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(JJ)V", &onResubscriptionAttemptMethod)); + env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod)); DeviceLayer::StackUnlock unlock; env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod, - static_cast(aTerminationCause.AsInteger()), - static_cast(apReadClient->ComputeTimeTillNextSubscription())); + static_cast(aTerminationCause.AsInteger()), + static_cast(apReadClient->ComputeTimeTillNextSubscription())); VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN); return CHIP_NO_ERROR; } @@ -584,10 +585,8 @@ void ReportCallback::ReportError(jobject attributePath, jobject eventPath, const JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); jthrowable exception; - err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception); - VerifyOrReturn( - err == CHIP_NO_ERROR, - ChipLogError(Controller, "Unable to create AndroidControllerException on ReportCallback::ReportError: %s", ErrorStr(err))); + err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception); + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err))); jmethodID onErrorMethod; err = JniReferences::GetInstance().FindMethod( @@ -814,12 +813,12 @@ CHIP_ERROR ReportEventCallback::OnResubscriptionNeeded(app::ReadClient * apReadC jmethodID onResubscriptionAttemptMethod; ReturnLogErrorOnFailure(JniReferences::GetInstance().FindMethod( - env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(JJ)V", &onResubscriptionAttemptMethod)); + env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod)); DeviceLayer::StackUnlock unlock; env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod, - static_cast(aTerminationCause.AsInteger()), - static_cast(apReadClient->ComputeTimeTillNextSubscription())); + static_cast(aTerminationCause.AsInteger()), + static_cast(apReadClient->ComputeTimeTillNextSubscription())); return CHIP_NO_ERROR; } @@ -840,10 +839,8 @@ void ReportEventCallback::ReportError(jobject eventPath, const char * message, C JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); jthrowable exception; - err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception); - VerifyOrReturn(err == CHIP_NO_ERROR, - ChipLogError(Controller, "Unable to create AndroidControllerException: %s on eportEventCallback::ReportError", - ErrorStr(err))); + err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception); + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err))); jmethodID onErrorMethod; err = JniReferences::GetInstance().FindMethod( @@ -946,11 +943,8 @@ void WriteAttributesCallback::ReportError(jobject attributePath, const char * me ChipLogError(Controller, "WriteAttributesCallback ReportError is called"); jthrowable exception; - err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception); - VerifyOrReturn(err == CHIP_NO_ERROR, - ChipLogError(Controller, - "Unable to create AndroidControllerException on WriteAttributesCallback::ReportError: %s", - ErrorStr(err))); + err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception); + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err))); jmethodID onErrorMethod; err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onError", @@ -1049,10 +1043,8 @@ void InvokeCallback::ReportError(const char * message, ChipError::StorageType er ChipLogError(Controller, "InvokeCallback ReportError is called"); jthrowable exception; - err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception); - VerifyOrReturn( - err == CHIP_NO_ERROR, - ChipLogError(Controller, "Unable to create AndroidControllerException: %s on InvokeCallback::ReportError", ErrorStr(err))); + err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception); + VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err))); jmethodID onErrorMethod; err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onError", "(Ljava/lang/Exception;)V", &onErrorMethod); diff --git a/src/controller/java/AndroidClusterExceptions.cpp b/src/controller/java/AndroidClusterExceptions.cpp index 4a757588b94414..a858a4b50fe948 100644 --- a/src/controller/java/AndroidClusterExceptions.cpp +++ b/src/controller/java/AndroidClusterExceptions.cpp @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021-2023 Project CHIP Authors + * Copyright (c) 2021 Project CHIP Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,7 @@ namespace chip { -CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx) +CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx) { CHIP_ERROR err = CHIP_NO_ERROR; jmethodID exceptionConstructor; @@ -34,10 +34,10 @@ CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, ui VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND); chip::JniClass clusterExceptionJniCls(clusterExceptionCls); - exceptionConstructor = env->GetMethodID(clusterExceptionCls, "", "(J)V"); + exceptionConstructor = env->GetMethodID(clusterExceptionCls, "", "(I)V"); VerifyOrReturnError(exceptionConstructor != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND); - outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, static_cast(errorCode)); + outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, errorCode); VerifyOrReturnError(outEx != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND); return err; diff --git a/src/controller/java/AndroidClusterExceptions.h b/src/controller/java/AndroidClusterExceptions.h index e0bdd07bab51e1..51a3638d3b39d8 100644 --- a/src/controller/java/AndroidClusterExceptions.h +++ b/src/controller/java/AndroidClusterExceptions.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021-2023 Project CHIP Authors + * Copyright (c) 2021 Project CHIP Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +37,7 @@ class AndroidClusterExceptions /** * Creates a Java ChipClusterException object in outEx. */ - CHIP_ERROR CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx); + CHIP_ERROR CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx); /** * Creates a Java IllegalStateException in outEx. diff --git a/src/controller/java/AndroidControllerExceptions.cpp b/src/controller/java/AndroidControllerExceptions.cpp deleted file mode 100644 index 0419ad2a9bf0ee..00000000000000 --- a/src/controller/java/AndroidControllerExceptions.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * Copyright (c) 2023 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "AndroidControllerExceptions.h" - -#include -#include -#include -#include - -namespace chip { - -CHIP_ERROR AndroidControllerExceptions::CreateAndroidControllerException(JNIEnv * env, const char * message, uint32_t errorCode, - jthrowable & outEx) -{ - jclass controllerExceptionCls; - CHIP_ERROR err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ChipDeviceControllerException", - controllerExceptionCls); - VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND); - JniClass controllerExceptionJniCls(controllerExceptionCls); - - jmethodID exceptionConstructor = env->GetMethodID(controllerExceptionCls, "", "(Jjava/lang/String;)V"); - outEx = (jthrowable) env->NewObject(controllerExceptionCls, exceptionConstructor, static_cast(errorCode), - env->NewStringUTF(message)); - VerifyOrReturnError(outEx != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND); - return CHIP_NO_ERROR; -} - -} // namespace chip diff --git a/src/controller/java/AndroidControllerExceptions.h b/src/controller/java/AndroidControllerExceptions.h deleted file mode 100644 index 02ed39d4bbb447..00000000000000 --- a/src/controller/java/AndroidControllerExceptions.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * Copyright (c) 2023 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include - -namespace chip { -class AndroidControllerExceptions -{ -public: - AndroidControllerExceptions(const AndroidControllerExceptions &) = delete; - AndroidControllerExceptions(const AndroidControllerExceptions &&) = delete; - AndroidControllerExceptions & operator=(const AndroidControllerExceptions &) = delete; - - static AndroidControllerExceptions & GetInstance() - { - static AndroidControllerExceptions androidControllerExceptions; - return androidControllerExceptions; - } - - /** - * Creates a Java AndroidControllerException object in outEx. - */ - CHIP_ERROR CreateAndroidControllerException(JNIEnv * env, const char * message, uint32_t errorCode, jthrowable & outEx); - -private: - AndroidControllerExceptions() {} -}; -} // namespace chip diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index ece7ed06d2e0dd..3d982bfa925e7d 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -41,8 +41,6 @@ shared_library("jni") { "AndroidClusterExceptions.h", "AndroidCommissioningWindowOpener.cpp", "AndroidCommissioningWindowOpener.h", - "AndroidControllerExceptions.cpp", - "AndroidControllerExceptions.h", "AndroidCurrentFabricRemover.cpp", "AndroidCurrentFabricRemover.h", "AndroidDeviceControllerWrapper.cpp", diff --git a/src/controller/java/src/chip/devicecontroller/ChipDeviceControllerException.java b/src/controller/java/src/chip/devicecontroller/ChipDeviceControllerException.java index ec7cfb7b3318ea..2d2d583e146665 100644 --- a/src/controller/java/src/chip/devicecontroller/ChipDeviceControllerException.java +++ b/src/controller/java/src/chip/devicecontroller/ChipDeviceControllerException.java @@ -21,11 +21,11 @@ public class ChipDeviceControllerException extends RuntimeException { private static final long serialVersionUID = 1L; - public long errorCode; + public int errorCode; public ChipDeviceControllerException() {} - public ChipDeviceControllerException(long errorCode, String message) { + public ChipDeviceControllerException(int errorCode, String message) { super(message != null ? message : String.format("Error Code %d", errorCode)); this.errorCode = errorCode; } diff --git a/src/controller/java/src/chip/devicecontroller/ResubscriptionAttemptCallback.java b/src/controller/java/src/chip/devicecontroller/ResubscriptionAttemptCallback.java index 01c6eaab8d5df3..cf029ffa54b593 100644 --- a/src/controller/java/src/chip/devicecontroller/ResubscriptionAttemptCallback.java +++ b/src/controller/java/src/chip/devicecontroller/ResubscriptionAttemptCallback.java @@ -18,5 +18,5 @@ package chip.devicecontroller; public interface ResubscriptionAttemptCallback { - void onResubscriptionAttempt(long terminationCause, long nextResubscribeIntervalMsec); + void onResubscriptionAttempt(int terminationCause, int nextResubscribeIntervalMsec); } diff --git a/src/controller/java/tests/chip/devicecontroller/GetConnectedDeviceCallbackJniTest.java b/src/controller/java/tests/chip/devicecontroller/GetConnectedDeviceCallbackJniTest.java index 56494b0efc20b7..f7f981483ba27c 100644 --- a/src/controller/java/tests/chip/devicecontroller/GetConnectedDeviceCallbackJniTest.java +++ b/src/controller/java/tests/chip/devicecontroller/GetConnectedDeviceCallbackJniTest.java @@ -48,10 +48,10 @@ public void deviceConnected() { public void connectionFailure() { var callback = new FakeGetConnectedDeviceCallback(); var jniCallback = new GetConnectedDeviceCallbackJni(callback); - callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100L); + callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100); assertThat(callback.error).isInstanceOf(ChipDeviceControllerException.class); - assertThat(((ChipDeviceControllerException) callback.error).errorCode).isEqualTo(100L); + assertThat(((ChipDeviceControllerException) callback.error).errorCode).isEqualTo(100); } class FakeGetConnectedDeviceCallback implements GetConnectedDeviceCallback {