Skip to content

Commit 117cc75

Browse files
[jnigen] Refactor type classes (#1591)
* Make all type class methods and fields internal * Use better import prefixes * Rename `castTo` to `as` * Relax renaming keywords * Rename to `MethodInvocation` * Rename typeclass of `Foo` to `$Foo$Type` * Make generic type parameters internal
1 parent 82c0aac commit 117cc75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+9917
-8605
lines changed

pkgs/jni/example/lib/main.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int randomUsingEnv(int n) => using((arena) {
5454
double randomDouble() {
5555
final math = JClass.forName("java/lang/Math");
5656
final random =
57-
math.staticMethodId("random", "()D").call(math, const jdoubleType(), []);
57+
math.staticMethodId("random", "()D").call(math, jdouble.type, []);
5858
math.release();
5959
return random;
6060
}
@@ -63,7 +63,7 @@ int uptime() {
6363
return JClass.forName("android/os/SystemClock").use(
6464
(systemClock) => systemClock
6565
.staticMethodId("uptimeMillis", "()J")
66-
.call(systemClock, const jlongType(), []),
66+
.call(systemClock, jlong.type, []),
6767
);
6868
}
6969

@@ -74,9 +74,8 @@ String backAndForth() {
7474
}
7575

7676
void quit() {
77-
JObject.fromReference(Jni.getCurrentActivity()).use((ac) => ac.jClass
78-
.instanceMethodId("finish", "()V")
79-
.call(ac, const jvoidType(), []));
77+
JObject.fromReference(Jni.getCurrentActivity()).use((ac) =>
78+
ac.jClass.instanceMethodId("finish", "()V").call(ac, jvoid.type, []));
8079
}
8180

8281
void showToast(String text) {
@@ -94,14 +93,14 @@ void showToast(String text) {
9493
'(Landroid/app/Activity;Landroid/content/Context;'
9594
'Ljava/lang/CharSequence;I)'
9695
'Lcom/github/dart_lang/jni_example/Toaster;');
97-
final toaster = makeText.call(toasterClass, const JObjectType(), [
96+
final toaster = makeText.call(toasterClass, JObject.type, [
9897
Jni.getCurrentActivity(),
9998
Jni.getCachedApplicationContext(),
10099
'😀'.toJString(),
101100
0,
102101
]);
103102
final show = toasterClass.instanceMethodId('show', '()V');
104-
show(toaster, const jvoidType(), []);
103+
show(toaster, jvoid.type, []);
105104
}
106105

107106
void main() {

pkgs/jni/lib/_internal.dart

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,68 @@
88
/// not to be used directly.
99
library;
1010

11-
import 'dart:ffi' as ffi;
11+
import 'dart:ffi' as ffi show Int32;
12+
import 'dart:ffi' hide Int32;
13+
14+
// Exporting all the necessary bits for the generated bindings.
15+
export 'dart:ffi'
16+
show
17+
Double,
18+
Int64,
19+
NativeFunction,
20+
NativeFunctionPointer,
21+
NativePort,
22+
Pointer,
23+
VarArgs,
24+
Void,
25+
nullptr;
26+
export 'dart:isolate' show RawReceivePort, ReceivePort;
27+
28+
export 'package:meta/meta.dart' show internal;
1229

1330
export 'src/accessors.dart';
1431
export 'src/jni.dart' show ProtectedJniExtensions;
1532
export 'src/jreference.dart';
1633
export 'src/method_invocation.dart';
17-
export 'src/types.dart' show referenceType;
34+
export 'src/types.dart'
35+
show
36+
JAccessible,
37+
JArrayElementType,
38+
JCallable,
39+
JConstructable,
40+
JObjType,
41+
JType,
42+
lowestCommonSuperType,
43+
referenceType;
1844

1945
/// Temporary fix for the macOS arm64 varargs problem.
2046
///
21-
/// This integer type is Int32 on all architectures, other than macOS arm64.
22-
/// Where it is Int64.
23-
@ffi.AbiSpecificIntegerMapping({
24-
ffi.Abi.androidArm: ffi.Int32(),
25-
ffi.Abi.androidArm64: ffi.Int32(),
26-
ffi.Abi.androidIA32: ffi.Int32(),
27-
ffi.Abi.androidX64: ffi.Int32(),
28-
ffi.Abi.androidRiscv64: ffi.Int32(),
29-
ffi.Abi.fuchsiaArm64: ffi.Int32(),
30-
ffi.Abi.fuchsiaX64: ffi.Int32(),
31-
ffi.Abi.fuchsiaRiscv64: ffi.Int32(),
32-
ffi.Abi.iosArm: ffi.Int32(),
33-
ffi.Abi.iosArm64: ffi.Int32(),
34-
ffi.Abi.iosX64: ffi.Int32(),
35-
ffi.Abi.linuxArm: ffi.Int32(),
36-
ffi.Abi.linuxArm64: ffi.Int32(),
37-
ffi.Abi.linuxIA32: ffi.Int32(),
38-
ffi.Abi.linuxX64: ffi.Int32(),
39-
ffi.Abi.linuxRiscv32: ffi.Int32(),
40-
ffi.Abi.linuxRiscv64: ffi.Int32(),
41-
ffi.Abi.macosArm64: ffi.Int64(), // <-- Only this is different.
42-
ffi.Abi.macosX64: ffi.Int32(),
43-
ffi.Abi.windowsArm64: ffi.Int32(),
44-
ffi.Abi.windowsIA32: ffi.Int32(),
45-
ffi.Abi.windowsX64: ffi.Int32(),
47+
/// This integer type is `Int32` on all architectures, other than macOS arm64.
48+
/// Where it is `Int64`.
49+
@AbiSpecificIntegerMapping({
50+
Abi.androidArm: ffi.Int32(),
51+
Abi.androidArm64: ffi.Int32(),
52+
Abi.androidIA32: ffi.Int32(),
53+
Abi.androidX64: ffi.Int32(),
54+
Abi.androidRiscv64: ffi.Int32(),
55+
Abi.fuchsiaArm64: ffi.Int32(),
56+
Abi.fuchsiaX64: ffi.Int32(),
57+
Abi.fuchsiaRiscv64: ffi.Int32(),
58+
Abi.iosArm: ffi.Int32(),
59+
Abi.iosArm64: ffi.Int32(),
60+
Abi.iosX64: ffi.Int32(),
61+
Abi.linuxArm: ffi.Int32(),
62+
Abi.linuxArm64: ffi.Int32(),
63+
Abi.linuxIA32: ffi.Int32(),
64+
Abi.linuxX64: ffi.Int32(),
65+
Abi.linuxRiscv32: ffi.Int32(),
66+
Abi.linuxRiscv64: ffi.Int32(),
67+
Abi.macosArm64: Int64(), // <-- Only this is different.
68+
Abi.macosX64: ffi.Int32(),
69+
Abi.windowsArm64: ffi.Int32(),
70+
Abi.windowsIA32: ffi.Int32(),
71+
Abi.windowsX64: ffi.Int32(),
4672
})
47-
final class $Int32 extends ffi.AbiSpecificInteger {
48-
const $Int32();
73+
final class Int32 extends AbiSpecificInteger {
74+
const Int32();
4975
}

pkgs/jni/lib/jni.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
/// This library provides classes and functions for JNI interop from Dart.
6161
library;
6262

63-
export 'dart:ffi' show nullptr;
64-
6563
export 'package:ffi/ffi.dart' show Arena, using;
6664

6765
export 'src/errors.dart';
@@ -74,5 +72,13 @@ export 'src/lang/lang.dart';
7472
export 'src/nio/nio.dart';
7573
export 'src/third_party/generated_bindings.dart'
7674
hide JniBindings, JniEnv, JniEnv1, JniExceptionDetails;
77-
export 'src/types.dart' hide referenceType;
75+
export 'src/types.dart'
76+
hide
77+
JAccessible,
78+
JArrayElementType,
79+
JCallable,
80+
JConstructable,
81+
JObjType,
82+
JType,
83+
lowestCommonSuperType;
7884
export 'src/util/util.dart';

pkgs/jni/lib/src/accessors.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import 'dart:ffi';
66

77
import 'package:meta/meta.dart' show internal;
88

9-
import '../jni.dart';
9+
import 'jni.dart';
10+
import 'jobject.dart';
11+
import 'jreference.dart';
12+
import 'third_party/generated_bindings.dart';
13+
import 'types.dart';
1014

1115
void _check(JThrowablePtr exception) {
1216
if (exception != nullptr) {

pkgs/jni/lib/src/jarray.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@
77
part of 'types.dart';
88

99
final class JArrayType<E> extends JObjType<JArray<E>> {
10+
@internal
1011
final JArrayElementType<E> elementType;
1112

13+
@internal
1214
const JArrayType(this.elementType);
1315

16+
@internal
1417
@override
1518
String get signature => '[${elementType.signature}';
1619

20+
@internal
1721
@override
1822
JArray<E> fromReference(JReference reference) =>
1923
JArray.fromReference(elementType, reference);
2024

25+
@internal
2126
@override
2227
JObjType get superType => const JObjectType();
2328

29+
@internal
2430
@override
2531
final int superCount = 1;
2632

@@ -36,18 +42,21 @@ final class JArrayType<E> extends JObjType<JArray<E>> {
3642
}
3743

3844
class JArray<E> extends JObject {
45+
@internal
3946
final JArrayElementType<E> elementType;
4047

48+
@internal
4149
@override
42-
late final JArrayType<E> $type = type(elementType) as JArrayType<E>;
50+
final JArrayType<E> $type;
4351

4452
/// The type which includes information such as the signature of this class.
45-
static JObjType<JArray<T>> type<T>(JArrayElementType<T> innerType) =>
53+
static JArrayType<E> type<E>(JArrayElementType<E> innerType) =>
4654
JArrayType(innerType);
4755

4856
/// Construct a new [JArray] with [reference] as its underlying reference.
4957
JArray.fromReference(this.elementType, JReference reference)
50-
: super.fromReference(reference);
58+
: $type = type(elementType),
59+
super.fromReference(reference);
5160

5261
/// Creates a [JArray] of the given length from the given [elementType].
5362
///

pkgs/jni/lib/src/jimplementer.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import 'dart:ffi';
66
import 'dart:isolate';
77

88
import 'package:ffi/ffi.dart';
9-
import 'package:meta/meta.dart';
9+
import 'package:meta/meta.dart' show internal;
1010

11-
import '../_internal.dart';
11+
import 'accessors.dart';
12+
import 'jni.dart';
1213
import 'jobject.dart';
14+
import 'jreference.dart';
1315
import 'lang/jstring.dart';
1416
import 'third_party/generated_bindings.dart';
1517
import 'types.dart';

pkgs/jni/lib/src/jni.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import 'package:ffi/ffi.dart';
1010
import 'package:meta/meta.dart' show internal;
1111
import 'package:path/path.dart';
1212

13-
import '../_internal.dart';
14-
import '../jni.dart';
13+
import 'accessors.dart';
14+
import 'errors.dart';
15+
import 'jobject.dart';
16+
import 'jreference.dart';
1517
import 'third_party/generated_bindings.dart';
18+
import 'types.dart';
1619

1720
String _getLibraryFileName(String base) {
1821
if (Platform.isLinux || Platform.isAndroid) {

pkgs/jni/lib/src/jobject.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
54
import 'dart:ffi';
65

76
import 'package:ffi/ffi.dart';
7+
import 'package:meta/meta.dart' show internal;
88

99
import 'jni.dart';
1010
import 'jreference.dart';
1111
import 'lang/jstring.dart';
1212
import 'types.dart';
1313

1414
final class JObjectType extends JObjType<JObject> {
15+
@internal
1516
const JObjectType();
1617

18+
@internal
1719
@override
1820
String get signature => 'Ljava/lang/Object;';
1921

22+
@internal
2023
@override
2124
JObject fromReference(JReference reference) =>
2225
JObject.fromReference(reference);
2326

27+
@internal
2428
@override
2529
JObjType get superType => const JObjectType();
2630

2731
// TODO(#70): Once interface implementation lands, other than [superType],
2832
// we should have a list of implemented interfaces.
2933

34+
@internal
3035
@override
3136
final int superCount = 0;
3237

@@ -43,9 +48,11 @@ final class JObjectType extends JObjType<JObject> {
4348
///
4449
/// This is the base class for classes generated by `jnigen`.
4550
class JObject {
51+
@internal
4652
final JReference reference;
4753

48-
late final JObjType<JObject> $type = type;
54+
@internal
55+
final JObjType<JObject> $type = type;
4956

5057
/// The type which includes information such as the signature of this class.
5158
static const JObjType<JObject> type = JObjectType();
@@ -77,7 +84,7 @@ class JObject {
7784
/// Casts this object to another [type].
7885
///
7986
/// If [releaseOriginal] is `true`, the casted object will be released.
80-
T castTo<T extends JObject>(
87+
T as<T extends JObject>(
8188
JObjType<T> type, {
8289
bool releaseOriginal = false,
8390
}) {

0 commit comments

Comments
 (0)