Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Override JObject's toString with appropriate Java's toString (#337)
Browse files Browse the repository at this point in the history
* add toString
* remove extra hashCode and == as JObject already has it
  • Loading branch information
HosseinYousefi committed Jul 31, 2023
1 parent be7a930 commit 71425b0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
9 changes: 9 additions & 0 deletions jni/lib/src/jobject.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ class JObject extends JReference {
return Jni.accessors.callMethodWithArgs(reference, _equalsId,
JniCallType.booleanType, [other.reference]).boolean;
}

static final _toStringId = Jni.accessors.getMethodIDOf(
_objectClass.reference, r"toString", r"()Ljava/lang/String;");
@override
String toString() {
return JString.fromRef(Jni.accessors.callMethodWithArgs(
reference, _toStringId, JniCallType.objectType, []).object)
.toDartString(deleteOriginal: true);
}
}

/// A high level wrapper over a JNI class reference.
Expand Down
15 changes: 0 additions & 15 deletions jni/lib/src/util/jlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,6 @@ class JList<$E extends JObject> extends JObject with ListMixin<$E> {
JSet<$E> toSet() {
return toJSet(E);
}

static final _hashCodeId =
Jni.accessors.getMethodIDOf(_class.reference, r"hashCode", r"()I");
@override
int get hashCode => Jni.accessors.callMethodWithArgs(
reference, _hashCodeId, JniCallType.intType, []).integer;

static final _equalsId = Jni.accessors
.getMethodIDOf(_class.reference, r"equals", r"(Ljava/lang/Object;)Z");
@override
bool operator ==(Object other) {
if (other is! JObject) return false;
return Jni.accessors.callMethodWithArgs(reference, _equalsId,
JniCallType.booleanType, [other.reference]).boolean;
}
}

extension ToJavaList<E extends JObject> on Iterable<E> {
Expand Down
17 changes: 0 additions & 17 deletions jni/lib/src/util/jset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,6 @@ class JSet<$E extends JObject> extends JObject with SetMixin<$E> {
return super.retainAll(elements);
}

static final _hashCodeId =
Jni.accessors.getMethodIDOf(_class.reference, r"hashCode", r"()I");
@override
int get hashCode => Jni.accessors.callMethodWithArgs(
reference, _hashCodeId, JniCallType.intType, []).integer;

static final _equalsId = Jni.accessors
.getMethodIDOf(_class.reference, r"equals", r"(Ljava/lang/Object;)Z");
@override
bool operator ==(Object other) {
if (other is! JObject) {
return false;
}
return Jni.accessors.callMethodWithArgs(reference, _equalsId,
JniCallType.booleanType, [other.reference]).boolean;
}

@override
$E? lookup(Object? element) {
if (contains(element)) return element as $E;
Expand Down
5 changes: 5 additions & 0 deletions jni/test/jstring_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ void run({required TestRunnerCallback testRunner}) {
testStringBackAndForth('');
});
});

testRunner('Inherited toString', () {
final s = 'hello'.toJString();
expect(s.toString(), 'hello');
});
}

0 comments on commit 71425b0

Please sign in to comment.