Skip to content

Commit f67b95c

Browse files
Use a different renaming strategy 2
1 parent 3c4898b commit f67b95c

21 files changed

Lines changed: 366 additions & 207 deletions

File tree

‎pkgs/jnigen/CHANGELOG.md‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
## 0.11.1-wip
2-
1+
## 0.12.0-wip
2+
3+
- **Breaking Change**([#1530](https://github.com/dart-lang/native/pull/1530)):
4+
Changed the renaming strategy for method overloadings. Instead of adding a
5+
numeric suffix, we add a dollar sign (`$`) and then the numeric suffix. This
6+
is done to avoid name collision between methods that originally end with
7+
numeric suffices and the renamed overloads. Similarly names that are Dart
8+
keywords get a dollar sign suffix now. For more information, check out the
9+
[documentation](https://github.com/dart-lang/native/tree/main/pkgs/jnigen/docs/java_differences.md#method_overloading).
310
- Fixed an issue where inheriting a generic class could generate incorrect code.
411
- No longer generating constructors for abstract classes.
512
- No longer generating `protected` elements.
6-
- Adding a dollar sign (`$`) to the end of each Java element that ends with a
7-
number to avoid name collisions that can happen with the numeric suffixes of
8-
the overloaded methods.
9-
([[#1530](https://github.com/dart-lang/native/pull/1530)).
1013

1114
## 0.11.0
1215

‎pkgs/jnigen/docs/java_differences.md‎

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,32 @@ public class Calculator {
2222
}
2323
```
2424

25-
This is not the case for Dart. Each method of a class must have a unique
26-
name. To overcome this limitation, JNIgen adds a dollar sign (`$`) and a numeric
25+
This is not the case for Dart. Each method of a class must have a unique name.
26+
To overcome this limitation, JNIgen adds a dollar sign (`$`) and a numeric
2727
suffix to the end of the overloaded method name.
2828

2929
```dart
30-
// Dart Bindings
30+
// Dart Bindings - Boilerplate omitted for clarity.
3131
class Calculator extends JObject {
32-
// Omitted for clarity.
3332
int add(int a, int b) { /* ... */ }
33+
3434
double add$1(double a, double b) { /* ... */ }
35+
3536
double add$2(double a, double b) { /* ... */ }
3637
}
3738
```
3839

39-
> [!INFORMATION]
40+
> [!WARNING]
4041
> Running the code generator again on a different version of the library can map
4142
> the methods differently if the order of the methods change or another
4243
> overloading of the same method gets added.
4344
>
4445
> Double check the doc comments on the generated methods to make sure you are
4546
> calling your intended method.
4647
47-
You might wonder why the method isn't renamed to `add1` instead of `add$1`. The reason is that a method named `add1` could already exist in the Java class.
48+
You might wonder why the method isn't renamed to `add1` instead of `add$1`. The
49+
reason is that a method named `add1` could already exist in the Java class. On
50+
the other hand, Java identifiers cannot contain dollar signs.
4851

4952
```java
5053
// Java
@@ -74,13 +77,90 @@ public class Calculator {
7477
In this case, the generated code will be:
7578

7679
```dart
77-
// Dart Bindings
80+
// Dart Bindings - Boilerplate omitted for clarity.
7881
class Calculator extends JObject {
79-
// Omitted for clarity.
8082
int add(int a, int b) { /* ... */ }
83+
8184
double add$1(double a, double b) { /* ... */ }
85+
8286
double add$2(double a, double b) { /* ... */ }
87+
8388
int add1(int a) { /* ... */ }
89+
8490
double add1$2(double a) { /* ... */ }
8591
}
8692
```
93+
94+
### Fields and methods with the same name
95+
96+
In Java, we can have a field and a method with the same name. This is not
97+
possible in Dart.
98+
99+
```java
100+
// Java
101+
public class Player {
102+
// Player's personal duck!
103+
public Duck duck;
104+
105+
// Lower your head!
106+
public void duck() { /* ... */ }
107+
}
108+
```
109+
110+
JNIgen handles this similarly to [method overloading](#method-overloading). The
111+
method with the same name as the field will be appended by a dollar sign (`$`)
112+
followed by a numeric suffix.
113+
114+
```dart
115+
// Dart Bindings - Boilerplate omitted for clarity.
116+
class Player extends JObject {
117+
Duck get duck { /* ... */ };
118+
set duck(Duck value) { /* ... */ }
119+
120+
void duck$1() { /* ... */ }
121+
122+
// If there were more `duck` methods they would be named
123+
// `duck$2`, `duck$3`, ...
124+
}
125+
```
126+
127+
It is important to note that the order of renaming is fields-first
128+
methods-second. So when both a field named `duck` and a method named `duck`
129+
exist, the field keeps its original name as seen above.
130+
131+
However the renaming happens for superclasses first. Consider this case.
132+
133+
```java
134+
// Java
135+
public class Player {
136+
// Lower your head!
137+
public void duck() { /* ... */ }
138+
}
139+
140+
public class DuckOwningPlayer extends Player {
141+
// Player's personal duck!
142+
public Duck duck;
143+
}
144+
```
145+
146+
The `Player` class already has a method named `duck` and no field with the same
147+
name. So this will be the generated bindings for it:
148+
149+
```dart
150+
// Dart Bindings - Boilerplate omitted for clarity.
151+
class Player extends JObject {
152+
void duck() { /* ... */ }
153+
}
154+
```
155+
156+
`DuckOwningPlayer` inherits the `duck()` method from `Player` and adds a field
157+
named `duck`. This time, the field will be renamed as the method is simply
158+
inherited.
159+
160+
```dart
161+
// Dart Bindings - Boilerplate omitted for clarity.
162+
class DuckOwningPlayer extends Player {
163+
Duck get duck$1 { /* ... */ };
164+
set duck$1(Duck value) { /* ... */ }
165+
}
166+
```

0 commit comments

Comments
 (0)