Skip to content

Commit 1076388

Browse files
yunhanw-googlepull[bot]
authored andcommitted
Reland Use controller exception in Java controller (#26802)
* Revert "Revert "Use controller exception in Java controller (#26708)" (#26799)" This reverts commit bf95967. * fix the type inside cluster exception * Add missing L fully-qualified-class for controller exception
1 parent 0415805 commit 1076388

File tree

12 files changed

+140
-42
lines changed

12 files changed

+140
-42
lines changed

examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class PairOnNetworkLongImSubscribeCommand(
5757
}
5858

5959
private inner class InternalResubscriptionAttemptCallback : ResubscriptionAttemptCallback {
60-
override fun onResubscriptionAttempt(terminationCause: Int, nextResubscribeIntervalMsec: Int) {
60+
override fun onResubscriptionAttempt(terminationCause: Long, nextResubscribeIntervalMsec: Long) {
6161
logger.log(Level.INFO, "ResubscriptionAttemptCallback");
6262
}
6363
}

examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/MatterError.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
import java.util.Objects;
2121

2222
public class MatterError {
23-
private int errorCode;
23+
private long errorCode;
2424
private String errorMessage;
2525

2626
public static final MatterError DISCOVERY_SERVICE_LOST =
27-
new MatterError(4, "Discovery service was lost.");
27+
new MatterError(4L, "Discovery service was lost.");
2828

2929
public static final MatterError NO_ERROR = new MatterError(0, null);
3030

31-
public MatterError(int errorCode, String errorMessage) {
31+
public MatterError(long errorCode, String errorMessage) {
3232
this.errorCode = errorCode;
3333
this.errorMessage = errorMessage;
3434
}
@@ -37,7 +37,7 @@ public boolean isNoError() {
3737
return this.equals(NO_ERROR);
3838
}
3939

40-
public int getErrorCode() {
40+
public long getErrorCode() {
4141
return errorCode;
4242
}
4343

src/controller/java/AndroidCallbacks.cpp

+32-24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
#include "AndroidCallbacks.h"
1818
#include <controller/java/AndroidClusterExceptions.h>
19+
#include <controller/java/AndroidControllerExceptions.h>
1920
#include <controller/java/CHIPAttributeTLVValueDecoder.h>
2021
#include <controller/java/CHIPEventTLVValueDecoder.h>
2122
#include <jni.h>
@@ -105,16 +106,14 @@ void GetConnectedDeviceCallback::OnDeviceConnectionFailureFn(void * context, con
105106
JniReferences::GetInstance().FindMethod(env, javaCallback, "onConnectionFailure", "(JLjava/lang/Exception;)V", &failureMethod);
106107
VerifyOrReturn(failureMethod != nullptr, ChipLogError(Controller, "Could not find onConnectionFailure method"));
107108

108-
// Create the exception to return.
109-
jclass controllerExceptionCls;
110-
CHIP_ERROR err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ChipDeviceControllerException",
111-
controllerExceptionCls);
112-
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find exception type for onConnectionFailure"));
113-
JniClass controllerExceptionJniCls(controllerExceptionCls);
114-
115-
jmethodID exceptionConstructor = env->GetMethodID(controllerExceptionCls, "<init>", "(ILjava/lang/String;)V");
116-
jobject exception =
117-
env->NewObject(controllerExceptionCls, exceptionConstructor, error.AsInteger(), env->NewStringUTF(ErrorStr(error)));
109+
jthrowable exception;
110+
CHIP_ERROR err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, ErrorStr(error),
111+
error.AsInteger(), exception);
112+
VerifyOrReturn(
113+
err == CHIP_NO_ERROR,
114+
ChipLogError(Controller,
115+
"Unable to create AndroidControllerException on GetConnectedDeviceCallback::OnDeviceConnectionFailureFn: %s",
116+
ErrorStr(err)));
118117

119118
DeviceLayer::StackUnlock unlock;
120119
env->CallVoidMethod(javaCallback, failureMethod, peerId.GetNodeId(), exception);
@@ -558,12 +557,12 @@ CHIP_ERROR ReportCallback::OnResubscriptionNeeded(app::ReadClient * apReadClient
558557

559558
jmethodID onResubscriptionAttemptMethod;
560559
ReturnLogErrorOnFailure(JniReferences::GetInstance().FindMethod(
561-
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod));
560+
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(JJ)V", &onResubscriptionAttemptMethod));
562561

563562
DeviceLayer::StackUnlock unlock;
564563
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod,
565-
static_cast<jint>(aTerminationCause.AsInteger()),
566-
static_cast<jint>(apReadClient->ComputeTimeTillNextSubscription()));
564+
static_cast<jlong>(aTerminationCause.AsInteger()),
565+
static_cast<jlong>(apReadClient->ComputeTimeTillNextSubscription()));
567566
VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN);
568567
return CHIP_NO_ERROR;
569568
}
@@ -585,8 +584,10 @@ void ReportCallback::ReportError(jobject attributePath, jobject eventPath, const
585584
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
586585

587586
jthrowable exception;
588-
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
589-
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
587+
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
588+
VerifyOrReturn(
589+
err == CHIP_NO_ERROR,
590+
ChipLogError(Controller, "Unable to create AndroidControllerException on ReportCallback::ReportError: %s", ErrorStr(err)));
590591

591592
jmethodID onErrorMethod;
592593
err = JniReferences::GetInstance().FindMethod(
@@ -813,12 +814,12 @@ CHIP_ERROR ReportEventCallback::OnResubscriptionNeeded(app::ReadClient * apReadC
813814

814815
jmethodID onResubscriptionAttemptMethod;
815816
ReturnLogErrorOnFailure(JniReferences::GetInstance().FindMethod(
816-
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod));
817+
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(JJ)V", &onResubscriptionAttemptMethod));
817818

818819
DeviceLayer::StackUnlock unlock;
819820
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod,
820-
static_cast<jint>(aTerminationCause.AsInteger()),
821-
static_cast<jint>(apReadClient->ComputeTimeTillNextSubscription()));
821+
static_cast<jlong>(aTerminationCause.AsInteger()),
822+
static_cast<jlong>(apReadClient->ComputeTimeTillNextSubscription()));
822823

823824
return CHIP_NO_ERROR;
824825
}
@@ -839,8 +840,10 @@ void ReportEventCallback::ReportError(jobject eventPath, const char * message, C
839840
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
840841

841842
jthrowable exception;
842-
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
843-
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
843+
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
844+
VerifyOrReturn(err == CHIP_NO_ERROR,
845+
ChipLogError(Controller, "Unable to create AndroidControllerException: %s on eportEventCallback::ReportError",
846+
ErrorStr(err)));
844847

845848
jmethodID onErrorMethod;
846849
err = JniReferences::GetInstance().FindMethod(
@@ -943,8 +946,11 @@ void WriteAttributesCallback::ReportError(jobject attributePath, const char * me
943946

944947
ChipLogError(Controller, "WriteAttributesCallback ReportError is called");
945948
jthrowable exception;
946-
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
947-
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
949+
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
950+
VerifyOrReturn(err == CHIP_NO_ERROR,
951+
ChipLogError(Controller,
952+
"Unable to create AndroidControllerException on WriteAttributesCallback::ReportError: %s",
953+
ErrorStr(err)));
948954

949955
jmethodID onErrorMethod;
950956
err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onError",
@@ -1043,8 +1049,10 @@ void InvokeCallback::ReportError(const char * message, ChipError::StorageType er
10431049

10441050
ChipLogError(Controller, "InvokeCallback ReportError is called");
10451051
jthrowable exception;
1046-
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
1047-
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
1052+
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
1053+
VerifyOrReturn(
1054+
err == CHIP_NO_ERROR,
1055+
ChipLogError(Controller, "Unable to create AndroidControllerException: %s on InvokeCallback::ReportError", ErrorStr(err)));
10481056

10491057
jmethodID onErrorMethod;
10501058
err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onError", "(Ljava/lang/Exception;)V", &onErrorMethod);

src/controller/java/AndroidClusterExceptions.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2021 Project CHIP Authors
3+
* Copyright (c) 2021-2023 Project CHIP Authors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424

2525
namespace chip {
2626

27-
CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx)
27+
CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx)
2828
{
2929
CHIP_ERROR err = CHIP_NO_ERROR;
3030
jmethodID exceptionConstructor;
@@ -34,10 +34,10 @@ CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, ji
3434
VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
3535
chip::JniClass clusterExceptionJniCls(clusterExceptionCls);
3636

37-
exceptionConstructor = env->GetMethodID(clusterExceptionCls, "<init>", "(I)V");
37+
exceptionConstructor = env->GetMethodID(clusterExceptionCls, "<init>", "(J)V");
3838
VerifyOrReturnError(exceptionConstructor != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
3939

40-
outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, errorCode);
40+
outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, static_cast<jlong>(errorCode));
4141
VerifyOrReturnError(outEx != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
4242

4343
return err;

src/controller/java/AndroidClusterExceptions.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (c) 2021 Project CHIP Authors
3+
* Copyright (c) 2021-2023 Project CHIP Authors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ class AndroidClusterExceptions
3737
/**
3838
* Creates a Java ChipClusterException object in outEx.
3939
*/
40-
CHIP_ERROR CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx);
40+
CHIP_ERROR CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx);
4141

4242
/**
4343
* Creates a Java IllegalStateException in outEx.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "AndroidControllerExceptions.h"
19+
20+
#include <lib/core/CHIPError.h>
21+
#include <lib/support/CHIPJNIError.h>
22+
#include <lib/support/JniReferences.h>
23+
#include <lib/support/JniTypeWrappers.h>
24+
25+
namespace chip {
26+
27+
CHIP_ERROR AndroidControllerExceptions::CreateAndroidControllerException(JNIEnv * env, const char * message, uint32_t errorCode,
28+
jthrowable & outEx)
29+
{
30+
jclass controllerExceptionCls;
31+
CHIP_ERROR err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ChipDeviceControllerException",
32+
controllerExceptionCls);
33+
VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
34+
JniClass controllerExceptionJniCls(controllerExceptionCls);
35+
36+
jmethodID exceptionConstructor = env->GetMethodID(controllerExceptionCls, "<init>", "(JLjava/lang/String;)V");
37+
outEx = (jthrowable) env->NewObject(controllerExceptionCls, exceptionConstructor, static_cast<jlong>(errorCode),
38+
env->NewStringUTF(message));
39+
VerifyOrReturnError(outEx != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
40+
return CHIP_NO_ERROR;
41+
}
42+
43+
} // namespace chip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include <jni.h>
21+
#include <lib/core/CHIPError.h>
22+
23+
namespace chip {
24+
class AndroidControllerExceptions
25+
{
26+
public:
27+
AndroidControllerExceptions(const AndroidControllerExceptions &) = delete;
28+
AndroidControllerExceptions(const AndroidControllerExceptions &&) = delete;
29+
AndroidControllerExceptions & operator=(const AndroidControllerExceptions &) = delete;
30+
31+
static AndroidControllerExceptions & GetInstance()
32+
{
33+
static AndroidControllerExceptions androidControllerExceptions;
34+
return androidControllerExceptions;
35+
}
36+
37+
/**
38+
* Creates a Java AndroidControllerException object in outEx.
39+
*/
40+
CHIP_ERROR CreateAndroidControllerException(JNIEnv * env, const char * message, uint32_t errorCode, jthrowable & outEx);
41+
42+
private:
43+
AndroidControllerExceptions() {}
44+
};
45+
} // namespace chip

src/controller/java/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ shared_library("jni") {
4141
"AndroidClusterExceptions.h",
4242
"AndroidCommissioningWindowOpener.cpp",
4343
"AndroidCommissioningWindowOpener.h",
44+
"AndroidControllerExceptions.cpp",
45+
"AndroidControllerExceptions.h",
4446
"AndroidCurrentFabricRemover.cpp",
4547
"AndroidCurrentFabricRemover.h",
4648
"AndroidDeviceControllerWrapper.cpp",

src/controller/java/src/chip/devicecontroller/ChipClusterException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
public class ChipClusterException extends Exception {
2222
private static final long serialVersionUID = 1L;
2323

24-
public int errorCode;
24+
public long errorCode;
2525

2626
public ChipClusterException() {}
2727

28-
public ChipClusterException(int errorCode) {
28+
public ChipClusterException(long errorCode) {
2929
super(String.format("CHIP cluster error: %d", errorCode));
3030
this.errorCode = errorCode;
3131
}

src/controller/java/src/chip/devicecontroller/ChipDeviceControllerException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
public class ChipDeviceControllerException extends RuntimeException {
2222
private static final long serialVersionUID = 1L;
2323

24-
public int errorCode;
24+
public long errorCode;
2525

2626
public ChipDeviceControllerException() {}
2727

28-
public ChipDeviceControllerException(int errorCode, String message) {
28+
public ChipDeviceControllerException(long errorCode, String message) {
2929
super(message != null ? message : String.format("Error Code %d", errorCode));
3030
this.errorCode = errorCode;
3131
}

src/controller/java/src/chip/devicecontroller/ResubscriptionAttemptCallback.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
package chip.devicecontroller;
1919

2020
public interface ResubscriptionAttemptCallback {
21-
void onResubscriptionAttempt(int terminationCause, int nextResubscribeIntervalMsec);
21+
void onResubscriptionAttempt(long terminationCause, long nextResubscribeIntervalMsec);
2222
}

src/controller/java/tests/chip/devicecontroller/GetConnectedDeviceCallbackJniTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public void deviceConnected() {
4848
public void connectionFailure() {
4949
var callback = new FakeGetConnectedDeviceCallback();
5050
var jniCallback = new GetConnectedDeviceCallbackJni(callback);
51-
callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100);
51+
callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100L);
5252

5353
assertThat(callback.error).isInstanceOf(ChipDeviceControllerException.class);
54-
assertThat(((ChipDeviceControllerException) callback.error).errorCode).isEqualTo(100);
54+
assertThat(((ChipDeviceControllerException) callback.error).errorCode).isEqualTo(100L);
5555
}
5656

5757
class FakeGetConnectedDeviceCallback implements GetConnectedDeviceCallback {

0 commit comments

Comments
 (0)