Skip to content

Commit acb973c

Browse files
[jnigen] User-defined visitors (#1755)
Config now accepts a list of user-defined visitors. For now the only the they do is excluding classes, methods, and fields.
1 parent 3aba894 commit acb973c

7 files changed

Lines changed: 218 additions & 22 deletions

File tree

‎pkgs/jnigen/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- **Breaking Change**([#1644](https://github.com/dart-lang/native/issues/1644)):
44
Generate null-safe Dart bindings for Java and Kotlin.
5+
- Add a simple AST and update `excluder.dart` to support user-defined exclusions for classes, methods, and fields.
6+
57

68
## 0.12.2
79

‎pkgs/jnigen/lib/src/bindings/excluder.dart‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Excluder extends Visitor<Classes, void> {
3535
void visit(Classes node) {
3636
node.decls.removeWhere((_, classDecl) {
3737
final excluded = classDecl.isPrivate ||
38+
classDecl.isExcluded ||
3839
!(config.exclude?.classes?.included(classDecl) ?? true);
3940
if (excluded) {
4041
log.fine('Excluded class ${classDecl.binaryName}');
@@ -61,13 +62,17 @@ class _ClassExcluder extends Visitor<ClassDecl, void> {
6162
@override
6263
void visit(ClassDecl node) {
6364
node.methods = node.methods.where((method) {
65+
final isExcluded = method.isExcluded;
6466
final isPrivate = method.isPrivate;
6567
final isAbstractCtor = method.isConstructor && node.isAbstract;
6668
final isBridgeMethod = method.isSynthetic && method.isBridge;
6769
final isExcludedInConfig =
6870
config.exclude?.methods?.included(node, method) ?? false;
69-
final excluded =
70-
isPrivate || isAbstractCtor || isBridgeMethod || isExcludedInConfig;
71+
final excluded = isPrivate ||
72+
isAbstractCtor ||
73+
isBridgeMethod ||
74+
isExcludedInConfig ||
75+
isExcluded;
7176
if (excluded) {
7277
log.fine('Excluded method ${node.binaryName}#${method.name}');
7378
}
@@ -80,8 +85,9 @@ class _ClassExcluder extends Visitor<ClassDecl, void> {
8085
return !excluded;
8186
}).toList();
8287
node.fields = node.fields.where((field) {
83-
final excluded = field.isPrivate &&
84-
(config.exclude?.fields?.included(node, field) ?? true);
88+
final excluded = field.isExcluded ||
89+
(field.isPrivate &&
90+
(config.exclude?.fields?.included(node, field) ?? true));
8591
if (excluded) {
8692
log.fine('Excluded field ${node.binaryName}#${field.name}');
8793
}

‎pkgs/jnigen/lib/src/config/config_types.dart‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:pub_semver/pub_semver.dart';
1010
import 'package:yaml/yaml.dart';
1111

1212
import '../elements/elements.dart';
13+
import '../elements/j_elements.dart' as j_ast;
1314
import '../logging/logging.dart';
1415
import '../util/find_package.dart';
1516
import 'config_exception.dart';
@@ -265,24 +266,24 @@ void _validateClassName(String className) {
265266

266267
/// Configuration for jnigen binding generation.
267268
class Config {
268-
Config({
269-
required this.outputConfig,
270-
required this.classes,
271-
this.experiments,
272-
this.exclude,
273-
this.sourcePath,
274-
this.classPath,
275-
this.preamble,
276-
this.customClassBody,
277-
this.androidSdkConfig,
278-
this.mavenDownloads,
279-
this.summarizerOptions,
280-
this.nonNullAnnotations,
281-
this.nullableAnnotations,
282-
this.logLevel = Level.INFO,
283-
this.dumpJsonTo,
284-
this.imports,
285-
}) {
269+
Config(
270+
{required this.outputConfig,
271+
required this.classes,
272+
this.experiments,
273+
this.exclude,
274+
this.sourcePath,
275+
this.classPath,
276+
this.preamble,
277+
this.customClassBody,
278+
this.androidSdkConfig,
279+
this.mavenDownloads,
280+
this.summarizerOptions,
281+
this.nonNullAnnotations,
282+
this.nullableAnnotations,
283+
this.logLevel = Level.INFO,
284+
this.dumpJsonTo,
285+
this.imports,
286+
this.visitors}) {
286287
for (final className in classes) {
287288
_validateClassName(className);
288289
}
@@ -349,6 +350,9 @@ class Config {
349350
/// Used for testing package:jnigen.
350351
final Map<String, String>? customClassBody;
351352

353+
// User custom visitors.
354+
List<j_ast.Visitor>? visitors;
355+
352356
Future<void> importClasses() async {
353357
importedClasses = {};
354358
for (final import in [

‎pkgs/jnigen/lib/src/elements/elements.dart‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Classes implements Element<Classes> {
5959
@JsonSerializable(createToJson: false)
6060
class ClassDecl with ClassMember, Annotated implements Element<ClassDecl> {
6161
ClassDecl({
62+
this.isExcluded = false,
6263
this.annotations,
6364
this.javadoc,
6465
required this.declKind,
@@ -77,6 +78,8 @@ class ClassDecl with ClassMember, Annotated implements Element<ClassDecl> {
7778
this.kotlinPackage,
7879
});
7980

81+
bool isExcluded;
82+
8083
@override
8184
final Set<String> modifiers;
8285

@@ -602,6 +605,7 @@ mixin ClassMember {
602605
@JsonSerializable(createToJson: false)
603606
class Method with ClassMember, Annotated implements Element<Method> {
604607
Method({
608+
this.isExcluded = false,
605609
this.annotations,
606610
this.javadoc,
607611
this.modifiers = const {},
@@ -612,6 +616,8 @@ class Method with ClassMember, Annotated implements Element<Method> {
612616
required this.returnType,
613617
});
614618

619+
bool isExcluded;
620+
615621
@override
616622
final String name;
617623
@override
@@ -704,6 +710,7 @@ class Param with Annotated implements Element<Param> {
704710
@JsonSerializable(createToJson: false)
705711
class Field with ClassMember, Annotated implements Element<Field> {
706712
Field({
713+
this.isExcluded = false,
707714
this.annotations,
708715
this.javadoc,
709716
this.modifiers = const {},
@@ -712,6 +719,8 @@ class Field with ClassMember, Annotated implements Element<Field> {
712719
this.defaultValue,
713720
});
714721

722+
bool isExcluded;
723+
715724
@override
716725
final String name;
717726
@override
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'elements.dart' as ast;
6+
7+
abstract class Element {
8+
void accept(Visitor visitor);
9+
}
10+
11+
abstract class Visitor {
12+
void visitClass(ClassDecl c) {}
13+
void visitMethod(Method method) {}
14+
void visitField(Field field) {}
15+
}
16+
17+
class Classes implements Element {
18+
Classes(this._classes);
19+
final ast.Classes _classes;
20+
21+
@override
22+
void accept(Visitor visitor) {
23+
for (final value in _classes.decls.values) {
24+
final classDecl = ClassDecl(value);
25+
classDecl.accept(visitor);
26+
}
27+
}
28+
29+
void let(void Function(dynamic userClasses) param0) {}
30+
}
31+
32+
class ClassDecl implements Element {
33+
ClassDecl(this._classDecl) : binaryName = _classDecl.binaryName;
34+
final ast.ClassDecl _classDecl;
35+
36+
// Ex: com.x.Foo.
37+
final String binaryName;
38+
39+
bool get isExcluded => _classDecl.isExcluded;
40+
set isExcluded(bool value) => _classDecl.isExcluded = value;
41+
42+
@override
43+
void accept(Visitor visitor) {
44+
visitor.visitClass(this);
45+
if (_classDecl.isExcluded) return;
46+
for (final method in _classDecl.methods) {
47+
Method(method).accept(visitor);
48+
}
49+
for (var field in _classDecl.fields) {
50+
Field(field).accept(visitor);
51+
}
52+
}
53+
}
54+
55+
class Method implements Element {
56+
Method(this._method);
57+
58+
final ast.Method _method;
59+
60+
String get name => _method.name;
61+
62+
bool get isExcluded => _method.isExcluded;
63+
set isExcluded(bool value) => _method.isExcluded = value;
64+
65+
@override
66+
void accept(Visitor visitor) {
67+
visitor.visitMethod(this);
68+
}
69+
}
70+
71+
class Field implements Element {
72+
Field(this._field);
73+
74+
final ast.Field _field;
75+
76+
String get name => _field.name;
77+
78+
bool get isExcluded => _field.isExcluded;
79+
set isExcluded(bool value) => _field.isExcluded = value;
80+
81+
@override
82+
void accept(Visitor visitor) {
83+
visitor.visitField(this);
84+
}
85+
}

‎pkgs/jnigen/lib/src/generate_bindings.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'bindings/linker.dart';
1313
import 'bindings/renamer.dart';
1414
import 'config/config.dart';
1515
import 'elements/elements.dart';
16+
import 'elements/j_elements.dart' as j_ast;
1617
import 'logging/logging.dart';
1718
import 'summary/summary.dart';
1819
import 'tools/tools.dart';
@@ -38,6 +39,9 @@ Future<void> generateJniBindings(Config config) async {
3839
log.fatal(e.message);
3940
}
4041

42+
final userClasses = j_ast.Classes(classes);
43+
config.visitors?.forEach(userClasses.accept);
44+
4145
classes.accept(Excluder(config));
4246
classes.accept(KotlinProcessor());
4347
await classes.accept(Linker(config));
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:jnigen/src/elements/elements.dart' as ast;
6+
import 'package:jnigen/src/elements/j_elements.dart';
7+
import 'package:test/test.dart';
8+
9+
extension on Iterable<ast.Method> {
10+
List<bool> get isExcludedValues => map((c) => c.isExcluded).toList();
11+
}
12+
13+
extension on Iterable<ast.Field> {
14+
List<bool> get isExcludedValues => map((c) => c.isExcluded).toList();
15+
}
16+
17+
// This is customizable by the user
18+
class UserExcluder extends Visitor {
19+
@override
20+
void visitClass(ClassDecl c) {
21+
if (c.binaryName.contains('y')) {
22+
c.isExcluded = true;
23+
}
24+
}
25+
26+
@override
27+
void visitMethod(Method method) {
28+
if (method.name == 'Bar') {
29+
method.isExcluded = true;
30+
}
31+
}
32+
33+
@override
34+
void visitField(Field field) {
35+
if (field.name == 'Bar') {
36+
field.isExcluded = true;
37+
}
38+
}
39+
}
40+
41+
void main() {
42+
test('Exclude something using the user excluder, Simple AST', () async {
43+
final classes = ast.Classes({
44+
'Foo': ast.ClassDecl(
45+
binaryName: 'Foo',
46+
declKind: ast.DeclKind.classKind,
47+
superclass: ast.TypeUsage.object,
48+
methods: [
49+
ast.Method(name: 'foo', returnType: ast.TypeUsage.object),
50+
ast.Method(name: 'Bar', returnType: ast.TypeUsage.object),
51+
ast.Method(name: 'foo1', returnType: ast.TypeUsage.object),
52+
ast.Method(name: 'Bar', returnType: ast.TypeUsage.object),
53+
],
54+
fields: [
55+
ast.Field(name: 'foo', type: ast.TypeUsage.object),
56+
ast.Field(name: 'Bar', type: ast.TypeUsage.object),
57+
ast.Field(name: 'foo1', type: ast.TypeUsage.object),
58+
ast.Field(name: 'Bar', type: ast.TypeUsage.object),
59+
],
60+
),
61+
'y.Foo': ast.ClassDecl(
62+
binaryName: 'y.Foo',
63+
declKind: ast.DeclKind.classKind,
64+
superclass: ast.TypeUsage.object,
65+
methods: [
66+
ast.Method(name: 'foo', returnType: ast.TypeUsage.object),
67+
ast.Method(name: 'Bar', returnType: ast.TypeUsage.object),
68+
],
69+
fields: [
70+
ast.Field(name: 'foo', type: ast.TypeUsage.object),
71+
ast.Field(name: 'Bar', type: ast.TypeUsage.object),
72+
]),
73+
});
74+
75+
final simpleClasses = Classes(classes);
76+
simpleClasses.accept(UserExcluder());
77+
78+
expect(classes.decls['y.Foo']?.isExcluded, true);
79+
expect(classes.decls['Foo']?.isExcluded, false);
80+
81+
expect(classes.decls['Foo']?.fields.isExcludedValues,
82+
[false, true, false, true]);
83+
expect(classes.decls['Foo']?.methods.isExcludedValues,
84+
[false, true, false, true]);
85+
});
86+
}

0 commit comments

Comments
 (0)