Skip to content

Commit 132972f

Browse files
Address comments
1 parent c10e189 commit 132972f

File tree

3 files changed

+101
-27
lines changed

3 files changed

+101
-27
lines changed

pkgs/jnigen/test/simple_package_test/bindings/simple_package.dart

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,7 +4428,7 @@ class StringConverterConsumer extends jni.JObject {
44284428

44294429
static final _id_consumeOnSameThread = _class.staticMethodId(
44304430
r"consumeOnSameThread",
4431-
r"(Lcom/github/dart_lang/jnigen/interfaces/StringConverter;Ljava/lang/String;)I",
4431+
r"(Lcom/github/dart_lang/jnigen/interfaces/StringConverter;Ljava/lang/String;)Ljava/lang/Integer;",
44324432
);
44334433

44344434
static final _consumeOnSameThread = ProtectedJniExtensions.lookup<
@@ -4440,13 +4440,14 @@ class StringConverterConsumer extends jni.JObject {
44404440
(
44414441
ffi.Pointer<ffi.Void>,
44424442
ffi.Pointer<ffi.Void>
4443-
)>)>>("globalEnv_CallStaticIntMethod")
4443+
)>)>>("globalEnv_CallStaticObjectMethod")
44444444
.asFunction<
44454445
jni.JniResult Function(ffi.Pointer<ffi.Void>, jni.JMethodIDPtr,
44464446
ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Void>)>();
44474447

4448-
/// from: static public int consumeOnSameThread(com.github.dart_lang.jnigen.interfaces.StringConverter stringConverter, java.lang.String s)
4449-
static int consumeOnSameThread(
4448+
/// from: static public java.lang.Integer consumeOnSameThread(com.github.dart_lang.jnigen.interfaces.StringConverter stringConverter, java.lang.String s)
4449+
/// The returned object must be released after use, by calling the [release] method.
4450+
static jni.JInteger consumeOnSameThread(
44504451
StringConverter stringConverter,
44514452
jni.JString s,
44524453
) {
@@ -4455,7 +4456,40 @@ class StringConverterConsumer extends jni.JObject {
44554456
_id_consumeOnSameThread as jni.JMethodIDPtr,
44564457
stringConverter.reference.pointer,
44574458
s.reference.pointer)
4458-
.integer;
4459+
.object(const jni.JIntegerType());
4460+
}
4461+
4462+
static final _id_consumeOnAnotherThread = _class.staticMethodId(
4463+
r"consumeOnAnotherThread",
4464+
r"(Lcom/github/dart_lang/jnigen/interfaces/StringConverter;Ljava/lang/String;)Ljava/util/concurrent/Future;",
4465+
);
4466+
4467+
static final _consumeOnAnotherThread = ProtectedJniExtensions.lookup<
4468+
ffi.NativeFunction<
4469+
jni.JniResult Function(
4470+
ffi.Pointer<ffi.Void>,
4471+
jni.JMethodIDPtr,
4472+
ffi.VarArgs<
4473+
(
4474+
ffi.Pointer<ffi.Void>,
4475+
ffi.Pointer<ffi.Void>
4476+
)>)>>("globalEnv_CallStaticObjectMethod")
4477+
.asFunction<
4478+
jni.JniResult Function(ffi.Pointer<ffi.Void>, jni.JMethodIDPtr,
4479+
ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Void>)>();
4480+
4481+
/// from: static public java.util.concurrent.Future<java.lang.Integer> consumeOnAnotherThread(com.github.dart_lang.jnigen.interfaces.StringConverter stringConverter, java.lang.String s)
4482+
/// The returned object must be released after use, by calling the [release] method.
4483+
static jni.JObject consumeOnAnotherThread(
4484+
StringConverter stringConverter,
4485+
jni.JString s,
4486+
) {
4487+
return _consumeOnAnotherThread(
4488+
_class.reference.pointer,
4489+
_id_consumeOnAnotherThread as jni.JMethodIDPtr,
4490+
stringConverter.reference.pointer,
4491+
s.reference.pointer)
4492+
.object(const jni.JObjectType());
44594493
}
44604494
}
44614495

