From 1ca777f2732ed0bbfd546ba40ec94f57d8d1af89 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 9 Nov 2022 19:59:20 +0100 Subject: [PATCH] [jnigen] added .filled ctor to JArray (#130) Closed #129. --- pkgs/jni/lib/src/jarray.dart | 43 +++++++++++++++++++++++++--------- pkgs/jni/test/jarray_test.dart | 29 ++++++++++++++++++----- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/pkgs/jni/lib/src/jarray.dart b/pkgs/jni/lib/src/jarray.dart index 8ce6b30e6..416916719 100644 --- a/pkgs/jni/lib/src/jarray.dart +++ b/pkgs/jni/lib/src/jarray.dart @@ -22,17 +22,38 @@ class JArray extends JObject { /// Construct a new [JArray] with [reference] as its underlying reference. JArray.fromRef(JArrayPtr reference) : super.fromRef(reference); - JArray(JType typeClass, int length) - : super.fromRef( - (typeClass._type == JniCallType.objectType) - ? _accessors - .newObjectArray( - length, typeClass._getClass().reference, nullptr) - .checkedRef - : _accessors - .newPrimitiveArray(length, typeClass._type) - .checkedRef, - ); + /// Creates a [JArray] of the given length from the given [type]. + /// + /// The [length] must be a non-negative integer. + factory JArray(JType type, int length) { + if (type._type == JniCallType.objectType) { + final clazz = type._getClass(); + final array = JArray.fromRef( + _accessors.newObjectArray(length, clazz.reference, nullptr).checkedRef, + ); + clazz.delete(); + return array; + } + return JArray.fromRef( + _accessors.newPrimitiveArray(length, type._type).checkedRef, + ); + } + + /// Creates a [JArray] of the given length with [fill] at each position. + /// + /// The [length] must be a non-negative integer. + /// The [fill] must be a non-null [JObject]. + static JArray filled(int length, E fill) { + assert(!fill.isNull, "fill must not be null."); + final clazz = fill.getClass(); + final array = JArray.fromRef( + _accessors + .newObjectArray(length, clazz.reference, fill.reference) + .checkedRef, + ); + clazz.delete(); + return array; + } int? _length; diff --git a/pkgs/jni/test/jarray_test.dart b/pkgs/jni/test/jarray_test.dart index ad14c0f63..9a75e410b 100644 --- a/pkgs/jni/test/jarray_test.dart +++ b/pkgs/jni/test/jarray_test.dart @@ -199,9 +199,9 @@ void main() { array[0] = "حس".toJString()..deletedIn(arena); array[1] = "\$".toJString()..deletedIn(arena); array[2] = "33".toJString()..deletedIn(arena); - expect(array[0].toDartString(), "حس"); - expect(array[1].toDartString(), "\$"); - expect(array[2].toDartString(), "33"); + expect(array[0].toDartString(deleteOriginal: true), "حس"); + expect(array[1].toDartString(deleteOriginal: true), "\$"); + expect(array[2].toDartString(deleteOriginal: true), "33"); array.setRange( 0, 3, @@ -213,9 +213,9 @@ void main() { ], 1, ); - expect(array[0].toDartString(), "55"); - expect(array[1].toDartString(), "66"); - expect(array[2].toDartString(), "77"); + expect(array[0].toDartString(deleteOriginal: true), "55"); + expect(array[1].toDartString(deleteOriginal: true), "66"); + expect(array[2].toDartString(deleteOriginal: true), "77"); expect(() { final _ = array[-1]; }, throwsRangeError); @@ -256,4 +256,21 @@ void main() { expect(twoDimArray[2][2], 4); }); }); + test("JArray.filled", () { + using((arena) { + final string = "abc".toJString()..deletedIn(arena); + final array = JArray.filled(3, string)..deletedIn(arena); + expect( + () { + final _ = JArray.filled(3, JString.fromRef(nullptr)) + ..deletedIn(arena); + }, + throwsA(isA()), + ); + expect(array.length, 3); + expect(array[0].toDartString(deleteOriginal: true), "abc"); + expect(array[1].toDartString(deleteOriginal: true), "abc"); + expect(array[2].toDartString(deleteOriginal: true), "abc"); + }); + }); }