Skip to content

Commit

Permalink
jnigen fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anikate-De committed Jul 7, 2024
1 parent 8a22001 commit 280b727
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 16 deletions.
5 changes: 0 additions & 5 deletions pkgs/ok_http/analysis_options.yaml

This file was deleted.

1 change: 1 addition & 0 deletions pkgs/ok_http/jnigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ classes:
- "okhttp3.Callback"
- "okhttp3.ConnectionPool"
- "okhttp3.Dispatcher"
- "java.util.concurrent.ExecutorService"
- "okhttp3.Cache"
- "com.example.ok_http.RedirectReceivedCallback"
- "com.example.ok_http.RedirectInterceptor"
Expand Down
322 changes: 319 additions & 3 deletions pkgs/ok_http/lib/src/jni/bindings.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// ignore_for_file: prefer_single_quotes
// ignore_for_file: directives_ordering
// ignore_for_file: prefer_expression_function_bodies
// ignore_for_file: unnecessary_parenthesis

// Autogenerated by jnigen. DO NOT EDIT!

// ignore_for_file: annotate_overrides
Expand Down Expand Up @@ -8527,10 +8532,10 @@ class Dispatcher extends jni.JObject {

/// from: public final java.util.concurrent.ExecutorService executorService()
/// The returned object must be released after use, by calling the [release] method.
jni.JObject executorService() {
ExecutorService executorService() {
return _executorService(
reference.pointer, _id_executorService as jni.JMethodIDPtr)
.object(const jni.JObjectType());
.object(const $ExecutorServiceType());
}

static final _id_new1 = _class.constructorId(
Expand All @@ -8551,7 +8556,7 @@ class Dispatcher extends jni.JObject {
/// from: public void <init>(java.util.concurrent.ExecutorService executorService)
/// The returned object must be released after use, by calling the [release] method.
factory Dispatcher.new1(
jni.JObject executorService,
ExecutorService executorService,
) {
return Dispatcher.fromReference(_new1(_class.reference.pointer,
_id_new1 as jni.JMethodIDPtr, executorService.reference.pointer)
Expand Down Expand Up @@ -8703,6 +8708,317 @@ final class $DispatcherType extends jni.JObjType<Dispatcher> {
}
}

/// from: java.util.concurrent.ExecutorService
class ExecutorService extends jni.JObject {
@override
late final jni.JObjType<ExecutorService> $type = type;

ExecutorService.fromReference(
jni.JReference reference,
) : super.fromReference(reference);

static final _class =
jni.JClass.forName(r"java/util/concurrent/ExecutorService");

/// The type which includes information such as the signature of this class.
static const type = $ExecutorServiceType();
static final _id_shutdown = _class.instanceMethodId(
r"shutdown",
r"()V",
);

static final _shutdown = ProtectedJniExtensions.lookup<
ffi.NativeFunction<
jni.JThrowablePtr Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>>("globalEnv_CallVoidMethod")
.asFunction<
jni.JThrowablePtr Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>();

/// from: public abstract void shutdown()
void shutdown() {
_shutdown(reference.pointer, _id_shutdown as jni.JMethodIDPtr).check();
}

static final _id_shutdownNow = _class.instanceMethodId(
r"shutdownNow",
r"()Ljava/util/List;",
);

static final _shutdownNow = ProtectedJniExtensions.lookup<
ffi.NativeFunction<
jni.JniResult Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>>("globalEnv_CallObjectMethod")
.asFunction<
jni.JniResult Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>();

/// from: public abstract java.util.List shutdownNow()
/// The returned object must be released after use, by calling the [release] method.
jni.JList<jni.JObject> shutdownNow() {
return _shutdownNow(reference.pointer, _id_shutdownNow as jni.JMethodIDPtr)
.object(const jni.JListType(jni.JObjectType()));
}

static final _id_isShutdown = _class.instanceMethodId(
r"isShutdown",
r"()Z",
);

static final _isShutdown = ProtectedJniExtensions.lookup<
ffi.NativeFunction<
jni.JniResult Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>>("globalEnv_CallBooleanMethod")
.asFunction<
jni.JniResult Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>();

/// from: public abstract boolean isShutdown()
bool isShutdown() {
return _isShutdown(reference.pointer, _id_isShutdown as jni.JMethodIDPtr)
.boolean;
}

static final _id_isTerminated = _class.instanceMethodId(
r"isTerminated",
r"()Z",
);

static final _isTerminated = ProtectedJniExtensions.lookup<
ffi.NativeFunction<
jni.JniResult Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>>("globalEnv_CallBooleanMethod")
.asFunction<
jni.JniResult Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
)>();

/// from: public abstract boolean isTerminated()
bool isTerminated() {
return _isTerminated(
reference.pointer, _id_isTerminated as jni.JMethodIDPtr)
.boolean;
}

static final _id_awaitTermination = _class.instanceMethodId(
r"awaitTermination",
r"(JLjava/util/concurrent/TimeUnit;)Z",
);

static final _awaitTermination = ProtectedJniExtensions.lookup<
ffi.NativeFunction<
jni.JniResult Function(
ffi.Pointer<ffi.Void>,
jni.JMethodIDPtr,
ffi.VarArgs<(ffi.Int64, ffi.Pointer<ffi.Void>)>)>>(
"globalEnv_CallBooleanMethod")
.asFunction<
jni.JniResult Function(ffi.Pointer<ffi.Void>, jni.JMethodIDPtr, int,
ffi.Pointer<ffi.Void>)>();

/// from: public abstract boolean awaitTermination(long j, java.util.concurrent.TimeUnit timeUnit)
bool awaitTermination(
int j,
jni.JObject timeUnit,
) {
return _awaitTermination(
reference.pointer,
_id_awaitTermination as jni.JMethodIDPtr,
j,
timeUnit.reference.pointer)
.boolean;
}

/// Maps a specific port to the implemented interface.
static final Map<int, $ExecutorServiceImpl> _$impls = {};
ReceivePort? _$p;

static jni.JObjectPtr _$invoke(
int port,
jni.JObjectPtr descriptor,
jni.JObjectPtr args,
) {
return _$invokeMethod(
port,
$MethodInvocation.fromAddresses(
0,
descriptor.address,
args.address,
),
);
}

static final ffi.Pointer<
ffi.NativeFunction<
jni.JObjectPtr Function(
ffi.Uint64, jni.JObjectPtr, jni.JObjectPtr)>>
_$invokePointer = ffi.Pointer.fromFunction(_$invoke);

static ffi.Pointer<ffi.Void> _$invokeMethod(
int $p,
$MethodInvocation $i,
) {
try {
final $d = $i.methodDescriptor.toDartString(releaseOriginal: true);
final $a = $i.args;
if ($d == r"shutdown()V") {
_$impls[$p]!.shutdown();
return jni.nullptr;
}
if ($d == r"shutdownNow()Ljava/util/List;") {
final $r = _$impls[$p]!.shutdownNow();
return ($r as jni.JObject)
.castTo(const jni.JObjectType())
.reference
.toPointer();
}
if ($d == r"isShutdown()Z") {
final $r = _$impls[$p]!.isShutdown();
return jni.JBoolean($r).reference.toPointer();
}
if ($d == r"isTerminated()Z") {
final $r = _$impls[$p]!.isTerminated();
return jni.JBoolean($r).reference.toPointer();
}
if ($d == r"awaitTermination(JLjava/util/concurrent/TimeUnit;)Z") {
final $r = _$impls[$p]!.awaitTermination(
$a[0]
.castTo(const jni.JLongType(), releaseOriginal: true)
.longValue(releaseOriginal: true),
$a[1].castTo(const jni.JObjectType(), releaseOriginal: true),
);
return jni.JBoolean($r).reference.toPointer();
}
} catch (e) {
return ProtectedJniExtensions.newDartException(e);
}
return jni.nullptr;
}

factory ExecutorService.implement(
$ExecutorServiceImpl $impl,
) {
final $p = ReceivePort();
final $x = ExecutorService.fromReference(
ProtectedJniExtensions.newPortProxy(
r"java.util.concurrent.ExecutorService",
$p,
_$invokePointer,
),
).._$p = $p;
final $a = $p.sendPort.nativePort;
_$impls[$a] = $impl;
$p.listen(($m) {
if ($m == null) {
_$impls.remove($p.sendPort.nativePort);
$p.close();
return;
}
final $i = $MethodInvocation.fromMessage($m as List<dynamic>);
final $r = _$invokeMethod($p.sendPort.nativePort, $i);
ProtectedJniExtensions.returnResult($i.result, $r);
});
return $x;
}
}

abstract interface class $ExecutorServiceImpl {
factory $ExecutorServiceImpl({
required void Function() shutdown,
required jni.JList<jni.JObject> Function() shutdownNow,
required bool Function() isShutdown,
required bool Function() isTerminated,
required bool Function(int j, jni.JObject timeUnit) awaitTermination,
}) = _$ExecutorServiceImpl;

void shutdown();
jni.JList<jni.JObject> shutdownNow();
bool isShutdown();
bool isTerminated();
bool awaitTermination(int j, jni.JObject timeUnit);
}

class _$ExecutorServiceImpl implements $ExecutorServiceImpl {
_$ExecutorServiceImpl({
required void Function() shutdown,
required jni.JList<jni.JObject> Function() shutdownNow,
required bool Function() isShutdown,
required bool Function() isTerminated,
required bool Function(int j, jni.JObject timeUnit) awaitTermination,
}) : _shutdown = shutdown,
_shutdownNow = shutdownNow,
_isShutdown = isShutdown,
_isTerminated = isTerminated,
_awaitTermination = awaitTermination;

final void Function() _shutdown;
final jni.JList<jni.JObject> Function() _shutdownNow;
final bool Function() _isShutdown;
final bool Function() _isTerminated;
final bool Function(int j, jni.JObject timeUnit) _awaitTermination;

void shutdown() {
return _shutdown();
}

jni.JList<jni.JObject> shutdownNow() {
return _shutdownNow();
}

bool isShutdown() {
return _isShutdown();
}

bool isTerminated() {
return _isTerminated();
}

bool awaitTermination(int j, jni.JObject timeUnit) {
return _awaitTermination(j, timeUnit);
}
}

final class $ExecutorServiceType extends jni.JObjType<ExecutorService> {
const $ExecutorServiceType();

@override
String get signature => r"Ljava/util/concurrent/ExecutorService;";

@override
ExecutorService fromReference(jni.JReference reference) =>
ExecutorService.fromReference(reference);

@override
jni.JObjType get superType => const jni.JObjectType();

@override
final superCount = 1;

@override
int get hashCode => ($ExecutorServiceType).hashCode;

@override
bool operator ==(Object other) {
return other.runtimeType == ($ExecutorServiceType) &&
other is $ExecutorServiceType;
}
}

/// from: okhttp3.Cache$Companion
class Cache_Companion extends jni.JObject {
@override
Expand Down
9 changes: 1 addition & 8 deletions pkgs/ok_http/lib/src/ok_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ class OkHttpClient extends BaseClient {
// Refer to OkHttp documentation for the shutdown procedure:
// https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/index.html#:~:text=Shutdown

// Bindings for `java.util.concurrent.ExecutorService` are erroneous.
// https://github.com/dart-lang/native/issues/588
// So, use the JClass API to call the `shutdown` method by its signature.
_client
.dispatcher()
.executorService()
.jClass
.instanceMethodId('shutdown', '()V');
_client.dispatcher().executorService().shutdown();

// Remove all idle connections from the resource pool.
_client.connectionPool().evictAll();
Expand Down

0 comments on commit 280b727

Please sign in to comment.