Skip to content

Commit

Permalink
[jnigen] Use Dart 3 class modifiers in package:jni (dart-archive/jn…
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi authored Sep 20, 2023
1 parent 9fafeac commit 266ab71
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 43 deletions.
4 changes: 4 additions & 0 deletions pkgs/jni/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
- **Breaking Change**: The default return `callType` of type parameter `int` for
methods such as `JObject.callMethodByName<int>` is now Java's `long` instead
of `int` to be consistent with the way arguments work.
- **Breaking Change**: `JType` is now `sealed`.
- **Breaking Change**: Primitive types and their type classes are now `final`.
- **Breaking Change**: `JArray.filled` now uses the generated type class of the
`fill` object and not its Java runtime type.

## 0.7.0

Expand Down
1 change: 0 additions & 1 deletion pkgs/jni/lib/jni.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export 'src/jvalues.dart' hide JValueArgs, toJValues;
export 'src/types.dart';
export 'src/jarray.dart';
export 'src/jobject.dart';
export 'src/jprimitives.dart';
export 'src/jreference.dart' show JReferenceUseExtension;

export 'src/lang/lang.dart';
Expand Down
6 changes: 2 additions & 4 deletions pkgs/jni/lib/src/jarray.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:jni/src/third_party/generated_bindings.dart';

import 'jni.dart';
import 'jobject.dart';
import 'jprimitives.dart';
import 'types.dart';

final class JArrayType<T> extends JObjType<JArray<T>> {
Expand Down Expand Up @@ -92,10 +91,9 @@ class JArray<E> extends JObject {
/// Creates a [JArray] of the given length with [fill] at each position.
///
/// The [length] must be a non-negative integer.
/// The [fill] must be a non-null [JObject].
static JArray<E> filled<E extends JObject>(int length, E fill) {
assert(!fill.isNull, "fill must not be null.");
final clazz = fill.getClass();
RangeError.checkNotNegative(length);
final clazz = fill.$type.getClass();
final array = JArray<E>.fromRef(
fill.$type as JObjType<E>,
Jni.accessors
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jni/lib/src/jni.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ DynamicLibrary _loadDartJniLibrary({String? dir, String baseName = "dartjni"}) {
}

/// Utilities to spawn and manage JNI.
abstract class Jni {
abstract final class Jni {
static final DynamicLibrary _dylib = _loadDartJniLibrary(dir: _dylibDir);
static final JniBindings _bindings = JniBindings(_dylib);
static final _getJniEnvFn = _dylib.lookup<Void>('GetJniEnv');
Expand Down
36 changes: 18 additions & 18 deletions pkgs/jni/lib/src/jprimitives.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,92 @@
// lowercase.
// ignore_for_file: camel_case_types

import 'types.dart';
part of 'types.dart';

abstract class JPrimitive {}
abstract final class JPrimitive {}

abstract class jbyte extends JPrimitive {
abstract final class jbyte extends JPrimitive {
static const type = jbyteType();
}

class jbyteType extends JType<jbyte> {
final class jbyteType extends JType<jbyte> {
const jbyteType();

@override
final signature = 'B';
}

abstract class jboolean extends JPrimitive {
abstract final class jboolean extends JPrimitive {
static const type = jbooleanType();
}

class jbooleanType extends JType<jboolean> {
final class jbooleanType extends JType<jboolean> {
const jbooleanType();

@override
final signature = 'Z';
}

abstract class jchar extends JPrimitive {
abstract final class jchar extends JPrimitive {
static const type = jcharType();
}

class jcharType extends JType<jchar> {
final class jcharType extends JType<jchar> {
const jcharType();

@override
final signature = 'C';
}

abstract class jshort extends JPrimitive {
abstract final class jshort extends JPrimitive {
static const type = jshortType();
}

class jshortType extends JType<jshort> {
final class jshortType extends JType<jshort> {
const jshortType();

@override
final signature = 'S';
}

abstract class jint extends JPrimitive {
abstract final class jint extends JPrimitive {
static const type = jintType();
}

class jintType extends JType<jint> {
final class jintType extends JType<jint> {
const jintType();

@override
final signature = 'I';
}

abstract class jlong extends JPrimitive {
abstract final class jlong extends JPrimitive {
static const type = jlongType();
}

class jlongType extends JType<jlong> {
final class jlongType extends JType<jlong> {
const jlongType();

@override
final signature = 'J';
}

abstract class jfloat extends JPrimitive {
abstract final class jfloat extends JPrimitive {
static const type = jfloatType();
}

class jfloatType extends JType<jfloat> {
final class jfloatType extends JType<jfloat> {
const jfloatType();

@override
final signature = 'F';
}

abstract class jdouble extends JPrimitive {
abstract final class jdouble extends JPrimitive {
static const type = jdoubleType();
}

class jdoubleType extends JType<jdouble> {
final class jdoubleType extends JType<jdouble> {
const jdoubleType();

@override
Expand Down
12 changes: 6 additions & 6 deletions pkgs/jni/lib/src/jvalues.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,35 @@ Pointer<JValue> toJValues(List<dynamic> args, {Allocator allocator = calloc}) {

/// Use this class as wrapper to convert an integer
/// to Java `int` in jvalues method.
class JValueInt {
final class JValueInt {
int value;
JValueInt(this.value);
}

/// Use this class as wrapper to convert an integer
/// to Java `short` in jvalues method.
class JValueShort {
final class JValueShort {
int value;
JValueShort(this.value);
}

/// Use this class as wrapper to convert an integer
/// to Java `byte` in jvalues method.
class JValueByte {
final class JValueByte {
int value;
JValueByte(this.value);
}

/// Use this class as wrapper to convert an double
/// to Java `float` in jvalues method.
class JValueFloat {
final class JValueFloat {
double value;
JValueFloat(this.value);
}

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

Expand Down
1 change: 0 additions & 1 deletion pkgs/jni/lib/src/nio/jbyte_buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'dart:typed_data';
import '../accessors.dart';
import '../jarray.dart';
import '../jni.dart';
import '../jprimitives.dart';
import '../jreference.dart';
import '../jvalues.dart';
import '../third_party/generated_bindings.dart';
Expand Down
4 changes: 3 additions & 1 deletion pkgs/jni/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'dart:ffi';
import 'jni.dart';
import 'jobject.dart';

abstract class JType<T> {
part 'jprimitives.dart';

sealed class JType<T> {
const JType();

String get signature;
Expand Down
7 changes: 2 additions & 5 deletions pkgs/jni/test/jarray_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,8 @@ void run({required TestRunnerCallback testRunner}) {
final string = "abc".toJString()..releasedBy(arena);
final array = JArray.filled(3, string)..releasedBy(arena);
expect(
() {
final _ = JArray.filled(3, JString.fromRef(nullptr))
..releasedBy(arena);
},
throwsA(isA<AssertionError>()),
() => JArray.filled(-3, JString.fromRef(nullptr)),
throwsA(isA<RangeError>()),
);
expect(array.length, 3);
expect(array[0].toDartString(releaseOriginal: true), "abc");
Expand Down
5 changes: 5 additions & 0 deletions pkgs/jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.8.0-wip

- **Breaking Change**: The generated impl class for interfaces is now an
`interface`.

## 0.7.0

- **Breaking Change** ([#387](https://github.com/dart-lang/jnigen/issues/387)):
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jnigen/lib/src/bindings/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class $name$typeParamsDef extends $superName {
'}',
);
s.write('''
abstract class $implClassName$typeParamsDef {
abstract interface class $implClassName$typeParamsDef {
factory $implClassName(
$abstractFactoryArgs
) = _$implClassName;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/jnigen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3446,7 +3446,7 @@ class MyInterface<$T extends jni.JObject> extends jni.JObject {
static Map<int, $MyInterfaceImpl> get $impls => _$impls;
}

abstract class $MyInterfaceImpl<$T extends jni.JObject> {
abstract interface class $MyInterfaceImpl<$T extends jni.JObject> {
factory $MyInterfaceImpl({
required jni.JObjType<$T> T,
required void Function(jni.JString s) voidCallback,
Expand Down Expand Up @@ -3743,7 +3743,7 @@ class MyRunnable extends jni.JObject {
}
}

abstract class $MyRunnableImpl {
abstract interface class $MyRunnableImpl {
factory $MyRunnableImpl({
required void Function() run,
}) = _$MyRunnableImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3255,7 +3255,7 @@ class MyInterface<$T extends jni.JObject> extends jni.JObject {
static Map<int, $MyInterfaceImpl> get $impls => _$impls;
}

abstract class $MyInterfaceImpl<$T extends jni.JObject> {
abstract interface class $MyInterfaceImpl<$T extends jni.JObject> {
factory $MyInterfaceImpl({
required jni.JObjType<$T> T,
required void Function(jni.JString s) voidCallback,
Expand Down Expand Up @@ -3550,7 +3550,7 @@ class MyRunnable extends jni.JObject {
}
}

abstract class $MyRunnableImpl {
abstract interface class $MyRunnableImpl {
factory $MyRunnableImpl({
required void Function() run,
}) = _$MyRunnableImpl;
Expand Down

0 comments on commit 266ab71

Please sign in to comment.