Skip to content

Commit

Permalink
Remove isNull from JObject
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi committed Nov 8, 2024
1 parent 80693da commit 53ed33f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pkgs/jni/lib/src/jarray.dart
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ extension DoubleArray on JArray<jdouble> {
}
}

extension ObjectArray<T extends JObject> on JArray<T> {
extension ObjectArray<T extends JObject?> on JArray<T> {
T operator [](int index) {
RangeError.checkValidIndex(index, this);
return (elementType as JObjType<T>).fromReference(JGlobalReference(
Expand All @@ -418,7 +418,7 @@ extension ObjectArray<T extends JObject> on JArray<T> {

void operator []=(int index, T value) {
RangeError.checkValidIndex(index, this);
final valueRef = value.reference;
final valueRef = value?.reference ?? jNullReference;
Jni.env.SetObjectArrayElement(reference.pointer, index, valueRef.pointer);
}

Expand Down
3 changes: 3 additions & 0 deletions pkgs/jni/lib/src/lang/jstring.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class JString extends JObject {
/// The type which includes information such as the signature of this class.
static const JObjType<JString> type = JStringType();

/// The type which includes information such as the signature of this class.
static const JObjType<JString?> nullableType = JStringNullableType();

/// Construct a new [JString] with [reference] as its underlying reference.
JString.fromReference(super.reference) : super.fromReference();

Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/lib/src/util/jlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class JList<$E extends JObject?> extends JObject with ListMixin<$E> {
}
}

extension ToJavaList<E extends JObject> on Iterable<E> {
extension ToJavaList<E extends JObject?> on Iterable<E> {
JList<E> toJList(JObjType<E> type) {
final list = JList.array(type);
list.addAll(this);
Expand Down
26 changes: 13 additions & 13 deletions pkgs/jni/test/jarray_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,75 +389,75 @@ void run({required TestRunnerCallback testRunner}) {
});
testRunner('JArray of JByte', () {
using((arena) {
final arr = JArray(JByte.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
final arr = JArray(JByte.nullableType, 1)..releasedBy(arena);
expect(arr[0], isNull);
});
});
testRunner('JArray of JShort', () {
using((arena) {
final arr = JArray(JShort.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JInteger', () {
using((arena) {
final arr = JArray(JInteger.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JCharacter', () {
using((arena) {
final arr = JArray(JCharacter.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JLong', () {
using((arena) {
final arr = JArray(JLong.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JFloat', () {
using((arena) {
final arr = JArray(JFloat.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JDouble', () {
using((arena) {
final arr = JArray(JDouble.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JBoolean', () {
using((arena) {
final arr = JArray(JBoolean.type, 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JSet', () {
using((arena) {
final arr = JArray(JSet.type(JString.type), 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JList', () {
using((arena) {
final arr = JArray(JList.type(JString.type), 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JMap', () {
using((arena) {
final arr = JArray(JMap.type(JString.type, JString.type), 1)
..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
testRunner('JArray of JIterator', () {
using((arena) {
final arr = JArray(JIterator.type(JString.type), 1)..releasedBy(arena);
expect((arr[0]..releasedBy(arena)).isNull, true);
expect(arr[0], isNull);
});
});
}
9 changes: 7 additions & 2 deletions pkgs/jni/test/jlist_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ void run({required TestRunnerCallback testRunner}) {
});
testRunner('length set', () {
using((arena) {
final list = testDataList(arena);
final list = <JString?>[
'1'.toJString()..releasedBy(arena),
'2'.toJString()..releasedBy(arena),
'3'.toJString()..releasedBy(arena),
].toJList(JString.nullableType)
..releasedBy(arena);
list.length = 2;
expect(list.length, 2);
list.length = 3;
expect(list.length, 3);
expect(list.last.isNull, true);
expect(list.last, isNull);
});
});
testRunner('[]', () {
Expand Down

0 comments on commit 53ed33f

Please sign in to comment.