Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Delete finalize method in java #37417

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import chip.devicecontroller.model.NodeState;
import chip.devicecontroller.model.Status;

import javax.annotation.Nullable;
import java.lang.ref.Cleaner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -169,10 +170,23 @@ public class ChipClusters {

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

private final Cleaner.Cleanable cleanable;

public BaseChipCluster(long devicePtr, int endpointId, long clusterId) {
this.devicePtr = devicePtr;
this.endpointId = endpointId;
this.clusterId = clusterId;

this.cleanable =
Cleaner.create()
.register(
this,
() -> {
if (chipClusterPtr != 0) {
deleteCluster(chipClusterPtr);
chipClusterPtr = 0;
}
});
}

/**
Expand Down Expand Up @@ -241,15 +255,6 @@ public class ChipClusters {

@Deprecated
public void deleteCluster(long chipClusterPtr) {}
@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

if (chipClusterPtr != 0) {
deleteCluster(chipClusterPtr);
chipClusterPtr = 0;
}
}
}

abstract static class ReportCallbackImpl implements ReportCallback, SubscriptionEstablishedCallback, ResubscriptionAttemptCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import chip.devicecontroller.model.Status;

import javax.annotation.Nullable;
import java.lang.ref.Cleaner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -92,10 +93,23 @@ public static abstract class BaseChipCluster {

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

private final Cleaner.Cleanable cleanable;

public BaseChipCluster(long devicePtr, int endpointId, long clusterId) {
this.devicePtr = devicePtr;
this.endpointId = endpointId;
this.clusterId = clusterId;

this.cleanable =
Cleaner.create()
.register(
this,
() -> {
if (chipClusterPtr != 0) {
deleteCluster(chipClusterPtr);
chipClusterPtr = 0;
}
});
}

/**
Expand Down Expand Up @@ -164,15 +178,6 @@ protected void invoke(

@Deprecated
public void deleteCluster(long chipClusterPtr) {}
@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

if (chipClusterPtr != 0) {
deleteCluster(chipClusterPtr);
chipClusterPtr = 0;
}
}
}

abstract static class ReportCallbackImpl implements ReportCallback, SubscriptionEstablishedCallback, ResubscriptionAttemptCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import chip.devicecontroller.model.Status;

import javax.annotation.Nullable;
import java.lang.ref.Cleaner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -92,10 +93,23 @@ public static abstract class BaseChipCluster {

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

private final Cleaner.Cleanable cleanable;

public BaseChipCluster(long devicePtr, int endpointId, long clusterId) {
this.devicePtr = devicePtr;
this.endpointId = endpointId;
this.clusterId = clusterId;

this.cleanable =
Cleaner.create()
.register(
this,
() -> {
if (chipClusterPtr != 0) {
deleteCluster(chipClusterPtr);
chipClusterPtr = 0;
}
});
}

/**
Expand Down Expand Up @@ -164,15 +178,6 @@ protected void invoke(

@Deprecated
public void deleteCluster(long chipClusterPtr) {}
@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

if (chipClusterPtr != 0) {
deleteCluster(chipClusterPtr);
chipClusterPtr = 0;
}
}
}

abstract static class ReportCallbackImpl implements ReportCallback, SubscriptionEstablishedCallback, ResubscriptionAttemptCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,31 @@

import chip.devicecontroller.model.InvokeResponseData;
import chip.devicecontroller.model.NoInvokeResponseData;
import java.lang.ref.Cleaner;
import java.util.Optional;
import javax.annotation.Nullable;

/** JNI wrapper callback class for {@link InvokeCallback}. */
public final class ExtendableInvokeCallbackJni {
private final ExtendableInvokeCallback wrappedExtendableInvokeCallback;
public final class BatchInvokeCallbackJni {
private final BatchInvokeCallbackJni wrappedBatchInvokeCallback;
private long callbackHandle;

public ExtendableInvokeCallbackJni(ExtendableInvokeCallback wrappedExtendableInvokeCallback) {
this.wrappedExtendableInvokeCallback = wrappedExtendableInvokeCallback;
private final Cleaner.Cleanable cleanable;

public BatchInvokeCallbackJni(BatchInvokeCallback wrappedBatchInvokeCallback) {
this.wrappedBatchInvokeCallback = wrappedBatchInvokeCallback;
this.callbackHandle = newCallback();

this.cleanable =
Cleaner.create()
.register(
this,
() -> {
if (chipClusterPtr != 0) {
deleteCluster(chipClusterPtr);
chipClusterPtr = 0;
}
});
}

long getCallbackHandle() {
Expand All @@ -41,7 +55,7 @@ long getCallbackHandle() {
private native void deleteCallback(long callbackHandle);

private void onError(Exception e) {
wrappedExtendableInvokeCallback.onError(e);
wrappedBatchInvokeCallback.onError(e);
}

private void onResponse(
Expand Down Expand Up @@ -74,21 +88,10 @@ private void onResponse(
}

private void onNoResponse(int commandRef) {
wrappedExtendableInvokeCallback.onNoResponse(NoInvokeResponseData.newInstance(commandRef));
wrappedBatchInvokeCallback.onNoResponse(NoInvokeResponseData.newInstance(commandRef));
}

private void onDone() {
wrappedExtendableInvokeCallback.onDone();
}

// TODO(#8578): Replace finalizer with PhantomReference.
@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

if (callbackHandle != 0) {
deleteCallback(callbackHandle);
callbackHandle = 0;
}
wrappedBatchInvokeCallback.onDone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import chip.devicecontroller.model.ChipEventPath;
import chip.devicecontroller.model.DataVersionFilter;
import chip.devicecontroller.model.InvokeElement;
import java.lang.ref.Cleaner;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -44,6 +45,8 @@ public class ChipDeviceController {
private ScanNetworksListener scanNetworksListener;
private NOCChainIssuer nocChainIssuer;

private final Cleaner.Cleanable cleanable;

/**
* To load class and jni, we need to new AndroidChipPlatform after jni load but before new
* ChipDeviceController
Expand All @@ -67,6 +70,17 @@ public ChipDeviceController(ControllerParams params) {
throw new NullPointerException("params cannot be null");
}
deviceControllerPtr = newDeviceController(params);

this.cleanable =
Cleaner.create()
.register(
this,
() -> {
if (deviceControllerPtr != 0) {
deleteDeviceController(deviceControllerPtr);
deviceControllerPtr = 0;
}
});
}

public void setCompletionListener(CompletionListener listener) {
Expand Down Expand Up @@ -1773,16 +1787,6 @@ private native void updateCommissioningICDRegistrationInfo(
System.loadLibrary("CHIPController");
}

@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

if (deviceControllerPtr != 0) {
deleteDeviceController(deviceControllerPtr);
deviceControllerPtr = 0;
}
}

/** Interface to implement custom operational credentials issuer (NOC chain generation). */
public interface NOCChainIssuer {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import chip.devicecontroller.model.InvokeResponseData;
import chip.devicecontroller.model.NoInvokeResponseData;
import java.lang.ref.Cleaner;
import java.util.Optional;
import javax.annotation.Nullable;

Expand All @@ -27,9 +28,22 @@ public final class ExtendableInvokeCallbackJni {
private final ExtendableInvokeCallback wrappedExtendableInvokeCallback;
private long callbackHandle;

private final Cleaner.Cleanable cleanable;

public ExtendableInvokeCallbackJni(ExtendableInvokeCallback wrappedExtendableInvokeCallback) {
this.wrappedExtendableInvokeCallback = wrappedExtendableInvokeCallback;
this.callbackHandle = newCallback();

this.cleanable =
Cleaner.create()
.register(
this,
() -> {
if (callbackHandle != 0) {
deleteCallback(callbackHandle);
callbackHandle = 0;
}
});
}

long getCallbackHandle() {
Expand Down Expand Up @@ -80,15 +94,4 @@ private void onNoResponse(int commandRef) {
private void onDone() {
wrappedExtendableInvokeCallback.onDone();
}

// TODO(#8578): Replace finalizer with PhantomReference.
@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

if (callbackHandle != 0) {
deleteCallback(callbackHandle);
callbackHandle = 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@
*/
package chip.devicecontroller;

import java.lang.ref.Cleaner;

/** JNI wrapper callback class for getting a connected device. */
public class GetConnectedDeviceCallbackJni {
private final GetConnectedDeviceCallback wrappedCallback;
private long callbackHandle;

private final Cleaner.Cleanable cleanable;

public GetConnectedDeviceCallbackJni(GetConnectedDeviceCallback wrappedCallback) {
this.wrappedCallback = wrappedCallback;
this.callbackHandle = newCallback(wrappedCallback);

this.cleanable =
Cleaner.create()
.register(
this,
() -> {
if (callbackHandle != 0) {
deleteCallback(callbackHandle);
callbackHandle = 0;
}
});
}

long getCallbackHandle() {
Expand All @@ -35,17 +50,6 @@ long getCallbackHandle() {

private native void deleteCallback(long callbackHandle);

// TODO(#8578): Replace finalizer with PhantomReference.
@SuppressWarnings("deprecation")
protected void finalize() throws Throwable {
super.finalize();

if (callbackHandle != 0) {
deleteCallback(callbackHandle);
callbackHandle = 0;
}
}

/** Callbacks for getting a device connected with PASE or CASE, depending on the context. */
public interface GetConnectedDeviceCallback {
void onDeviceConnected(long devicePointer);
Expand Down
Loading
Loading