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] adding the ability for the user to rename #1946

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
44 changes: 35 additions & 9 deletions pkgs/jnigen/lib/src/bindings/renamer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ class _ClassRenamer implements Visitor<ClassDecl, void> {

final outerClassName =
node.outerClass == null ? '' : '${node.outerClass!.finalName}\$';
final className = '$outerClassName${_preprocess(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 All @@ -196,7 +197,14 @@ class _ClassRenamer implements Visitor<ClassDecl, void> {
: className;
node.typeClassName = '\$${node.finalName}\$Type';
node.nullableTypeClassName = '\$${node.finalName}\$NullableType';
log.fine('Class ${node.binaryName} is named ${node.finalName}');

if (node.userDefinedName == null ||
node.userDefinedName == node.finalName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly!

log.fine('Class ${node.binaryName} is named ${node.finalName}');
} else {
log.warning('Renaming Class ${node.binaryName} to ${node.userDefinedName}'
' cause a name collision. Renamed to ${node.finalName} instead.');
}

// Rename fields before renaming methods. In case a method and a field have
// identical names, the field will keep its original name and the
Expand Down Expand Up @@ -227,7 +235,8 @@ class _MethodRenamer implements Visitor<Method, void> {

@override
void visit(Method node) {
final name = _preprocess(node.isConstructor ? 'new' : node.name);
final name = _preprocess(
node.isConstructor ? 'new' : node.userDefinedName ?? node.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user should technically be able to rename the constructors as well, I'm not sure that we currently support this in the dart_generator though. So let's fix that in another PR.

final sig = node.javaSig;
// If node is in super class, assign its number, overriding it.
final superClass =
Expand All @@ -245,8 +254,16 @@ class _MethodRenamer implements Visitor<Method, void> {
node.finalName = _renameConflict(nameCounts, name, _ElementKind.method);
node.classDecl.methodNumsAfterRenaming[sig] = nameCounts[name]! - 1;
}
log.fine('Method ${node.classDecl.binaryName}#${node.name}'
' is named ${node.finalName}');

if (node.userDefinedName == null ||
node.userDefinedName == node.finalName) {
log.fine('Method ${node.classDecl.binaryName}#${node.name}'
' is named ${node.finalName}');
} else {
log.warning('Renaming Method ${node.classDecl.binaryName}#'
'${node.name} to ${node.userDefinedName} cause a name collision. '
'Renamed to ${node.finalName} instead.');
}

final paramRenamer = _ParamRenamer(config);
for (final param in node.params) {
Expand All @@ -263,10 +280,18 @@ class _FieldRenamer implements Visitor<Field, void> {

@override
void visit(Field node) {
final fieldName = _preprocess(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}');

if (node.userDefinedName == null ||
node.userDefinedName == node.finalName) {
log.fine('Field ${node.classDecl.binaryName}#${node.name}'
' is named ${node.finalName}');
} else {
log.warning('Renaming Field ${node.classDecl.binaryName}#${node.name}'
' to ${node.userDefinedName} cause a name collision. '
'Renamed to ${node.finalName} instead.');
}
}
}

Expand All @@ -277,6 +302,7 @@ class _ParamRenamer implements Visitor<Param, void> {

@override
void visit(Param node) {
node.finalName = _keywordRename(node.name, _ElementKind.field);
node.finalName =
_keywordRename(node.userDefinedName ?? node.name, _ElementKind.field);
}
}
12 changes: 12 additions & 0 deletions pkgs/jnigen/lib/src/elements/elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class ClassDecl with ClassMember, Annotated implements Element<ClassDecl> {
@JsonKey(includeFromJson: false)
bool isExcluded;

@JsonKey(includeFromJson: false)
String? userDefinedName;

@override
final Set<String> modifiers;

Expand Down Expand Up @@ -647,6 +650,9 @@ class Method with ClassMember, Annotated implements Element<Method> {
@JsonKey(includeFromJson: false)
bool isExcluded;

@JsonKey(includeFromJson: false)
String? userDefinedName;

@override
final String name;
@override
Expand Down Expand Up @@ -707,6 +713,9 @@ class Param with Annotated implements Element<Param> {
required this.type,
});

@JsonKey(includeFromJson: false)
String? userDefinedName;

@override
List<Annotation>? annotations;
final JavaDocComment? javadoc;
Expand Down Expand Up @@ -751,6 +760,9 @@ class Field with ClassMember, Annotated implements Element<Field> {
@JsonKey(includeFromJson: false)
bool isExcluded;

@JsonKey(includeFromJson: false)
String? userDefinedName;

@override
final String name;
@override
Expand Down
45 changes: 38 additions & 7 deletions pkgs/jnigen/lib/src/elements/j_elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ abstract class Visitor {
void visitClass(ClassDecl c) {}
void visitMethod(Method method) {}
void visitField(Field field) {}
void visitParam(Param parameter) {}
}

class Classes implements Element {
Expand All @@ -30,15 +31,19 @@ class Classes implements Element {
}

class ClassDecl implements Element {
ClassDecl(this._classDecl) : binaryName = _classDecl.binaryName;
ClassDecl(this._classDecl);
final ast.ClassDecl _classDecl;

// Ex: com.x.Foo.
final String binaryName;
String get binaryName => _classDecl.binaryName;

bool get isExcluded => _classDecl.isExcluded;
set isExcluded(bool value) => _classDecl.isExcluded = value;

String get name => _classDecl.userDefinedName ?? _classDecl.name;
set name(String newName) => _classDecl.userDefinedName = newName;

String get originalName => _classDecl.name;

@override
void accept(Visitor visitor) {
visitor.visitClass(this);
Expand All @@ -57,14 +62,37 @@ class Method implements Element {

final ast.Method _method;

String get name => _method.name;

bool get isExcluded => _method.isExcluded;
set isExcluded(bool value) => _method.isExcluded = value;

String get name => _method.userDefinedName ?? _method.name;
set name(String newName) => _method.userDefinedName = newName;

String get originalName => _method.name;

@override
void accept(Visitor visitor) {
visitor.visitMethod(this);
if (_method.isExcluded) return;
for (final param in _method.params) {
Param(param).accept(visitor);
}
}
}

class Param implements Element {
Param(this._param);

final ast.Param _param;

String get name => _param.userDefinedName ?? _param.name;
set name(String newName) => _param.userDefinedName = newName;

String get originalName => _param.name;

@override
void accept(Visitor visitor) {
visitor.visitParam(this);
}
}

Expand All @@ -73,11 +101,14 @@ class Field implements Element {

final ast.Field _field;

String get name => _field.name;

bool get isExcluded => _field.isExcluded;
set isExcluded(bool value) => _field.isExcluded = value;

String get name => _field.userDefinedName ?? _field.name;
set name(String newName) => _field.userDefinedName = newName;

String get originalName => _field.name;

@override
void accept(Visitor visitor) {
visitor.visitField(this);
Expand Down
86 changes: 0 additions & 86 deletions pkgs/jnigen/test/user_excluder.dart

This file was deleted.

Loading