Skip to content

Commit e77e0fe

Browse files
[jnigen] Change typeclass names and remove API leaks (#2536)
* Make JniError.of internal * Export needed types from thirdparty * Fix API type leakage * Rename typeclasses * Rename JObjType to JType * Hide unused symbols
1 parent e0cd7d4 commit e77e0fe

Some content is hidden

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

64 files changed

+6920
-5527
lines changed

pkgs/jni/CHANGELOG.md

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

3+
- **Breaking Change**: Made `Jni.env` internal.
4+
- **Breaking Change**: Renamed `JObjType` to `JType`.
5+
- **Breaking Change**: Made all of the type classes internal.
36
- Update to the latest lints.
47

58
## 0.14.2

pkgs/jni/example/lib/main.dart

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,11 @@
44

55
// ignore_for_file: library_private_types_in_public_api
66

7-
import 'dart:ffi';
87
import 'dart:io';
98

10-
import 'package:ffi/ffi.dart';
119
import 'package:flutter/material.dart';
1210
import 'package:jni/jni.dart';
1311

14-
extension on String {
15-
/// Returns a Utf-8 encoded `Pointer<Char>` with contents same as this string.
16-
Pointer<Char> toNativeChars(Allocator allocator) {
17-
return toNativeUtf8(allocator: allocator).cast<Char>();
18-
}
19-
}
20-
21-
// An example of calling JNI methods using low level primitives.
22-
// GlobalJniEnv is a thin abstraction over JNIEnv in JNI C API.
23-
//
24-
// For a more ergonomic API for common use cases of calling methods and
25-
// accessing fields, see next examples using JObject and JClass.
26-
String toJavaStringUsingEnv(int n) => using((arena) {
27-
final env = Jni.env;
28-
final cls = env.FindClass("java/lang/String".toNativeChars(arena));
29-
final mId = env.GetStaticMethodID(cls, "valueOf".toNativeChars(arena),
30-
"(I)Ljava/lang/String;".toNativeChars(arena));
31-
final i = arena<JValue>();
32-
i.ref.i = n;
33-
final res = env.CallStaticObjectMethodA(cls, mId, i);
34-
final str = env.toDartString(res);
35-
env.DeleteGlobalRef(res);
36-
env.DeleteGlobalRef(cls);
37-
return str;
38-
});
39-
40-
int randomUsingEnv(int n) => using((arena) {
41-
final env = Jni.env;
42-
final randomCls = env.FindClass("java/util/Random".toNativeChars(arena));
43-
final ctor = env.GetMethodID(
44-
randomCls, "<init>".toNativeChars(arena), "()V".toNativeChars(arena));
45-
final random = env.NewObject(randomCls, ctor);
46-
final nextInt = env.GetMethodID(randomCls, "nextInt".toNativeChars(arena),
47-
"(I)I".toNativeChars(arena));
48-
final res =
49-
env.CallIntMethodA(random, nextInt, toJValues([n], allocator: arena));
50-
env.DeleteGlobalRef(randomCls);
51-
env.DeleteGlobalRef(random);
52-
return res;
53-
});
5412
double randomDouble() {
5513
final math = JClass.forName("java/lang/Math");
5614
final random =
@@ -108,9 +66,6 @@ void main() {
10866
Jni.spawn();
10967
}
11068
final examples = [
111-
Example("String.valueOf(1332)", () => toJavaStringUsingEnv(1332)),
112-
Example("Generate random number", () => randomUsingEnv(180),
113-
runInitially: false),
11469
Example("Math.random()", () => randomDouble(), runInitially: false),
11570
if (Platform.isAndroid) ...[
11671
Example("Minutes of usage since reboot",

pkgs/jni/ffigen.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ structs:
7272
- 'CallbackResult'
7373
rename:
7474
## opaque struct definitions, base types of jfieldID and jmethodID
75+
'_Dart_FinalizableHandle': 'Dart_FinalizableHandle_'
7576
'_jfieldID': 'jfieldID_'
7677
'_jmethodID':
7778
'jmethodID_'

pkgs/jni/lib/_internal.dart

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,54 @@ export 'dart:isolate' show RawReceivePort, ReceivePort;
2828
export 'package:meta/meta.dart' show internal;
2929

3030
export 'src/accessors.dart';
31+
export 'src/jarray.dart'
32+
show
33+
$JArray$NullableType$,
34+
$JArray$Type$,
35+
$JBooleanArray$NullableType$,
36+
$JBooleanArray$Type$,
37+
$JByteArray$NullableType$,
38+
$JByteArray$Type$,
39+
$JCharArray$NullableType$,
40+
$JCharArray$Type$,
41+
$JDoubleArray$NullableType$,
42+
$JDoubleArray$Type$,
43+
$JFloatArray$NullableType$,
44+
$JFloatArray$Type$,
45+
$JIntArray$NullableType$,
46+
$JIntArray$Type$,
47+
$JLongArray$NullableType$,
48+
$JLongArray$Type$,
49+
$JShortArray$NullableType$,
50+
$JShortArray$Type$;
3151
export 'src/jni.dart' show ProtectedJniExtensions;
52+
export 'src/jobject.dart' show $JObject$NullableType$, $JObject$Type$;
3253
export 'src/jreference.dart';
3354
export 'src/kotlin.dart'
3455
show coroutineSingletonsClass, failureExceptionField, result$FailureClass;
56+
export 'src/lang/jboolean.dart' show $JBoolean$NullableType$, $JBoolean$Type$;
57+
export 'src/lang/jbyte.dart' show $JByte$NullableType$, $JByte$Type$;
58+
export 'src/lang/jcharacter.dart'
59+
show $JCharacter$NullableType$, $JCharacter$Type$;
60+
export 'src/lang/jdouble.dart' show $JDouble$NullableType$, $JDouble$Type$;
61+
export 'src/lang/jfloat.dart' show $JFloat$NullableType$, $JFloat$Type$;
62+
export 'src/lang/jinteger.dart' show $JInteger$NullableType$, $JInteger$Type$;
63+
export 'src/lang/jlong.dart' show $JLong$NullableType$, $JLong$Type$;
64+
export 'src/lang/jnumber.dart' show $JNumber$NullableType$, $JNumber$Type$;
65+
export 'src/lang/jshort.dart' show $JShort$NullableType$, $JShort$Type$;
66+
export 'src/lang/jstring.dart' show $JString$NullableType$, $JString$Type$;
3567
export 'src/method_invocation.dart';
36-
export 'src/types.dart'
37-
show
38-
JAccessible,
39-
JCallable,
40-
JConstructable,
41-
JObjType,
42-
JType,
43-
lowestCommonSuperType,
44-
referenceType;
68+
export 'src/nio/jbuffer.dart' show $JBuffer$NullableType$, $JBuffer$Type$;
69+
export 'src/nio/jbyte_buffer.dart'
70+
show $JByteBuffer$NullableType$, $JByteBuffer$Type$;
71+
export 'src/third_party/generated_bindings.dart'
72+
show JFieldIDPtr, JMethodIDPtr, JObjectPtr, JThrowablePtr, JniResult;
73+
export 'src/types.dart' show JTypeBase, lowestCommonSuperType, referenceType;
74+
export 'src/util/jiterator.dart'
75+
show $JIterator$NullableType$, $JIterator$Type$;
76+
export 'src/util/jlist.dart' show $JList$NullableType$, $JList$Type$;
77+
export 'src/util/jmap.dart' show $JMap$NullableType$, $JMap$Type$;
78+
export 'src/util/jset.dart' show $JSet$NullableType$, $JSet$Type$;
4579

4680
/// Temporary fix for the macOS arm64 varargs problem.
4781
///

pkgs/jni/lib/jni.dart

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,33 @@ library;
6363
export 'package:ffi/ffi.dart' show Arena, using;
6464

6565
export 'src/errors.dart';
66-
export 'src/jarray.dart';
66+
export 'src/jarray.dart'
67+
hide
68+
$JArray$NullableType$,
69+
$JArray$Type$,
70+
$JBooleanArray$NullableType$,
71+
$JBooleanArray$Type$,
72+
$JByteArray$NullableType$,
73+
$JByteArray$Type$,
74+
$JCharArray$NullableType$,
75+
$JCharArray$Type$,
76+
$JDoubleArray$NullableType$,
77+
$JDoubleArray$Type$,
78+
$JFloatArray$NullableType$,
79+
$JFloatArray$Type$,
80+
$JIntArray$NullableType$,
81+
$JIntArray$Type$,
82+
$JLongArray$NullableType$,
83+
$JLongArray$Type$,
84+
$JShortArray$NullableType$,
85+
$JShortArray$Type$;
6786
export 'src/jimplementer.dart';
68-
export 'src/jni.dart' hide ProtectedJniExtensions, StringMethodsForJni;
69-
export 'src/jobject.dart';
87+
export 'src/jni.dart'
88+
hide InternalJniExtension, ProtectedJniExtensions, StringMethodsForJni;
89+
export 'src/jobject.dart' hide $JObject$NullableType$, $JObject$Type$;
7090
export 'src/jreference.dart' hide ProtectedJReference;
7191
export 'src/jvalues.dart';
7292
export 'src/lang/lang.dart';
7393
export 'src/nio/nio.dart';
74-
export 'src/third_party/generated_bindings.dart'
75-
hide JniBindings, JniEnv, JniEnv1, JniExceptionDetails;
76-
export 'src/types.dart'
77-
hide
78-
JAccessible,
79-
JCallable,
80-
JConstructable,
81-
JObjType,
82-
JType,
83-
lowestCommonSuperType;
94+
export 'src/types.dart' hide JTypeBase, lowestCommonSuperType;
8495
export 'src/util/util.dart';

pkgs/jni/lib/jni_symbols.yaml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,57 @@ files:
33
'jni.dart':
44
'java.lang.Object':
55
name: JObject
6-
type_class: JObjectType
7-
nullable_type_class: JObjectNullableType
86
super_count: 0
97
'java.lang.String':
108
name: JString
11-
type_class: JStringType
12-
nullable_type_class: JStringNullableType
139
super_count: 1
1410
'java.lang.Number':
1511
name: JNumber
16-
type_class: JNumberType
17-
nullable_type_class: JNumberNullableType
1812
super_count: 1
1913
'java.lang.Byte':
2014
name: JByte
21-
type_class: JByteType
22-
nullable_type_class: JByteNullableType
2315
super_count: 2
2416
'java.lang.Short':
2517
name: JShort
26-
type_class: JShortType
27-
nullable_type_class: JShortNullableType
2818
super_count: 2
2919
'java.lang.Integer':
3020
name: JInteger
31-
type_class: JIntegerType
32-
nullable_type_class: JIntegerNullableType
3321
super_count: 2
3422
'java.lang.Long':
3523
name: JLong
36-
type_class: JLongType
37-
nullable_type_class: JLongNullableType
3824
super_count: 2
3925
'java.lang.Float':
4026
name: JFloat
41-
type_class: JFloatType
42-
nullable_type_class: JFloatNullableType
4327
super_count: 2
4428
'java.lang.Double':
4529
name: JDouble
46-
type_class: JDoubleType
47-
nullable_type_class: JDoubleNullableType
4830
super_count: 2
4931
'java.lang.Boolean':
5032
name: JBoolean
51-
type_class: JBooleanType
52-
nullable_type_class: JBooleanNullableType
5333
super_count: 1
5434
'java.lang.Character':
5535
name: JCharacter
56-
type_class: JCharacterType
57-
nullable_type_class: JCharacterNullableType
5836
super_count: 1
5937
'java.util.Set':
6038
name: JSet
61-
type_class: JSetType
62-
nullable_type_class: JSetNullableType
6339
super_count: 1
6440
type_params:
6541
E:
6642
'java.lang.Object': DECLARED
6743
'java.util.List':
6844
name: JList
69-
type_class: JListType
70-
nullable_type_class: JListNullableType
7145
super_count: 1
7246
type_params:
7347
E:
7448
'java.lang.Object': DECLARED
7549
'java.util.Iterator':
7650
name: JIterator
77-
type_class: JIteratorType
78-
nullable_type_class: JIteratorNullableType
7951
super_count: 1
8052
type_params:
8153
E:
8254
'java.lang.Object': DECLARED
8355
'java.util.Map':
8456
name: JMap
85-
type_class: JMapType
86-
nullable_type_class: JMapNullableType
8757
super_count: 1
8858
type_params:
8959
K:
@@ -92,11 +62,7 @@ files:
9262
'java.lang.Object': DECLARED
9363
'java.nio.Buffer':
9464
name: JBuffer
95-
type_class: JBufferType
96-
nullable_type_class: JBufferNullableType
9765
super_count: 1
9866
'java.nio.ByteBuffer':
9967
name: JByteBuffer
100-
type_class: JByteBufferType
101-
nullable_type_class: JByteBufferNullableType
10268
super_count: 2

pkgs/jni/lib/src/accessors.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extension JniResultMethods on JniResult {
7272
return pointer == nullptr ? jNullReference : JGlobalReference(pointer);
7373
}
7474

75-
T object<T extends JObject?>(JObjType<T> type) {
75+
T object<T extends JObject?>(JType<T> type) {
7676
return type.fromReference(reference);
7777
}
7878

pkgs/jni/lib/src/errors.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import 'dart:io';
66

7+
import 'package:meta/meta.dart' show internal;
8+
79
import 'third_party/generated_bindings.dart';
810

911
// TODO(#567): Add the fact that [JException] is now a [JObject] to the
@@ -37,6 +39,7 @@ sealed class JniError extends Error {
3739

3840
JniError(this.message);
3941

42+
@internal
4043
factory JniError.of(JniErrorCode status) {
4144
if (!_errors.containsKey(status)) {
4245
status = JniErrorCode.ERR;

0 commit comments

Comments
 (0)