pkgs/jnigen/test/simple_package_test/java/com/github/dart_lang/jnigen/interfaces/StringConverterConsumer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
package com.github.dart_lang.jnigen.interfaces;
66

7-
public class StringConverterConsumer {
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.Future;
810

9-
public static int consumeOnSameThread(StringConverter stringConverter, String s) {
11+
public class StringConverterConsumer {
12+
public static Integer consumeOnSameThread(StringConverter stringConverter, String s) {
1013
try {
1114
return stringConverter.parseToInt(s);
1215
} catch (StringConversionException e) {
1316
return -1;
1417
}
1518
}
19+
20+
public static Future<Integer> consumeOnAnotherThread(StringConverter stringConverter, String s) {
21+
ExecutorService executor = Executors.newSingleThreadExecutor();
22+
return executor.submit(() -> consumeOnSameThread(stringConverter, s));
23+
}
1624
}

pkgs/jnigen/test/simple_package_test/runtime_test_registrant.dart

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:io';
7+
import 'dart:isolate';
78

89
import 'package:test/test.dart';
910
import 'package:jni/jni.dart';
@@ -689,29 +690,60 @@ void registerTests(String groupName, TestRunnerCallback test) {
689690
});
690691

691692
group('throw Java exceptions', () {
692-
test('StringConverter.implement', () async {
693-
final stringConverter = StringConverter.implement($StringConverterImpl(
694-
parseToInt: (s) {
695-
final value = int.tryParse(s.toDartString());
696-
if (value == null) {
697-
throw StringConversionException(
698-
'Invalid integer expression: ${s.toDartString()}'
699-
.toJString());
700-
}
693+
for (final (threading, consume) in [
694+
('another thread', StringConverterConsumer.consumeOnAnotherThread),
695+
('the same thread', StringConverterConsumer.consumeOnSameThread),
696+
]) {
697+
test('StringConverter.implement on $threading ', () async {
698+
final stringConverter =
699+
StringConverter.implement($StringConverterImpl(
700+
parseToInt: (s) {
701+
final value = int.tryParse(s.toDartString());
702+
if (value == null) {
703+
throw StringConversionException(
704+
'Invalid integer expression: $s'.toJString());
705+
}
701706

702-
return value;
703-
},
704-
));
707+
return value;
708+
},
709+
));
710+
711+
// Gets the result of a Java Future.
712+
// TODO(#1213): remove this once we support Java futures.
713+
Future<$T> toDartFuture<$T extends JObject>(
714+
JObject future, JObjType<$T> T) {
715+
return Isolate.run(() {
716+
final futureClass = JClass.forName('java/util/concurrent');
717+
final getMethod = futureClass.instanceMethodId(
718+
'get', '(Ljava/lang/Object;)Ljava/lang/Object;');
719+
final result = getMethod(future, JObject.type, []);
720+
return result.castTo(T);
721+
});
722+
}
705723

706-
var result = StringConverterConsumer.consumeOnSameThread(
707-
stringConverter, '700'.toJString());
708-
expect(result, 700);
724+
final sevenHundredBoxed = consume(stringConverter, '700'.toJString());
725+
final int sevenHundred;
726+
if (sevenHundredBoxed is JInteger) {
727+
sevenHundred = sevenHundredBoxed.intValue();
728+
} else {
729+
sevenHundred =
730+
(await toDartFuture(sevenHundredBoxed, JInteger.type))
731+
.intValue();
732+
}
733+
expect(sevenHundred, 700);
734+
735+
final fooBoxed = consume(stringConverter, 'foo'.toJString());
736+
final int foo;
737+
if (fooBoxed is JInteger) {
738+
foo = fooBoxed.intValue();
739+
} else {
740+
foo = (await toDartFuture(fooBoxed, JInteger.type)).intValue();
741+
}
742+
expect(foo, -1);
709743

710-
result = StringConverterConsumer.consumeOnSameThread(
711-
stringConverter, 'foo'.toJString());
712-
expect(result, -1);
713-
stringConverter.release();
714-
});
744+
stringConverter.release();
745+
});
746+
}
715747
});
716748
});
717749

0 commit comments

Comments
 (0)