Repository navigation
Expand file tree
/
Copy pathJavaNames.java
More file actions
266 lines (221 loc) · 6.93 KB
/
Copy pathJavaNames.java
File metadata and controls
266 lines (221 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package dev.sqlcj.generator;
import dev.sqlcj.analysis.QueryColumn;
import dev.sqlcj.analysis.QueryModel;
import dev.sqlcj.analysis.QueryParameter;
import javax.lang.model.SourceVersion;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Resolves the Java identifiers of one generated query class.
*
* <p>SQL names that are already valid, non-reserved Java identifiers keep their
* spelling. Every other name is normalized deterministically, and names that
* collide inside the generated parameter list or result record are
* disambiguated in their existing SQL order.
*/
final class JavaNames {
private static final SourceVersion SOURCE_VERSION = SourceVersion.RELEASE_21;
/**
* Simple type names referenced by generated source, plus the Java 21
* identifiers that cannot name a type.
*/
private static final Set<String> RESERVED_CLASS_NAMES = Set.of(
"QueryExecutor",
"RowMapper",
"List",
"BigDecimal",
"LocalDate",
"LocalDateTime",
"OffsetDateTime",
"UUID",
"Boolean",
"Integer",
"Long",
"Short",
"String",
"permits",
"record",
"sealed",
"var",
"yield"
);
/**
* Inherited {@link Object} method names that a generated method or record
* component cannot use.
*/
private static final Set<String> RESERVED_MEMBER_NAMES = Set.of(
"clone",
"finalize",
"getClass",
"hashCode",
"notify",
"notifyAll",
"toString",
"wait"
);
/** Generator-owned names referenced by the generated method body. */
private static final Set<String> RESERVED_PARAMETER_NAMES = Set.of(
"executor",
"ROW_MAPPER"
);
private final String className;
private final String methodName;
private final List<String> parameterNames;
private final List<String> componentNames;
private JavaNames(
String className,
String methodName,
List<String> parameterNames,
List<String> componentNames
) {
this.className = className;
this.methodName = methodName;
this.parameterNames = parameterNames;
this.componentNames = componentNames;
}
static JavaNames of(QueryModel query) {
String className = className(query.name());
return new JavaNames(
className,
methodName(className),
resolveNames(
query.parameters().stream()
.map(QueryParameter::name)
.toList(),
RESERVED_PARAMETER_NAMES
),
resolveNames(
query.columns().stream()
.map(QueryColumn::name)
.toList(),
RESERVED_MEMBER_NAMES
)
);
}
String className() {
return className;
}
String resultTypeName() {
return className + "Result";
}
String methodName() {
return methodName;
}
List<String> parameterNames() {
return parameterNames;
}
List<String> componentNames() {
return componentNames;
}
private static String className(String queryName) {
String name = normalize(queryName);
while (RESERVED_CLASS_NAMES.contains(name)) {
name += "_";
}
return name;
}
private static String methodName(String className) {
String name = decapitalize(className);
while (isKeyword(name) || RESERVED_MEMBER_NAMES.contains(name)) {
name += "_";
}
return name;
}
private static String decapitalize(String className) {
int codePoint = className.codePointAt(0);
return new StringBuilder()
.appendCodePoint(Character.toLowerCase(codePoint))
.append(className.substring(Character.charCount(codePoint)))
.toString();
}
/**
* Resolves one generated namespace, keeping the declared order and using
* stable {@code name1}, {@code name2}, ... suffixes for names that repeat
* or are reserved.
*/
private static List<String> resolveNames(List<String> sqlNames, Set<String> reserved) {
List<String> bases = sqlNames.stream()
.map(JavaNames::normalize)
.toList();
Map<String, Long> occurrences = bases.stream()
.collect(
Collectors.groupingBy(
Function.identity(),
Collectors.counting()
)
);
Set<String> used = new HashSet<>();
List<String> names = new ArrayList<>(bases.size());
for (String base : bases) {
names.add(
resolveName(
base,
occurrences.get(base) > 1,
reserved,
used
)
);
}
return List.copyOf(names);
}
private static String resolveName(
String base,
boolean repeated,
Set<String> reserved,
Set<String> used
) {
if (!repeated && !reserved.contains(base) && !used.contains(base)) {
used.add(base);
return base;
}
int suffix = 1;
String candidate = base + suffix;
while (reserved.contains(candidate) || used.contains(candidate)) {
suffix++;
candidate = base + suffix;
}
used.add(candidate);
return candidate;
}
/**
* Turns a SQL name into a valid Java identifier, preserving names that are
* already valid and non-reserved.
*/
private static String normalize(String sqlName) {
if (SourceVersion.isIdentifier(sqlName) && !isKeyword(sqlName)) {
return sqlName;
}
StringBuilder builder = new StringBuilder();
boolean invalidRun = false;
int index = 0;
while (index < sqlName.length()) {
int codePoint = sqlName.codePointAt(index);
index += Character.charCount(codePoint);
if (Character.isJavaIdentifierPart(codePoint)) {
if (invalidRun) {
builder.append('_');
invalidRun = false;
}
builder.appendCodePoint(codePoint);
continue;
}
invalidRun = true;
}
if (invalidRun) {
builder.append('_');
}
if (builder.isEmpty() || !Character.isJavaIdentifierStart(builder.codePointAt(0))) {
builder.insert(0, '_');
}
String name = builder.toString();
return isKeyword(name) ? name + "_" : name;
}
private static boolean isKeyword(String name) {
return SourceVersion.isKeyword(name, SOURCE_VERSION);
}
}