17
17
18
18
import static java .util .stream .Collectors .joining ;
19
19
import static software .amazon .awssdk .codegen .internal .Constant .AUTHORIZER_NAME_PREFIX ;
20
+ import static software .amazon .awssdk .codegen .internal .Constant .CONFLICTING_NAME_SUFFIX ;
20
21
import static software .amazon .awssdk .codegen .internal .Constant .EXCEPTION_CLASS_SUFFIX ;
21
22
import static software .amazon .awssdk .codegen .internal .Constant .FAULT_CLASS_SUFFIX ;
22
23
import static software .amazon .awssdk .codegen .internal .Constant .REQUEST_CLASS_SUFFIX ;
23
24
import static software .amazon .awssdk .codegen .internal .Constant .RESPONSE_CLASS_SUFFIX ;
24
- import static software .amazon .awssdk .codegen .internal .Constant .VARIABLE_NAME_SUFFIX ;
25
25
import static software .amazon .awssdk .codegen .internal .Utils .unCapitalize ;
26
26
27
27
import java .util .Arrays ;
@@ -50,16 +50,41 @@ public class DefaultNamingStrategy implements NamingStrategy {
50
50
51
51
private static final Set <String > RESERVED_KEYWORDS ;
52
52
53
+ private static final Set <String > RESERVED_EXCEPTION_METHOD_NAMES ;
54
+
55
+ private static final Set <Object > RESERVED_STRUCTURE_METHOD_NAMES ;
56
+
53
57
static {
54
- Set <String > keywords = new HashSet <>();
55
- Collections .addAll (keywords ,
58
+ Set <String > reservedJavaKeywords = new HashSet <>();
59
+ Collections .addAll (reservedJavaKeywords ,
56
60
"abstract" , "assert" , "boolean" , "break" , "byte" , "case" , "catch" , "char" , "class" ,
57
- "continue" , "default" , "do" , "double" , "else" , "enum" , "extends" , "final" , "finally" , "float" , "for" ,
58
- "if" , "implements" , "import" , "instanceof" , "int" , "interface" , "long" , "native" , "new" , "package" ,
59
- "private" , "protected" , "public" , "return" , "short" , "static" , "strictfp" , "super" , "switch" ,
60
- "synchronized" , "this" , "throw" , "throws" , "transient" , "try" , "void" , "volatile" , "while" , "true" ,
61
- "null" , "false" , "const" , "goto" );
62
- RESERVED_KEYWORDS = Collections .unmodifiableSet (keywords );
61
+ "const" , "continue" , "default" , "do" , "double" , "else" , "enum" , "extends" , "false" , "final" ,
62
+ "finally" , "float" , "for" , "goto" , "if" , "implements" , "import" , "instanceof" , "int" ,
63
+ "interface" , "long" , "native" , "new" , "null" , "package" , "private" , "protected" , "public" ,
64
+ "return" , "short" , "static" , "strictfp" , "super" , "switch" , "synchronized" , "this" , "throw" ,
65
+ "throws" , "transient" , "true" , "try" , "void" , "volatile" , "while" );
66
+ RESERVED_KEYWORDS = Collections .unmodifiableSet (reservedJavaKeywords );
67
+
68
+
69
+ Set <String > reservedJavaMethodNames = new HashSet <>();
70
+ Collections .addAll (reservedJavaMethodNames ,
71
+ "equals" , "finalize" , "getClass" , "hashCode" , "notify" , "notifyAll" , "toString" , "wait" );
72
+
73
+ Set <String > reserveJavaPojoMethodNames = new HashSet <>(reservedJavaMethodNames );
74
+ Collections .addAll (reserveJavaPojoMethodNames ,
75
+ "builder" , "sdkFields" , "toBuilder" );
76
+
77
+ Set <String > reservedExceptionMethodNames = new HashSet <>(reserveJavaPojoMethodNames );
78
+ Collections .addAll (reservedExceptionMethodNames ,
79
+ "awsErrorDetails" , "cause" , "fillInStackTrace" , "getCause" , "getLocalizedMessage" ,
80
+ "getMessage" , "getStackTrace" , "getSuppressed" , "isClockSkewException" , "isThrottlingException" ,
81
+ "printStackTrace" , "requestId" , "retryable" , "serializableBuilderClass" , "statusCode" );
82
+ RESERVED_EXCEPTION_METHOD_NAMES = Collections .unmodifiableSet (reservedExceptionMethodNames );
83
+
84
+ Set <String > reservedStructureMethodNames = new HashSet <>(reserveJavaPojoMethodNames );
85
+ Collections .addAll (reservedStructureMethodNames ,
86
+ "overrideConfiguration" , "sdkHttpResponse" );
87
+ RESERVED_STRUCTURE_METHOD_NAMES = Collections .unmodifiableSet (reservedStructureMethodNames );
63
88
}
64
89
65
90
private final ServiceModel serviceModel ;
@@ -189,11 +214,15 @@ public String getResponseClassName(String operationName) {
189
214
190
215
@ Override
191
216
public String getVariableName (String name ) {
192
- if (isJavaKeyword (name )) {
193
- return unCapitalize (name + VARIABLE_NAME_SUFFIX );
194
- } else {
195
- return unCapitalize (name );
217
+ // Exclude keywords because they will not compile, and exclude reserved method names because they're frequently
218
+ // used for local variable names.
219
+ if (RESERVED_KEYWORDS .contains (name ) ||
220
+ RESERVED_STRUCTURE_METHOD_NAMES .contains (name ) ||
221
+ RESERVED_EXCEPTION_METHOD_NAMES .contains (name )) {
222
+ return unCapitalize (name + CONFLICTING_NAME_SUFFIX );
196
223
}
224
+
225
+ return unCapitalize (name );
197
226
}
198
227
199
228
@ Override
@@ -236,9 +265,11 @@ public String getAuthorizerClassName(String shapeName) {
236
265
}
237
266
238
267
@ Override
239
- public String getFluentGetterMethodName (String memberName , Shape shape ) {
268
+ public String getFluentGetterMethodName (String memberName , Shape parentShape , Shape shape ) {
240
269
String getterMethodName = Utils .unCapitalize (memberName );
241
270
271
+ getterMethodName = rewriteInvalidMemberName (getterMethodName , parentShape );
272
+
242
273
if (Utils .isOrContainsEnumShape (shape , serviceModel .getShapes ())) {
243
274
getterMethodName += "AsString" ;
244
275
@@ -251,33 +282,34 @@ public String getFluentGetterMethodName(String memberName, Shape shape) {
251
282
}
252
283
253
284
@ Override
254
- public String getFluentEnumGetterMethodName (String memberName , Shape shape ) {
285
+ public String getFluentEnumGetterMethodName (String memberName , Shape parentShape , Shape shape ) {
255
286
if (!Utils .isOrContainsEnumShape (shape , serviceModel .getShapes ())) {
256
287
return null ;
257
288
}
258
289
259
- return Utils .unCapitalize (memberName );
260
- }
261
-
262
- @ Override
263
- public String getBeanStyleGetterMethodName (String memberName ) {
264
- return String .format ("get%s" , Utils .capitalize (memberName ));
290
+ String getterMethodName = Utils .unCapitalize (memberName );
291
+ getterMethodName = rewriteInvalidMemberName (getterMethodName , parentShape );
292
+ return getterMethodName ;
265
293
}
266
294
267
295
@ Override
268
- public String getSetterMethodName (String memberName ) {
269
- return Utils .unCapitalize (memberName );
296
+ public String getBeanStyleGetterMethodName (String memberName , Shape parentShape , Shape c2jShape ) {
297
+ String fluentGetterMethodName = getFluentGetterMethodName (memberName , parentShape , c2jShape );
298
+ return String .format ("get%s" , Utils .capitalize (fluentGetterMethodName ));
270
299
}
271
300
272
301
@ Override
273
- public String getBeanStyleSetterMethodName (String memberName ) {
274
- return String .format ("set%s" , Utils .capitalize (memberName ));
302
+ public String getBeanStyleSetterMethodName (String memberName , Shape parentShape , Shape c2jShape ) {
303
+ String fluentSetterMethodName = getFluentSetterMethodName (memberName , parentShape , c2jShape );
304
+ return String .format ("set%s" , Utils .capitalize (fluentSetterMethodName ));
275
305
}
276
306
277
307
@ Override
278
- public String getFluentSetterMethodName (String memberName , Shape shape ) {
308
+ public String getFluentSetterMethodName (String memberName , Shape parentShape , Shape shape ) {
279
309
String setterMethodName = Utils .unCapitalize (memberName );
280
310
311
+ setterMethodName = rewriteInvalidMemberName (setterMethodName , parentShape );
312
+
281
313
if (Utils .isOrContainsEnumShape (shape , serviceModel .getShapes ()) &&
282
314
(Utils .isListShape (shape ) || Utils .isMapShape (shape ))) {
283
315
@@ -288,19 +320,37 @@ public String getFluentSetterMethodName(String memberName, Shape shape) {
288
320
}
289
321
290
322
@ Override
291
- public String getFluentEnumSetterMethodName (String memberName , Shape shape ) {
323
+ public String getFluentEnumSetterMethodName (String memberName , Shape parentShape , Shape shape ) {
292
324
if (!Utils .isOrContainsEnumShape (shape , serviceModel .getShapes ())) {
293
325
return null ;
294
326
}
295
327
296
- return Utils .unCapitalize (memberName );
328
+ String setterMethodName = Utils .unCapitalize (memberName );
329
+ setterMethodName = rewriteInvalidMemberName (setterMethodName , parentShape );
330
+ return setterMethodName ;
297
331
}
298
332
299
333
@ Override
300
334
public String getSdkFieldFieldName (MemberModel memberModel ) {
301
335
return screamCase (memberModel .getName ()) + "_FIELD" ;
302
336
}
303
337
338
+ private String rewriteInvalidMemberName (String memberName , Shape parentShape ) {
339
+ if (isJavaKeyword (memberName ) || isDisallowedNameForShape (memberName , parentShape )) {
340
+ return Utils .unCapitalize (memberName + CONFLICTING_NAME_SUFFIX );
341
+ }
342
+
343
+ return memberName ;
344
+ }
345
+
346
+ private boolean isDisallowedNameForShape (String name , Shape parentShape ) {
347
+ if (parentShape .isException ()) {
348
+ return RESERVED_EXCEPTION_METHOD_NAMES .contains (name );
349
+ } else {
350
+ return RESERVED_STRUCTURE_METHOD_NAMES .contains (name );
351
+ }
352
+ }
353
+
304
354
private String [] splitOnWordBoundaries (String toSplit ) {
305
355
String result = toSplit ;
306
356
0 commit comments