Skip to content

Commit f208cfc

Browse files
authored
[jni] Support custom exceptions (dart-lang#3190)
1 parent f2faed2 commit f208cfc

14 files changed

Lines changed: 5086 additions & 2081 deletions

File tree

‎pkgs/jni/CHANGELOG.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 0.16.0
1+
## 0.16.0-wip
22

33
- **Breaking Change**: All Java wrapper classes have been migrated to extension
44
types. The main effects are:
@@ -17,6 +17,13 @@
1717
- Changed the behavior of `JObject.releasedBy`. It now does not throw a
1818
`DoubleReleaseError` if the object was manually released before the end of
1919
arena.
20+
- Added `JThrowable` class which inherits from `JObject` and implements
21+
`Exception`.
22+
- **Breaking Change**: `JniException` has been deleted. Java exceptions are now
23+
thrown as `JThrowable` instead. `JThrowable` holds an actual Java exception,
24+
instead of just holding a string message. It's a `JObject`, so the usual
25+
`.isA` and `.as` methods work to cast the `JThrowable` to the underlying Java
26+
exception.
2027

2128
## 0.15.2
2229

‎pkgs/jni/lib/src/errors.dart‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,6 @@ final class NoJvmInstanceError extends Error {
106106
String toString() => 'No JNI instance is available';
107107
}
108108

109-
// TODO(#567): Remove this class in favor of `JThrowable`.
110-
class JniException implements Exception {
111-
/// Error message from Java exception.
112-
final String message;
113-
114-
/// Stack trace from Java.
115-
final String stackTrace;
116-
117-
JniException(this.message, this.stackTrace);
118-
119-
@override
120-
String toString() => 'Exception in Java code called through JNI: '
121-
'$message\n\n$stackTrace\n';
122-
}
123-
124109
final class HelperNotFoundError extends Error {
125110
final String path;
126111

@@ -155,3 +140,11 @@ Please ensure ${Platform.isWindows ? r'that `\bin\server\jvm.dll` is in the PATH
155140
''';
156141
}
157142
}
143+
144+
final class JniNewStringException implements Exception {
145+
final String string;
146+
JniNewStringException(this.string);
147+
148+
@override
149+
String toString() => 'Failed to convert string to a JString: $string';
150+
}

‎pkgs/jni/lib/src/jni.dart‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,16 @@ abstract final class Jni {
199199
}
200200

201201
/// Throws an exception.
202-
// TODO(#561): Throw an actual `JThrowable`.
203202
@internal
204203
static void throwException(JThrowablePtr exception) {
205204
final details = _bindings.GetExceptionDetails(exception);
206205
final env = Jni.env;
207206
final message = env.toDartString(details.message);
208207
final stacktrace = env.toDartString(details.stacktrace);
209-
env.DeleteGlobalRef(exception);
210208
env.DeleteGlobalRef(details.message);
211209
env.DeleteGlobalRef(details.stacktrace);
212-
throw JniException(message, stacktrace);
210+
throw JThrowable.fromReference(
211+
JGlobalReference(exception), message, stacktrace);
213212
}
214213

215214
/// Returns the instance of [GlobalJniEnvStruct], which is an abstraction over
@@ -422,8 +421,7 @@ extension AdditionalEnvMethods on GlobalJniEnv {
422421
final utf = s.toNativeUtf16(allocator: arena).cast<Uint16>();
423422
final result = NewString(utf, s.length);
424423
if (utf == nullptr) {
425-
throw JniException(
426-
'Fatal: cannot convert string to Java string: $s', '');
424+
throw JniNewStringException(s);
427425
}
428426
return result;
429427
});

‎pkgs/jni/lib/src/jobject.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ class JObject {
164164
}
165165
}
166166

167+
/// A high-level wrapper for JNI global object reference of a
168+
/// `java.lang.Throwable`.
169+
///
170+
/// This is the base class for all exceptions generated by `jnigen`.
171+
class JThrowable extends JObject implements Exception {
172+
final String message;
173+
final String javaStackTrace;
174+
175+
@internal
176+
JThrowable.fromReference(
177+
super.reference,
178+
this.message,
179+
this.javaStackTrace,
180+
) : super.fromReference();
181+
182+
@override
183+
String toString() => 'Exception in Java: $message\n$javaStackTrace';
184+
}
185+
167186
extension JObjectUseExtension<T extends JObject?> on T {
168187
/// Applies [callback] on this object and then delete the underlying JNI
169188
/// reference, returning the result of [callback].

0 commit comments

Comments
 (0)