[jnigen] Override JObject's toString with appropriate Java's toString (https://github.com/dart-lang/jnigen/issues/337)
* add toString
* remove extra hashCode and == as JObject already has it
diff --git a/pkgs/jni/lib/src/jobject.dart b/pkgs/jni/lib/src/jobject.dart
index 5de5bbd..26df6be 100644
--- a/pkgs/jni/lib/src/jobject.dart
+++ b/pkgs/jni/lib/src/jobject.dart
@@ -342,6 +342,15 @@
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/pkgs/jni/lib/src/util/jlist.dart b/pkgs/jni/lib/src/util/jlist.dart
index eac7f20..8087af9 100644
--- a/pkgs/jni/lib/src/util/jlist.dart
+++ b/pkgs/jni/lib/src/util/jlist.dart
@@ -267,21 +267,6 @@
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> {
diff --git a/pkgs/jni/lib/src/util/jset.dart b/pkgs/jni/lib/src/util/jset.dart
index 5300107..367c121 100644
--- a/pkgs/jni/lib/src/util/jset.dart
+++ b/pkgs/jni/lib/src/util/jset.dart
@@ -190,23 +190,6 @@
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/pkgs/jni/test/jstring_test.dart b/pkgs/jni/test/jstring_test.dart
index 1bc6436..210c91d 100644
--- a/pkgs/jni/test/jstring_test.dart
+++ b/pkgs/jni/test/jstring_test.dart
@@ -43,4 +43,9 @@
testStringBackAndForth('');
});
});
+
+ testRunner('Inherited toString', () {
+ final s = 'hello'.toJString();
+ expect(s.toString(), 'hello');
+ });
}