Skip to content

Commit

Permalink
[jnigen] Refactor into Visitor pattern (#200)
Browse files Browse the repository at this point in the history
Introducing a top-level `Classes` class which includes all the classes we're trying to generate as yet another element, makes each step of the code generation pipeline, feel the same. For example exclusion of classes and methods are now both done via actually removing the objects instead of setting an `isIncluded` flag. 

This `Classes` object will go through a pipeline of:

1. `Excluder` – excludes the unwanted classes, the private ones and the ones specified by the user.
2. `Linker` – links up the objects together to create a graph.
3. `Renamer` – renames the classes, methods, fields, ... not to clash with other reserved names.
4. `DartGenerator` – generates and writes to files.
  • Loading branch information
HosseinYousefi authored Mar 16, 2023
1 parent aef53e2 commit 253254d
Show file tree
Hide file tree
Showing 47 changed files with 2,874 additions and 2,751 deletions.
2 changes: 2 additions & 0 deletions pkgs/jnigen/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ linter:
- prefer_final_locals
- prefer_const_declarations
- unawaited_futures
- prefer_const_constructors
- prefer_relative_imports

16 changes: 9 additions & 7 deletions pkgs/jnigen/example/in_app_java/lib/android_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
// ignore_for_file: overridden_fields
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_element
// ignore_for_file: unused_field
// ignore_for_file: unused_import
// ignore_for_file: unused_shown_name

import "dart:isolate" show ReceivePort;
import "dart:ffi" as ffi;
Expand All @@ -34,7 +36,6 @@ class AndroidUtils extends jni.JObject {

/// The type which includes information such as the signature of this class.
static const type = $AndroidUtilsType();

static final _showToast = jniLookup<
ffi.NativeFunction<
jni.JniResult Function(ffi.Pointer<ffi.Void>,
Expand Down Expand Up @@ -82,7 +83,6 @@ class Build extends jni.JObject {

/// The type which includes information such as the signature of this class.
static const type = $BuildType();

static final _get_BOARD =
jniLookup<ffi.NativeFunction<jni.JniResult Function()>>(
"get_Build__BOARD")
Expand Down Expand Up @@ -339,7 +339,7 @@ class Build extends jni.JObject {
const jni.JStringType().fromRef(_get_TYPE().object);

/// from: static public final java.lang.String UNKNOWN
static const UNKNOWN = "unknown";
static const UNKNOWN = r"""unknown""";

static final _get_USER =
jniLookup<ffi.NativeFunction<jni.JniResult Function()>>("get_Build__USER")
Expand All @@ -355,6 +355,7 @@ class Build extends jni.JObject {
.asFunction<jni.JniResult Function()>();

/// from: public void <init>()
/// The returned object must be deleted after use, by calling the `delete` method.
Build() : super.fromRef(_ctor().object);

static final _getSerial =
Expand Down Expand Up @@ -414,10 +415,7 @@ class HashMap<K extends jni.JObject, V extends jni.JObject>
extends jni.JObject {
late final jni.JObjType? _$type;
@override
jni.JObjType get $type => _$type ??= type(
$K,
$V,
);
jni.JObjType get $type => _$type ??= type($K, $V);

final jni.JObjType<K> $K;
final jni.JObjType<V> $V;
Expand Down Expand Up @@ -445,6 +443,7 @@ class HashMap<K extends jni.JObject, V extends jni.JObject>
.asFunction<jni.JniResult Function(int, double)>();

/// from: public void <init>(int i, float f)
/// The returned object must be deleted after use, by calling the `delete` method.
HashMap(this.$K, this.$V, int i, double f)
: super.fromRef(_ctor(i, f).object);

Expand All @@ -454,13 +453,15 @@ class HashMap<K extends jni.JObject, V extends jni.JObject>
.asFunction<jni.JniResult Function(int)>();

/// from: public void <init>(int i)
/// The returned object must be deleted after use, by calling the `delete` method.
HashMap.ctor1(this.$K, this.$V, int i) : super.fromRef(_ctor1(i).object);

static final _ctor2 =
jniLookup<ffi.NativeFunction<jni.JniResult Function()>>("HashMap__ctor2")
.asFunction<jni.JniResult Function()>();

/// from: public void <init>()
/// The returned object must be deleted after use, by calling the `delete` method.
HashMap.ctor2(this.$K, this.$V) : super.fromRef(_ctor2().object);

static final _ctor3 = jniLookup<
Expand All @@ -469,6 +470,7 @@ class HashMap<K extends jni.JObject, V extends jni.JObject>
.asFunction<jni.JniResult Function(ffi.Pointer<ffi.Void>)>();

/// from: public void <init>(java.util.Map map)
/// The returned object must be deleted after use, by calling the `delete` method.
HashMap.ctor3(this.$K, this.$V, jni.JObject map)
: super.fromRef(_ctor3(map.reference).object);

Expand Down
8 changes: 5 additions & 3 deletions pkgs/jnigen/example/kotlin_plugin/lib/kotlin_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
// ignore_for_file: overridden_fields
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_element
// ignore_for_file: unused_field
// ignore_for_file: unused_import
// ignore_for_file: unused_shown_name

import "dart:isolate" show ReceivePort;
import "dart:ffi" as ffi;
Expand All @@ -34,12 +36,12 @@ class Example extends jni.JObject {

/// The type which includes information such as the signature of this class.
static const type = $ExampleType();

static final _ctor =
jniLookup<ffi.NativeFunction<jni.JniResult Function()>>("Example__ctor")
.asFunction<jni.JniResult Function()>();

/// from: public void <init>()
/// The returned object must be deleted after use, by calling the `delete` method.
Example() : super.fromRef(_ctor().object);

static final _thinkBeforeAnswering = jniLookup<
Expand All @@ -54,8 +56,8 @@ class Example extends jni.JObject {
/// The returned object must be deleted after use, by calling the `delete` method.
Future<jni.JString> thinkBeforeAnswering() async {
final $p = ReceivePort();
final $c = jni.Jni.newPortContinuation($p);
_thinkBeforeAnswering(reference, $c).object;
final $c = jni.JObject.fromRef(jni.Jni.newPortContinuation($p));
_thinkBeforeAnswering(reference, $c.reference).object;
final $o = jni.JObjectPtr.fromAddress(await $p.first);
final $k = const jni.JStringType().getClass().reference;
if (jni.Jni.env.IsInstanceOf($o, $k) == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// ignore_for_file: overridden_fields
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_element
// ignore_for_file: unused_field
// ignore_for_file: unused_import
// ignore_for_file: unused_shown_name

import "dart:isolate" show ReceivePort;
import "dart:ffi" as ffi;
Expand All @@ -38,12 +40,12 @@ class Notifications extends jni.JObject {

/// The type which includes information such as the signature of this class.
static const type = $NotificationsType();

static final _ctor = jniLookup<ffi.NativeFunction<jni.JniResult Function()>>(
"Notifications__ctor")
.asFunction<jni.JniResult Function()>();

/// from: public void <init>()
/// The returned object must be deleted after use, by calling the `delete` method.
Notifications() : super.fromRef(_ctor().object);

static final _showNotification = jniLookup<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
// ignore_for_file: overridden_fields
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_element
// ignore_for_file: unused_field
// ignore_for_file: unused_import
// ignore_for_file: unused_shown_name

import "dart:isolate" show ReceivePort;
import "dart:ffi" as ffi;
import "package:jni/internal_helpers_for_jnigen.dart";
import "package:jni/jni.dart" as jni;

import "PDDocumentInformation.dart" as pddocumentinformation_;
import "../../../../_init.dart" show jniLookup;
import "../../../../_init.dart";

/// from: org.apache.pdfbox.pdmodel.PDDocument
///
Expand All @@ -54,12 +56,12 @@ class PDDocument extends jni.JObject {

/// The type which includes information such as the signature of this class.
static const type = $PDDocumentType();

static final _ctor = jniLookup<ffi.NativeFunction<jni.JniResult Function()>>(
"PDDocument__ctor")
.asFunction<jni.JniResult Function()>();

/// from: public void <init>()
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Creates an empty PDF document.
/// You need to add at least one page for the document to be valid.
Expand All @@ -72,6 +74,7 @@ class PDDocument extends jni.JObject {
.asFunction<jni.JniResult Function(ffi.Pointer<ffi.Void>)>();

/// from: public void <init>(org.apache.pdfbox.io.MemoryUsageSetting memUsageSetting)
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Creates an empty PDF document.
/// You need to add at least one page for the document to be valid.
Expand All @@ -86,6 +89,7 @@ class PDDocument extends jni.JObject {
.asFunction<jni.JniResult Function(ffi.Pointer<ffi.Void>)>();

/// from: public void <init>(org.apache.pdfbox.cos.COSDocument doc)
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Constructor that uses an existing document. The COSDocument that is passed in must be valid.
///@param doc The COSDocument that this document wraps.
Expand All @@ -101,6 +105,7 @@ class PDDocument extends jni.JObject {
ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Void>)>();

/// from: public void <init>(org.apache.pdfbox.cos.COSDocument doc, org.apache.pdfbox.io.RandomAccessRead source)
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Constructor that uses an existing document. The COSDocument that is passed in must be valid.
///@param doc The COSDocument that this document wraps.
Expand All @@ -119,6 +124,7 @@ class PDDocument extends jni.JObject {
ffi.Pointer<ffi.Void>)>();

/// from: public void <init>(org.apache.pdfbox.cos.COSDocument doc, org.apache.pdfbox.io.RandomAccessRead source, org.apache.pdfbox.pdmodel.encryption.AccessPermission permission)
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Constructor that uses an existing document. The COSDocument that is passed in must be valid.
///@param doc The COSDocument that this document wraps.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
// ignore_for_file: overridden_fields
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_element
// ignore_for_file: unused_field
// ignore_for_file: unused_import
// ignore_for_file: unused_shown_name

import "dart:isolate" show ReceivePort;
import "dart:ffi" as ffi;
import "package:jni/internal_helpers_for_jnigen.dart";
import "package:jni/jni.dart" as jni;

import "../../../../_init.dart" show jniLookup;
import "../../../../_init.dart";

/// from: org.apache.pdfbox.pdmodel.PDDocumentInformation
///
Expand All @@ -55,12 +57,12 @@ class PDDocumentInformation extends jni.JObject {

/// The type which includes information such as the signature of this class.
static const type = $PDDocumentInformationType();

static final _ctor = jniLookup<ffi.NativeFunction<jni.JniResult Function()>>(
"PDDocumentInformation__ctor")
.asFunction<jni.JniResult Function()>();

/// from: public void <init>()
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Default Constructor.
PDDocumentInformation() : super.fromRef(_ctor().object);
Expand All @@ -72,6 +74,7 @@ class PDDocumentInformation extends jni.JObject {
.asFunction<jni.JniResult Function(ffi.Pointer<ffi.Void>)>();

/// from: public void <init>(org.apache.pdfbox.cos.COSDictionary dic)
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Constructor that is used for a preexisting dictionary.
///@param dic The underlying dictionary.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
// ignore_for_file: overridden_fields
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_element
// ignore_for_file: unused_field
// ignore_for_file: unused_import
// ignore_for_file: unused_shown_name

import "dart:isolate" show ReceivePort;
import "dart:ffi" as ffi;
import "package:jni/internal_helpers_for_jnigen.dart";
import "package:jni/jni.dart" as jni;

import "../pdmodel/PDDocument.dart" as pddocument_;
import "../../../../_init.dart" show jniLookup;
import "../../../../_init.dart";

/// from: org.apache.pdfbox.text.PDFTextStripper
///
Expand All @@ -58,7 +60,6 @@ class PDFTextStripper extends jni.JObject {

/// The type which includes information such as the signature of this class.
static const type = $PDFTextStripperType();

static final _get_LINE_SEPARATOR = jniLookup<
ffi.NativeFunction<
jni.JniResult Function(
Expand Down Expand Up @@ -86,6 +87,14 @@ class PDFTextStripper extends jni.JObject {
jni.JObjectPtr,
)>();

static final _set_charactersByArticle = jniLookup<
ffi.NativeFunction<
jni.JThrowablePtr Function(
jni.JObjectPtr, ffi.Pointer<ffi.Void>)>>(
"set_PDFTextStripper__charactersByArticle")
.asFunction<
jni.JThrowablePtr Function(jni.JObjectPtr, ffi.Pointer<ffi.Void>)>();

/// from: protected java.util.ArrayList<java.util.List<org.apache.pdfbox.text.TextPosition>> charactersByArticle
/// The returned object must be deleted after use, by calling the `delete` method.
///
Expand All @@ -103,13 +112,6 @@ class PDFTextStripper extends jni.JObject {
/// Most PDFs won't have any beads, so charactersByArticle will contain a single entry.
jni.JObject get charactersByArticle => const jni.JObjectType()
.fromRef(_get_charactersByArticle(reference).object);
static final _set_charactersByArticle = jniLookup<
ffi.NativeFunction<
jni.JThrowablePtr Function(
jni.JObjectPtr, ffi.Pointer<ffi.Void>)>>(
"set_PDFTextStripper__charactersByArticle")
.asFunction<
jni.JThrowablePtr Function(jni.JObjectPtr, ffi.Pointer<ffi.Void>)>();

/// from: protected java.util.ArrayList<java.util.List<org.apache.pdfbox.text.TextPosition>> charactersByArticle
/// The returned object must be deleted after use, by calling the `delete` method.
Expand Down Expand Up @@ -139,17 +141,18 @@ class PDFTextStripper extends jni.JObject {
jni.JObjectPtr,
)>();

/// from: protected org.apache.pdfbox.pdmodel.PDDocument document
/// The returned object must be deleted after use, by calling the `delete` method.
pddocument_.PDDocument get document => const pddocument_.$PDDocumentType()
.fromRef(_get_document(reference).object);
static final _set_document = jniLookup<
ffi.NativeFunction<
jni.JThrowablePtr Function(jni.JObjectPtr,
ffi.Pointer<ffi.Void>)>>("set_PDFTextStripper__document")
.asFunction<
jni.JThrowablePtr Function(jni.JObjectPtr, ffi.Pointer<ffi.Void>)>();

/// from: protected org.apache.pdfbox.pdmodel.PDDocument document
/// The returned object must be deleted after use, by calling the `delete` method.
pddocument_.PDDocument get document => const pddocument_.$PDDocumentType()
.fromRef(_get_document(reference).object);

/// from: protected org.apache.pdfbox.pdmodel.PDDocument document
/// The returned object must be deleted after use, by calling the `delete` method.
set document(pddocument_.PDDocument value) =>
Expand All @@ -165,17 +168,18 @@ class PDFTextStripper extends jni.JObject {
jni.JObjectPtr,
)>();

/// from: protected java.io.Writer output
/// The returned object must be deleted after use, by calling the `delete` method.
jni.JObject get output =>
const jni.JObjectType().fromRef(_get_output(reference).object);
static final _set_output = jniLookup<
ffi.NativeFunction<
jni.JThrowablePtr Function(jni.JObjectPtr,
ffi.Pointer<ffi.Void>)>>("set_PDFTextStripper__output")
.asFunction<
jni.JThrowablePtr Function(jni.JObjectPtr, ffi.Pointer<ffi.Void>)>();

/// from: protected java.io.Writer output
/// The returned object must be deleted after use, by calling the `delete` method.
jni.JObject get output =>
const jni.JObjectType().fromRef(_get_output(reference).object);

/// from: protected java.io.Writer output
/// The returned object must be deleted after use, by calling the `delete` method.
set output(jni.JObject value) => _set_output(reference, value.reference);
Expand All @@ -185,6 +189,7 @@ class PDFTextStripper extends jni.JObject {
.asFunction<jni.JniResult Function()>();

/// from: public void <init>()
/// The returned object must be deleted after use, by calling the `delete` method.
///
/// Instantiate a new PDFTextStripper object.
///@throws IOException If there is an error loading the properties.
Expand Down
9 changes: 0 additions & 9 deletions pkgs/jnigen/lib/src/bindings/bindings.dart

This file was deleted.

Loading

0 comments on commit 253254d

Please sign in to comment.