Skip to content

Commit

Permalink
ability for the user to rename
Browse files Browse the repository at this point in the history
  • Loading branch information
marshelino-maged committed Jan 27, 2025
1 parent eec4f6e commit 92d2a8d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 96 deletions.
14 changes: 7 additions & 7 deletions pkgs/jnigen/lib/src/bindings/renamer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ class _ClassRenamer implements Visitor<ClassDecl, void> {

final outerClassName =
node.outerClass == null ? '' : '${node.outerClass!.finalName}\$';
final className =
'$outerClassName${_preprocess(node.userDefinedName?? node.name)}';
final className =
'$outerClassName${_preprocess(node.userDefinedName ?? node.name)}';

// When generating all the classes in a single file
// the names need to be unique.
Expand Down Expand Up @@ -228,8 +228,8 @@ class _MethodRenamer implements Visitor<Method, void> {

@override
void visit(Method node) {
final name = _preprocess(node.isConstructor ?
'new' : node.userDefinedName?? node.name);
final name = _preprocess(
node.isConstructor ? 'new' : node.userDefinedName ?? node.name);
final sig = node.javaSig;
// If node is in super class, assign its number, overriding it.
final superClass =
Expand Down Expand Up @@ -265,7 +265,7 @@ class _FieldRenamer implements Visitor<Field, void> {

@override
void visit(Field node) {
final fieldName = _preprocess(node.userDefinedName?? node.name);
final fieldName = _preprocess(node.userDefinedName ?? node.name);
node.finalName = _renameConflict(nameCounts, fieldName, _ElementKind.field);
log.fine('Field ${node.classDecl.binaryName}#${node.name}'
' is named ${node.finalName}');
Expand All @@ -279,7 +279,7 @@ class _ParamRenamer implements Visitor<Param, void> {

@override
void visit(Param node) {
node.finalName =
_keywordRename(node.userDefinedName?? node.name, _ElementKind.field);
node.finalName =
_keywordRename(node.userDefinedName ?? node.name, _ElementKind.field);
}
}
86 changes: 0 additions & 86 deletions pkgs/jnigen/test/user_excluder.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ 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();
}

extension on Iterable<ast.Method> {
List<String> get finalNames => map((m) => m.finalName).toList();
}
Expand All @@ -21,6 +29,30 @@ extension on Iterable<ast.Field> {
List<String> get finalNames => map((f) => f.finalName).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;
}
}
}

// This is customizable by the user
class UserRenamer extends Visitor {
@override
Expand Down Expand Up @@ -60,14 +92,58 @@ Future<void> rename(ast.Classes classes) async {
structure: jnigen.OutputStructure.singleFile,
),
),
classes: []
);
classes: []);
await classes.accept(Linker(config));
classes.accept(Renamer(config));
}

void main() {
test('rename classes, fields, methods and params using user visitors',
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]);
});

test('Rename classes, fields, methods and params using the user renamer',
() async {
final classes = ast.Classes({
'Foo': ast.ClassDecl(
Expand Down

0 comments on commit 92d2a8d

Please sign in to comment.