Skip to content

Commit d8eef4d

Browse files
[jnigen] Use Dart 3 class modifiers in package:jni (#398)
1 parent ece9bdc commit d8eef4d

File tree

14 files changed

+47
-43
lines changed

14 files changed

+47
-43
lines changed

pkgs/jni/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
- **Breaking Change**: The default return `callType` of type parameter `int` for
1515
methods such as `JObject.callMethodByName<int>` is now Java's `long` instead
1616
of `int` to be consistent with the way arguments work.
17+
- **Breaking Change**: `JType` is now `sealed`.
18+
- **Breaking Change**: Primitive types and their type classes are now `final`.
19+
- **Breaking Change**: `JArray.filled` now uses the generated type class of the
20+
`fill` object and not its Java runtime type.
1721

1822
## 0.7.0
1923

pkgs/jni/lib/jni.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ export 'src/jvalues.dart' hide JValueArgs, toJValues;
6666
export 'src/types.dart';
6767
export 'src/jarray.dart';
6868
export 'src/jobject.dart';
69-
export 'src/jprimitives.dart';
7069
export 'src/jreference.dart' show JReferenceUseExtension;
7170

7271
export 'src/lang/lang.dart';

pkgs/jni/lib/src/jarray.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import 'package:jni/src/third_party/generated_bindings.dart';
1313

1414
import 'jni.dart';
1515
import 'jobject.dart';
16-
import 'jprimitives.dart';
1716
import 'types.dart';
1817

1918
final class JArrayType<T> extends JObjType<JArray<T>> {
@@ -92,10 +91,9 @@ class JArray<E> extends JObject {
9291
/// Creates a [JArray] of the given length with [fill] at each position.
9392
///
9493
/// The [length] must be a non-negative integer.
95-
/// The [fill] must be a non-null [JObject].
9694
static JArray<E> filled<E extends JObject>(int length, E fill) {
97-
assert(!fill.isNull, "fill must not be null.");
98-
final clazz = fill.getClass();
95+
RangeError.checkNotNegative(length);
96+
final clazz = fill.$type.getClass();
9997
final array = JArray<E>.fromRef(
10098
fill.$type as JObjType<E>,
10199
Jni.accessors

pkgs/jni/lib/src/jni.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ DynamicLibrary _loadDartJniLibrary({String? dir, String baseName = "dartjni"}) {
4343
}
4444

4545
/// Utilities to spawn and manage JNI.
46-
abstract class Jni {
46+
abstract final class Jni {
4747
static final DynamicLibrary _dylib = _loadDartJniLibrary(dir: _dylibDir);
4848
static final JniBindings _bindings = JniBindings(_dylib);
4949
static final _getJniEnvFn = _dylib.lookup<Void>('GetJniEnv');

pkgs/jni/lib/src/jprimitives.dart

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,92 @@
66
// lowercase.
77
// ignore_for_file: camel_case_types
88

9-
import 'types.dart';
9+
part of 'types.dart';
1010

11-
abstract class JPrimitive {}
11+
abstract final class JPrimitive {}
1212

13-
abstract class jbyte extends JPrimitive {
13+
abstract final class jbyte extends JPrimitive {
1414
static const type = jbyteType();
1515
}
1616

17-
class jbyteType extends JType<jbyte> {
17+
final class jbyteType extends JType<jbyte> {
1818
const jbyteType();
1919

2020
@override
2121
final signature = 'B';
2222
}
2323

24-
abstract class jboolean extends JPrimitive {
24+
abstract final class jboolean extends JPrimitive {
2525
static const type = jbooleanType();
2626
}
2727

28-
class jbooleanType extends JType<jboolean> {
28+
final class jbooleanType extends JType<jboolean> {
2929
const jbooleanType();
3030

3131
@override
3232
final signature = 'Z';
3333
}
3434

35-
abstract class jchar extends JPrimitive {
35+
abstract final class jchar extends JPrimitive {
3636
static const type = jcharType();
3737
}
3838

39-
class jcharType extends JType<jchar> {
39+
final class jcharType extends JType<jchar> {
4040
const jcharType();
4141

4242
@override
4343
final signature = 'C';
4444
}
4545

46-
abstract class jshort extends JPrimitive {
46+
abstract final class jshort extends JPrimitive {
4747
static const type = jshortType();
4848
}
4949

50-
class jshortType extends JType<jshort> {
50+
final class jshortType extends JType<jshort> {
5151
const jshortType();
5252

5353
@override
5454
final signature = 'S';
5555
}
5656

57-
abstract class jint extends JPrimitive {
57+
abstract final class jint extends JPrimitive {
5858
static const type = jintType();
5959
}
6060

61-
class jintType extends JType<jint> {
61+
final class jintType extends JType<jint> {
6262
const jintType();
6363

6464
@override
6565
final signature = 'I';
6666
}
6767

68-
abstract class jlong extends JPrimitive {
68+
abstract final class jlong extends JPrimitive {
6969
static const type = jlongType();
7070
}
7171

72-
class jlongType extends JType<jlong> {
72+
final class jlongType extends JType<jlong> {
7373
const jlongType();
7474

7575
@override
7676
final signature = 'J';
7777
}
7878

79-
abstract class jfloat extends JPrimitive {
79+
abstract final class jfloat extends JPrimitive {
8080
static const type = jfloatType();
8181
}
8282

83-
class jfloatType extends JType<jfloat> {
83+
final class jfloatType extends JType<jfloat> {
8484
const jfloatType();
8585

8686
@override
8787
final signature = 'F';
8888
}
8989

90-
abstract class jdouble extends JPrimitive {
90+
abstract final class jdouble extends JPrimitive {
9191
static const type = jdoubleType();
9292
}
9393

94-
class jdoubleType extends JType<jdouble> {
94+
final class jdoubleType extends JType<jdouble> {
9595
const jdoubleType();
9696

9797
@override

pkgs/jni/lib/src/jvalues.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,35 @@ Pointer<JValue> toJValues(List<dynamic> args, {Allocator allocator = calloc}) {
6767

6868
/// Use this class as wrapper to convert an integer
6969
/// to Java `int` in jvalues method.
70-
class JValueInt {
70+
final class JValueInt {
7171
int value;
7272
JValueInt(this.value);
7373
}
7474

7575
/// Use this class as wrapper to convert an integer
7676
/// to Java `short` in jvalues method.
77-
class JValueShort {
77+
final class JValueShort {
7878
int value;
7979
JValueShort(this.value);
8080
}
8181

8282
/// Use this class as wrapper to convert an integer
8383
/// to Java `byte` in jvalues method.
84-
class JValueByte {
84+
final class JValueByte {
8585
int value;
8686
JValueByte(this.value);
8787
}
8888

8989
/// Use this class as wrapper to convert an double
9090
/// to Java `float` in jvalues method.
91-
class JValueFloat {
91+
final class JValueFloat {
9292
double value;
9393
JValueFloat(this.value);
9494
}
9595

9696
/// Use this class as wrapper to convert an integer
9797
/// to Java `char` in jvalues method.
98-
class JValueChar {
98+
final class JValueChar {
9999
int value;
100100
JValueChar(this.value);
101101
JValueChar.fromString(String s) : value = 0 {
@@ -115,7 +115,7 @@ class JValueChar {
115115
///
116116
/// Returned value is allocated using provided allocator.
117117
/// But default allocator may be used for string conversions.
118-
class JValueArgs {
118+
final class JValueArgs {
119119
late Pointer<JValue> values;
120120
final List<JObjectPtr> createdRefs = [];
121121

pkgs/jni/lib/src/nio/jbyte_buffer.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'dart:typed_data';
88
import '../accessors.dart';
99
import '../jarray.dart';
1010
import '../jni.dart';
11-
import '../jprimitives.dart';
1211
import '../jreference.dart';
1312
import '../jvalues.dart';
1413
import '../third_party/generated_bindings.dart';

pkgs/jni/lib/src/types.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import 'dart:ffi';
77
import 'jni.dart';
88
import 'jobject.dart';
99

10-
abstract class JType<T> {
10+
part 'jprimitives.dart';
11+
12+
sealed class JType<T> {
1113
const JType();
1214

1315
String get signature;

pkgs/jni/test/jarray_test.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,8 @@ void run({required TestRunnerCallback testRunner}) {
289289
final string = "abc".toJString()..releasedBy(arena);
290290
final array = JArray.filled(3, string)..releasedBy(arena);
291291
expect(
292-
() {
293-
final _ = JArray.filled(3, JString.fromRef(nullptr))
294-
..releasedBy(arena);
295-
},
296-
throwsA(isA<AssertionError>()),
292+
() => JArray.filled(-3, JString.fromRef(nullptr)),
293+
throwsA(isA<RangeError>()),
297294
);
298295
expect(array.length, 3);
299296
expect(array[0].toDartString(releaseOriginal: true), "abc");

pkgs/jnigen/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.8.0-wip
2+
3+
- **Breaking Change**: The generated impl class for interfaces is now an
4+
`interface`.
5+
16
## 0.7.0
27

38
- **Breaking Change** ([#387](https://github.com/dart-lang/jnigen/issues/387)):

pkgs/jnigen/lib/src/bindings/dart_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ class $name$typeParamsDef extends $superName {
520520
'}',
521521
);
522522
s.write('''
523-
abstract class $implClassName$typeParamsDef {
523+
abstract interface class $implClassName$typeParamsDef {
524524
factory $implClassName(
525525
$abstractFactoryArgs
526526
) = _$implClassName;

pkgs/jnigen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: jnigen
66
description: A Dart bindings generator for Java and Kotlin that uses JNI under the hood to interop with Java virtual machine.
7-
version: 0.7.0
7+
version: 0.8.0-wip
88
repository: https://github.com/dart-lang/jnigen/tree/main/jnigen
99

1010
environment:

pkgs/jnigen/test/simple_package_test/c_based/dart_bindings/simple_package.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,7 +3446,7 @@ class MyInterface<$T extends jni.JObject> extends jni.JObject {
34463446
static Map<int, $MyInterfaceImpl> get $impls => _$impls;
34473447
}
34483448

3449-
abstract class $MyInterfaceImpl<$T extends jni.JObject> {
3449+
abstract interface class $MyInterfaceImpl<$T extends jni.JObject> {
34503450
factory $MyInterfaceImpl({
34513451
required jni.JObjType<$T> T,
34523452
required void Function(jni.JString s) voidCallback,
@@ -3743,7 +3743,7 @@ class MyRunnable extends jni.JObject {
37433743
}
37443744
}
37453745

3746-
abstract class $MyRunnableImpl {
3746+
abstract interface class $MyRunnableImpl {
37473747
factory $MyRunnableImpl({
37483748
required void Function() run,
37493749
}) = _$MyRunnableImpl;

pkgs/jnigen/test/simple_package_test/dart_only/dart_bindings/simple_package.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,7 +3255,7 @@ class MyInterface<$T extends jni.JObject> extends jni.JObject {
32553255
static Map<int, $MyInterfaceImpl> get $impls => _$impls;
32563256
}
32573257

3258-
abstract class $MyInterfaceImpl<$T extends jni.JObject> {
3258+
abstract interface class $MyInterfaceImpl<$T extends jni.JObject> {
32593259
factory $MyInterfaceImpl({
32603260
required jni.JObjType<$T> T,
32613261
required void Function(jni.JString s) voidCallback,
@@ -3550,7 +3550,7 @@ class MyRunnable extends jni.JObject {
35503550
}
35513551
}
35523552

3553-
abstract class $MyRunnableImpl {
3553+
abstract interface class $MyRunnableImpl {
35543554
factory $MyRunnableImpl({
35553555
required void Function() run,
35563556
}) = _$MyRunnableImpl;

0 commit comments

Comments
 (0)