You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f67b95c
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: pkgs/jnigen/docs/java_differences.md
+88-8Lines changed: 88 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,29 +22,32 @@ public class Calculator {
22
22
}
23
23
```
24
24
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
27
27
suffix to the end of the overloaded method name.
28
28
29
29
```dart
30
-
// Dart Bindings
30
+
// Dart Bindings - Boilerplate omitted for clarity.
31
31
class Calculator extends JObject {
32
-
// Omitted for clarity.
33
32
int add(int a, int b) { /* ... */ }
33
+
34
34
double add$1(double a, double b) { /* ... */ }
35
+
35
36
double add$2(double a, double b) { /* ... */ }
36
37
}
37
38
```
38
39
39
-
> [!INFORMATION]
40
+
> [!WARNING]
40
41
> Running the code generator again on a different version of the library can map
41
42
> the methods differently if the order of the methods change or another
42
43
> overloading of the same method gets added.
43
44
>
44
45
> Double check the doc comments on the generated methods to make sure you are
45
46
> calling your intended method.
46
47
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.
48
51
49
52
```java
50
53
// Java
@@ -74,13 +77,90 @@ public class Calculator {
74
77
In this case, the generated code will be:
75
78
76
79
```dart
77
-
// Dart Bindings
80
+
// Dart Bindings - Boilerplate omitted for clarity.
78
81
class Calculator extends JObject {
79
-
// Omitted for clarity.
80
82
int add(int a, int b) { /* ... */ }
83
+
81
84
double add$1(double a, double b) { /* ... */ }
85
+
82
86
double add$2(double a, double b) { /* ... */ }
87
+
83
88
int add1(int a) { /* ... */ }
89
+
84
90
double add1$2(double a) { /* ... */ }
85
91
}
86
92
```
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
+
publicclassPlayer {
102
+
// Player's personal duck!
103
+
publicDuck duck;
104
+
105
+
// Lower your head!
106
+
publicvoidduck() { /* ... */ }
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
+
publicclassPlayer {
136
+
// Lower your head!
137
+
publicvoidduck() { /* ... */ }
138
+
}
139
+
140
+
publicclassDuckOwningPlayerextendsPlayer {
141
+
// Player's personal duck!
142
+
publicDuck 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.
0 commit comments