Skip to content

Commit 2a77a11

Browse files
Revert "[Java] Delete finalize method in java (#37417)" (#37446)
This reverts commit d777dce.
1 parent e1d26a0 commit 2a77a11

File tree

10 files changed

+110
-148
lines changed

10 files changed

+110
-148
lines changed

scripts/py_matter_idl/matter_idl/generators/java/ChipClusters_java.jinja

+9-14
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ import chip.devicecontroller.model.NodeState;
105105
import chip.devicecontroller.model.Status;
106106

107107
import javax.annotation.Nullable;
108-
import java.lang.ref.Cleaner;
109108
import java.util.ArrayList;
110109
import java.util.Arrays;
111110
import java.util.List;
@@ -170,23 +169,10 @@ public class ChipClusters {
170169

171170
private Optional<Long> timeoutMillis = Optional.empty();
172171

173-
private final Cleaner.Cleanable cleanable;
174-
175172
public BaseChipCluster(long devicePtr, int endpointId, long clusterId) {
176173
this.devicePtr = devicePtr;
177174
this.endpointId = endpointId;
178175
this.clusterId = clusterId;
179-
180-
this.cleanable =
181-
Cleaner.create()
182-
.register(
183-
this,
184-
() -> {
185-
if (chipClusterPtr != 0) {
186-
deleteCluster(chipClusterPtr);
187-
chipClusterPtr = 0;
188-
}
189-
});
190176
}
191177

192178
/**
@@ -255,6 +241,15 @@ public class ChipClusters {
255241

256242
@Deprecated
257243
public void deleteCluster(long chipClusterPtr) {}
244+
@SuppressWarnings("deprecation")
245+
protected void finalize() throws Throwable {
246+
super.finalize();
247+
248+
if (chipClusterPtr != 0) {
249+
deleteCluster(chipClusterPtr);
250+
chipClusterPtr = 0;
251+
}
252+
}
258253
}
259254

260255
abstract static class ReportCallbackImpl implements ReportCallback, SubscriptionEstablishedCallback, ResubscriptionAttemptCallback {

scripts/py_matter_idl/matter_idl/tests/outputs/several_clusters/java/ChipClusters.java

+9-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import chip.devicecontroller.model.Status;
2929

3030
import javax.annotation.Nullable;
31-
import java.lang.ref.Cleaner;
3231
import java.util.ArrayList;
3332
import java.util.Arrays;
3433
import java.util.List;
@@ -93,23 +92,10 @@ public static abstract class BaseChipCluster {
9392

9493
private Optional<Long> timeoutMillis = Optional.empty();
9594

96-
private final Cleaner.Cleanable cleanable;
97-
9895
public BaseChipCluster(long devicePtr, int endpointId, long clusterId) {
9996
this.devicePtr = devicePtr;
10097
this.endpointId = endpointId;
10198
this.clusterId = clusterId;
102-
103-
this.cleanable =
104-
Cleaner.create()
105-
.register(
106-
this,
107-
() -> {
108-
if (chipClusterPtr != 0) {
109-
deleteCluster(chipClusterPtr);
110-
chipClusterPtr = 0;
111-
}
112-
});
11399
}
114100

115101
/**
@@ -178,6 +164,15 @@ protected void invoke(
178164

179165
@Deprecated
180166
public void deleteCluster(long chipClusterPtr) {}
167+
@SuppressWarnings("deprecation")
168+
protected void finalize() throws Throwable {
169+
super.finalize();
170+
171+
if (chipClusterPtr != 0) {
172+
deleteCluster(chipClusterPtr);
173+
chipClusterPtr = 0;
174+
}
175+
}
181176
}
182177

183178
abstract static class ReportCallbackImpl implements ReportCallback, SubscriptionEstablishedCallback, ResubscriptionAttemptCallback {

src/controller/java/generated/java/chip/devicecontroller/ChipClusters.java

+9-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import chip.devicecontroller.model.Status;
2929

3030
import javax.annotation.Nullable;
31-
import java.lang.ref.Cleaner;
3231
import java.util.ArrayList;
3332
import java.util.Arrays;
3433
import java.util.List;
@@ -93,23 +92,10 @@ public static abstract class BaseChipCluster {
9392

9493
private Optional<Long> timeoutMillis = Optional.empty();
9594

96-
private final Cleaner.Cleanable cleanable;
97-
9895
public BaseChipCluster(long devicePtr, int endpointId, long clusterId) {
9996
this.devicePtr = devicePtr;
10097
this.endpointId = endpointId;
10198
this.clusterId = clusterId;
102-
103-
this.cleanable =
104-
Cleaner.create()
105-
.register(
106-
this,
107-
() -> {
108-
if (chipClusterPtr != 0) {
109-
deleteCluster(chipClusterPtr);
110-
chipClusterPtr = 0;
111-
}
112-
});
11399
}
114100

115101
/**
@@ -178,6 +164,15 @@ protected void invoke(
178164

179165
@Deprecated
180166
public void deleteCluster(long chipClusterPtr) {}
167+
@SuppressWarnings("deprecation")
168+
protected void finalize() throws Throwable {
169+
super.finalize();
170+
171+
if (chipClusterPtr != 0) {
172+
deleteCluster(chipClusterPtr);
173+
chipClusterPtr = 0;
174+
}
175+
}
181176
}
182177

183178
abstract static class ReportCallbackImpl implements ReportCallback, SubscriptionEstablishedCallback, ResubscriptionAttemptCallback {

src/controller/java/src/chip/devicecontroller/BatchInvokeCallbackJni.java

+18-21
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,17 @@
1919

2020
import chip.devicecontroller.model.InvokeResponseData;
2121
import chip.devicecontroller.model.NoInvokeResponseData;
22-
import java.lang.ref.Cleaner;
2322
import java.util.Optional;
2423
import javax.annotation.Nullable;
2524

2625
/** JNI wrapper callback class for {@link InvokeCallback}. */
27-
public final class BatchInvokeCallbackJni {
28-
private final BatchInvokeCallbackJni wrappedBatchInvokeCallback;
26+
public final class ExtendableInvokeCallbackJni {
27+
private final ExtendableInvokeCallback wrappedExtendableInvokeCallback;
2928
private long callbackHandle;
3029

31-
private final Cleaner.Cleanable cleanable;
32-
33-
public BatchInvokeCallbackJni(BatchInvokeCallback wrappedBatchInvokeCallback) {
34-
this.wrappedBatchInvokeCallback = wrappedBatchInvokeCallback;
30+
public ExtendableInvokeCallbackJni(ExtendableInvokeCallback wrappedExtendableInvokeCallback) {
31+
this.wrappedExtendableInvokeCallback = wrappedExtendableInvokeCallback;
3532
this.callbackHandle = newCallback();
36-
37-
this.cleanable =
38-
Cleaner.create()
39-
.register(
40-
this,
41-
() -> {
42-
if (chipClusterPtr != 0) {
43-
deleteCluster(chipClusterPtr);
44-
chipClusterPtr = 0;
45-
}
46-
});
4733
}
4834

4935
long getCallbackHandle() {
@@ -55,7 +41,7 @@ long getCallbackHandle() {
5541
private native void deleteCallback(long callbackHandle);
5642

5743
private void onError(Exception e) {
58-
wrappedBatchInvokeCallback.onError(e);
44+
wrappedExtendableInvokeCallback.onError(e);
5945
}
6046

6147
private void onResponse(
@@ -88,10 +74,21 @@ private void onResponse(
8874
}
8975

9076
private void onNoResponse(int commandRef) {
91-
wrappedBatchInvokeCallback.onNoResponse(NoInvokeResponseData.newInstance(commandRef));
77+
wrappedExtendableInvokeCallback.onNoResponse(NoInvokeResponseData.newInstance(commandRef));
9278
}
9379

9480
private void onDone() {
95-
wrappedBatchInvokeCallback.onDone();
81+
wrappedExtendableInvokeCallback.onDone();
82+
}
83+
84+
// TODO(#8578): Replace finalizer with PhantomReference.
85+
@SuppressWarnings("deprecation")
86+
protected void finalize() throws Throwable {
87+
super.finalize();
88+
89+
if (callbackHandle != 0) {
90+
deleteCallback(callbackHandle);
91+
callbackHandle = 0;
92+
}
9693
}
9794
}

src/controller/java/src/chip/devicecontroller/ChipDeviceController.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import chip.devicecontroller.model.ChipEventPath;
2727
import chip.devicecontroller.model.DataVersionFilter;
2828
import chip.devicecontroller.model.InvokeElement;
29-
import java.lang.ref.Cleaner;
3029
import java.time.ZoneOffset;
3130
import java.util.ArrayList;
3231
import java.util.Calendar;
@@ -45,8 +44,6 @@ public class ChipDeviceController {
4544
private ScanNetworksListener scanNetworksListener;
4645
private NOCChainIssuer nocChainIssuer;
4746

48-
private final Cleaner.Cleanable cleanable;
49-
5047
/**
5148
* To load class and jni, we need to new AndroidChipPlatform after jni load but before new
5249
* ChipDeviceController
@@ -70,17 +67,6 @@ public ChipDeviceController(ControllerParams params) {
7067
throw new NullPointerException("params cannot be null");
7168
}
7269
deviceControllerPtr = newDeviceController(params);
73-
74-
this.cleanable =
75-
Cleaner.create()
76-
.register(
77-
this,
78-
() -> {
79-
if (deviceControllerPtr != 0) {
80-
deleteDeviceController(deviceControllerPtr);
81-
deviceControllerPtr = 0;
82-
}
83-
});
8470
}
8571

8672
public void setCompletionListener(CompletionListener listener) {
@@ -1787,6 +1773,16 @@ private native void updateCommissioningICDRegistrationInfo(
17871773
System.loadLibrary("CHIPController");
17881774
}
17891775

1776+
@SuppressWarnings("deprecation")
1777+
protected void finalize() throws Throwable {
1778+
super.finalize();
1779+
1780+
if (deviceControllerPtr != 0) {
1781+
deleteDeviceController(deviceControllerPtr);
1782+
deviceControllerPtr = 0;
1783+
}
1784+
}
1785+
17901786
/** Interface to implement custom operational credentials issuer (NOC chain generation). */
17911787
public interface NOCChainIssuer {
17921788
/**

src/controller/java/src/chip/devicecontroller/ExtendableInvokeCallbackJni.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import chip.devicecontroller.model.InvokeResponseData;
2121
import chip.devicecontroller.model.NoInvokeResponseData;
22-
import java.lang.ref.Cleaner;
2322
import java.util.Optional;
2423
import javax.annotation.Nullable;
2524

@@ -28,22 +27,9 @@ public final class ExtendableInvokeCallbackJni {
2827
private final ExtendableInvokeCallback wrappedExtendableInvokeCallback;
2928
private long callbackHandle;
3029

31-
private final Cleaner.Cleanable cleanable;
32-
3330
public ExtendableInvokeCallbackJni(ExtendableInvokeCallback wrappedExtendableInvokeCallback) {
3431
this.wrappedExtendableInvokeCallback = wrappedExtendableInvokeCallback;
3532
this.callbackHandle = newCallback();
36-
37-
this.cleanable =
38-
Cleaner.create()
39-
.register(
40-
this,
41-
() -> {
42-
if (callbackHandle != 0) {
43-
deleteCallback(callbackHandle);
44-
callbackHandle = 0;
45-
}
46-
});
4733
}
4834

4935
long getCallbackHandle() {
@@ -94,4 +80,15 @@ private void onNoResponse(int commandRef) {
9480
private void onDone() {
9581
wrappedExtendableInvokeCallback.onDone();
9682
}
83+
84+
// TODO(#8578): Replace finalizer with PhantomReference.
85+
@SuppressWarnings("deprecation")
86+
protected void finalize() throws Throwable {
87+
super.finalize();
88+
89+
if (callbackHandle != 0) {
90+
deleteCallback(callbackHandle);
91+
callbackHandle = 0;
92+
}
93+
}
9794
}

src/controller/java/src/chip/devicecontroller/GetConnectedDeviceCallbackJni.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,14 @@
1717
*/
1818
package chip.devicecontroller;
1919

20-
import java.lang.ref.Cleaner;
21-
2220
/** JNI wrapper callback class for getting a connected device. */
2321
public class GetConnectedDeviceCallbackJni {
2422
private final GetConnectedDeviceCallback wrappedCallback;
2523
private long callbackHandle;
2624

27-
private final Cleaner.Cleanable cleanable;
28-
2925
public GetConnectedDeviceCallbackJni(GetConnectedDeviceCallback wrappedCallback) {
3026
this.wrappedCallback = wrappedCallback;
3127
this.callbackHandle = newCallback(wrappedCallback);
32-
33-
this.cleanable =
34-
Cleaner.create()
35-
.register(
36-
this,
37-
() -> {
38-
if (callbackHandle != 0) {
39-
deleteCallback(callbackHandle);
40-
callbackHandle = 0;
41-
}
42-
});
4328
}
4429

4530
long getCallbackHandle() {
@@ -50,6 +35,17 @@ long getCallbackHandle() {
5035

5136
private native void deleteCallback(long callbackHandle);
5237

38+
// TODO(#8578): Replace finalizer with PhantomReference.
39+
@SuppressWarnings("deprecation")
40+
protected void finalize() throws Throwable {
41+
super.finalize();
42+
43+
if (callbackHandle != 0) {
44+
deleteCallback(callbackHandle);
45+
callbackHandle = 0;
46+
}
47+
}
48+
5349
/** Callbacks for getting a device connected with PASE or CASE, depending on the context. */
5450
public interface GetConnectedDeviceCallback {
5551
void onDeviceConnected(long devicePointer);

0 commit comments

Comments
 (0)