Skip to content

Commit bf95967

Browse files
Revert "Use controller exception in Java controller (#26708)" (#26799)
This reverts commit e022057.
1 parent 86267e6 commit bf95967

File tree

11 files changed

+40
-138
lines changed

11 files changed

+40
-138
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: Long, nextResubscribeIntervalMsec: Long) {
60+
override fun onResubscriptionAttempt(terminationCause: Int, nextResubscribeIntervalMsec: Int) {
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 long errorCode;
23+
private int errorCode;
2424
private String errorMessage;
2525

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

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

31-
public MatterError(long errorCode, String errorMessage) {
31+
public MatterError(int 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 long getErrorCode() {
40+
public int getErrorCode() {
4141
return errorCode;
4242
}
4343

src/controller/java/AndroidCallbacks.cpp

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

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)));
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)));
117118

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

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

562563
DeviceLayer::StackUnlock unlock;
563564
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod,
564-
static_cast<jlong>(aTerminationCause.AsInteger()),
565-
static_cast<jlong>(apReadClient->ComputeTimeTillNextSubscription()));
565+
static_cast<jint>(aTerminationCause.AsInteger()),
566+
static_cast<jint>(apReadClient->ComputeTimeTillNextSubscription()));
566567
VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN);
567568
return CHIP_NO_ERROR;
568569
}
@@ -584,10 +585,8 @@ void ReportCallback::ReportError(jobject attributePath, jobject eventPath, const
584585
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
585586

586587
jthrowable exception;
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)));
588+
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
589+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
591590

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

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

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

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

842841
jthrowable exception;
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)));
842+
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
843+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
847844

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

947944
ChipLogError(Controller, "WriteAttributesCallback ReportError is called");
948945
jthrowable exception;
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)));
946+
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
947+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
954948

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

10501044
ChipLogError(Controller, "InvokeCallback ReportError is called");
10511045
jthrowable exception;
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)));
1046+
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
1047+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
10561048

10571049
jmethodID onErrorMethod;
10581050
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-2023 Project CHIP Authors
3+
* Copyright (c) 2021 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, uint32_t errorCode, jthrowable & outEx)
27+
CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx)
2828
{
2929
CHIP_ERROR err = CHIP_NO_ERROR;
3030
jmethodID exceptionConstructor;
@@ -34,10 +34,10 @@ CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, ui
3434
VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
3535
chip::JniClass clusterExceptionJniCls(clusterExceptionCls);
3636

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

40-
outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, static_cast<jlong>(errorCode));
40+
outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, 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-2023 Project CHIP Authors
3+
* Copyright (c) 2021 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, uint32_t errorCode, jthrowable & outEx);
40+
CHIP_ERROR CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx);
4141

4242
/**
4343
* Creates a Java IllegalStateException in outEx.

src/controller/java/AndroidControllerExceptions.cpp

-43
This file was deleted.

src/controller/java/AndroidControllerExceptions.h

-45
This file was deleted.

src/controller/java/BUILD.gn

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

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 long errorCode;
24+
public int errorCode;
2525

2626
public ChipDeviceControllerException() {}
2727

28-
public ChipDeviceControllerException(long errorCode, String message) {
28+
public ChipDeviceControllerException(int 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(long terminationCause, long nextResubscribeIntervalMsec);
21+
void onResubscriptionAttempt(int terminationCause, int 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, 100L);
51+
callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100);
5252

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

5757
class FakeGetConnectedDeviceCallback implements GetConnectedDeviceCallback {

0 commit comments

Comments
 (0)