Skip to content

Commit 558625b

Browse files
authored
[jnigen] Support use arenas on nullable JObjects (#2239)
1 parent 625ed30 commit 558625b

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

pkgs/jni/lib/src/jobject.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ class JObject {
219219
void releasedBy(Arena arena) => arena.onReleaseAll(release);
220220
}
221221

222-
extension JObjectUseExtension<T extends JObject> on T {
222+
extension JObjectUseExtension<T extends JObject?> on T {
223223
/// Applies [callback] on this object and then delete the underlying JNI
224224
/// reference, returning the result of [callback].
225225
R use<R>(R Function(T) callback) {
226226
try {
227227
final result = callback(this);
228228
return result;
229229
} finally {
230-
release();
230+
this?.release();
231231
}
232232
}
233233
}

pkgs/jni/test/jobject_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ void run({required TestRunnerCallback testRunner}) {
175175
});
176176
});
177177
expect(randomInt, lessThan(15));
178+
const JObject? nullableJObject = null;
179+
expect(nullableJObject.use((_) => 'foo'), equals('foo'));
178180
});
179181

180182
// The JObject and JClass have NativeFinalizer. However, it's possible to

0 commit comments

Comments
 (0)