Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jnigen] small fix for renaming constructors #1970

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkgs/jnigen/lib/src/bindings/renamer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class _MethodRenamer implements Visitor<Method, void> {
@override
void visit(Method node) {
final name = _preprocess(
node.isConstructor ? 'new' : node.userDefinedName ?? node.name);
node.userDefinedName ?? (node.isConstructor ? 'new' : node.name));
final sig = node.javaSig;
// If node is in super class, assign its number, overriding it.
final superClass =
Expand Down
2 changes: 2 additions & 0 deletions pkgs/jnigen/lib/src/elements/j_elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class Method implements Element {

String get originalName => _method.name;

bool get isConstructor => _method.isConstructor;

@override
void accept(Visitor visitor) {
visitor.visitMethod(this);
Expand Down
6 changes: 6 additions & 0 deletions pkgs/jnigen/test/user_visitors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class UserRenamer extends Visitor {
if (method.originalName.contains('Foo')) {
method.name = method.originalName.replaceAll('Foo', 'Bar');
}
if (method.isConstructor) {
method.name = 'constructor';
}
}

@override
Expand Down Expand Up @@ -172,6 +175,7 @@ void main() {
ast.Param(name: 'Foo', type: ast.TypeUsage.object),
ast.Param(name: 'Foo1', type: ast.TypeUsage.object),
]),
ast.Method(name: '<init>', returnType: ast.TypeUsage.object)
],
),
});
Expand All @@ -190,6 +194,8 @@ void main() {
expect(classes.decls['Foo']?.methods.finalNames,
[r'Bar$2', r'Bar$3', r'Bar1$2', r'Bar1$3']);

expect(classes.decls['y.Foo']?.methods.finalNames, [r'Bar', 'constructor']);

expect(classes.decls['y.Foo']?.methods.first.params.finalNames,
['Bar', 'Bar1']);
});
Expand Down