From 71425b0b9b7a8e7a774a4cbcf92c831bcc921bd9 Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Mon, 31 Jul 2023 13:05:06 +0200 Subject: [PATCH] Override JObject's toString with appropriate Java's toString (#337) * add toString * remove extra hashCode and == as JObject already has it --- jni/lib/src/jobject.dart | 9 +++++++++ jni/lib/src/util/jlist.dart | 15 --------------- jni/lib/src/util/jset.dart | 17 ----------------- jni/test/jstring_test.dart | 5 +++++ 4 files changed, 14 insertions(+), 32 deletions(-) diff --git a/jni/lib/src/jobject.dart b/jni/lib/src/jobject.dart index 5de5bbdf..26df6be1 100644 --- a/jni/lib/src/jobject.dart +++ b/jni/lib/src/jobject.dart @@ -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. diff --git a/jni/lib/src/util/jlist.dart b/jni/lib/src/util/jlist.dart index eac7f20c..8087af98 100644 --- a/jni/lib/src/util/jlist.dart +++ b/jni/lib/src/util/jlist.dart @@ -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 on Iterable { diff --git a/jni/lib/src/util/jset.dart b/jni/lib/src/util/jset.dart index 53001078..367c1213 100644 --- a/jni/lib/src/util/jset.dart +++ b/jni/lib/src/util/jset.dart @@ -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; diff --git a/jni/test/jstring_test.dart b/jni/test/jstring_test.dart index 1bc6436a..210c91d6 100644 --- a/jni/test/jstring_test.dart +++ b/jni/test/jstring_test.dart @@ -43,4 +43,9 @@ void run({required TestRunnerCallback testRunner}) { testStringBackAndForth(''); }); }); + + testRunner('Inherited toString', () { + final s = 'hello'.toJString(); + expect(s.toString(), 'hello'); + }); }