-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
1 parent
3aba894
commit acb973c
Showing
7 changed files
with
218 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'elements.dart' as ast; | ||
|
||
abstract class Element { | ||
void accept(Visitor visitor); | ||
} | ||
|
||
abstract class Visitor { | ||
void visitClass(ClassDecl c) {} | ||
void visitMethod(Method method) {} | ||
void visitField(Field field) {} | ||
} | ||
|
||
class Classes implements Element { | ||
Classes(this._classes); | ||
final ast.Classes _classes; | ||
|
||
@override | ||
void accept(Visitor visitor) { | ||
for (final value in _classes.decls.values) { | ||
final classDecl = ClassDecl(value); | ||
classDecl.accept(visitor); | ||
} | ||
} | ||
|
||
void let(void Function(dynamic userClasses) param0) {} | ||
} | ||
|
||
class ClassDecl implements Element { | ||
ClassDecl(this._classDecl) : binaryName = _classDecl.binaryName; | ||
final ast.ClassDecl _classDecl; | ||
|
||
// Ex: com.x.Foo. | ||
final String binaryName; | ||
|
||
bool get isExcluded => _classDecl.isExcluded; | ||
set isExcluded(bool value) => _classDecl.isExcluded = value; | ||
|
||
@override | ||
void accept(Visitor visitor) { | ||
visitor.visitClass(this); | ||
if (_classDecl.isExcluded) return; | ||
for (final method in _classDecl.methods) { | ||
Method(method).accept(visitor); | ||
} | ||
for (var field in _classDecl.fields) { | ||
Field(field).accept(visitor); | ||
} | ||
} | ||
} | ||
|
||
class Method implements Element { | ||
Method(this._method); | ||
|
||
final ast.Method _method; | ||
|
||
String get name => _method.name; | ||
|
||
bool get isExcluded => _method.isExcluded; | ||
set isExcluded(bool value) => _method.isExcluded = value; | ||
|
||
@override | ||
void accept(Visitor visitor) { | ||
visitor.visitMethod(this); | ||
} | ||
} | ||
|
||
class Field implements Element { | ||
Field(this._field); | ||
|
||
final ast.Field _field; | ||
|
||
String get name => _field.name; | ||
|
||
bool get isExcluded => _field.isExcluded; | ||
set isExcluded(bool value) => _field.isExcluded = value; | ||
|
||
@override | ||
void accept(Visitor visitor) { | ||
visitor.visitField(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:jnigen/src/elements/elements.dart' as ast; | ||
import 'package:jnigen/src/elements/j_elements.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
extension on Iterable<ast.Method> { | ||
List<bool> get isExcludedValues => map((c) => c.isExcluded).toList(); | ||
} | ||
|
||
extension on Iterable<ast.Field> { | ||
List<bool> get isExcludedValues => map((c) => c.isExcluded).toList(); | ||
} | ||
|
||
// This is customizable by the user | ||
class UserExcluder extends Visitor { | ||
@override | ||
void visitClass(ClassDecl c) { | ||
if (c.binaryName.contains('y')) { | ||
c.isExcluded = true; | ||
} | ||
} | ||
|
||
@override | ||
void visitMethod(Method method) { | ||
if (method.name == 'Bar') { | ||
method.isExcluded = true; | ||
} | ||
} | ||
|
||
@override | ||
void visitField(Field field) { | ||
if (field.name == 'Bar') { | ||
field.isExcluded = true; | ||
} | ||
} | ||
} | ||
|
||
void main() { | ||
test('Exclude something using the user excluder, Simple AST', () async { | ||
final classes = ast.Classes({ | ||
'Foo': ast.ClassDecl( | ||
binaryName: 'Foo', | ||
declKind: ast.DeclKind.classKind, | ||
superclass: ast.TypeUsage.object, | ||
methods: [ | ||
ast.Method(name: 'foo', returnType: ast.TypeUsage.object), | ||
ast.Method(name: 'Bar', returnType: ast.TypeUsage.object), | ||
ast.Method(name: 'foo1', returnType: ast.TypeUsage.object), | ||
ast.Method(name: 'Bar', returnType: ast.TypeUsage.object), | ||
], | ||
fields: [ | ||
ast.Field(name: 'foo', type: ast.TypeUsage.object), | ||
ast.Field(name: 'Bar', type: ast.TypeUsage.object), | ||
ast.Field(name: 'foo1', type: ast.TypeUsage.object), | ||
ast.Field(name: 'Bar', type: ast.TypeUsage.object), | ||
], | ||
), | ||
'y.Foo': ast.ClassDecl( | ||
binaryName: 'y.Foo', | ||
declKind: ast.DeclKind.classKind, | ||
superclass: ast.TypeUsage.object, | ||
methods: [ | ||
ast.Method(name: 'foo', returnType: ast.TypeUsage.object), | ||
ast.Method(name: 'Bar', returnType: ast.TypeUsage.object), | ||
], | ||
fields: [ | ||
ast.Field(name: 'foo', type: ast.TypeUsage.object), | ||
ast.Field(name: 'Bar', type: ast.TypeUsage.object), | ||
]), | ||
}); | ||
|
||
final simpleClasses = Classes(classes); | ||
simpleClasses.accept(UserExcluder()); | ||
|
||
expect(classes.decls['y.Foo']?.isExcluded, true); | ||
expect(classes.decls['Foo']?.isExcluded, false); | ||
|
||
expect(classes.decls['Foo']?.fields.isExcludedValues, | ||
[false, true, false, true]); | ||
expect(classes.decls['Foo']?.methods.isExcludedValues, | ||
[false, true, false, true]); | ||
}); | ||
} |