@@ -37,6 +37,8 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
37
37
protected String artifactVersion = "1.0.0" ;
38
38
protected String sourceFolder = "src/main/java" ;
39
39
protected String localVariablePrefix = "" ;
40
+ protected boolean fullJavaUtil = false ;
41
+ protected String javaUtilPrefix = "" ;
40
42
protected Boolean serializableModel = false ;
41
43
42
44
public JavaClientCodegen () {
@@ -81,6 +83,7 @@ public JavaClientCodegen() {
81
83
cliOptions .add (new CliOption (CodegenConstants .SOURCE_FOLDER , CodegenConstants .SOURCE_FOLDER_DESC ));
82
84
cliOptions .add (new CliOption (CodegenConstants .LOCAL_VARIABLE_PREFIX , CodegenConstants .LOCAL_VARIABLE_PREFIX_DESC ));
83
85
cliOptions .add (new CliOption (CodegenConstants .SERIALIZABLE_MODEL , CodegenConstants .SERIALIZABLE_MODEL_DESC ));
86
+ cliOptions .add (new CliOption ("fullJavaUtil" , "whether to use fully qualified name for classes under java.util (default to false)" ));
84
87
85
88
supportedLibraries .put ("<default>" , "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2" );
86
89
supportedLibraries .put ("jersey2" , "HTTP client: Jersey client 2.6" );
@@ -107,7 +110,7 @@ public String getHelp() {
107
110
@ Override
108
111
public void processOpts () {
109
112
super .processOpts ();
110
-
113
+
111
114
if (additionalProperties .containsKey (CodegenConstants .INVOKER_PACKAGE )) {
112
115
this .setInvokerPackage ((String ) additionalProperties .get (CodegenConstants .INVOKER_PACKAGE ));
113
116
} else {
@@ -152,13 +155,42 @@ public void processOpts() {
152
155
// need to put back serializableModel (boolean) into additionalProperties as value in additionalProperties is string
153
156
additionalProperties .put (CodegenConstants .SERIALIZABLE_MODEL , serializableModel );
154
157
158
+ if (additionalProperties .containsKey ("fullJavaUtil" )) {
159
+ fullJavaUtil = Boolean .valueOf (additionalProperties .get ("fullJavaUtil" ).toString ());
160
+ }
161
+ if (fullJavaUtil ) {
162
+ javaUtilPrefix = "java.util." ;
163
+ }
164
+ additionalProperties .put ("fullJavaUtil" , fullJavaUtil );
165
+ additionalProperties .put ("javaUtilPrefix" , javaUtilPrefix );
166
+
167
+ if (fullJavaUtil ) {
168
+ typeMapping .put ("array" , "java.util.List" );
169
+ typeMapping .put ("map" , "java.util.Map" );
170
+ typeMapping .put ("DateTime" , "java.util.Date" );
171
+ typeMapping .remove ("List" );
172
+ importMapping .remove ("Date" );
173
+ importMapping .remove ("Map" );
174
+ importMapping .remove ("HashMap" );
175
+ importMapping .remove ("Array" );
176
+ importMapping .remove ("ArrayList" );
177
+ importMapping .remove ("List" );
178
+ importMapping .remove ("Set" );
179
+ importMapping .remove ("DateTime" );
180
+ instantiationTypes .put ("array" , "java.util.ArrayList" );
181
+ instantiationTypes .put ("map" , "java.util.HashMap" );
182
+ }
183
+
155
184
this .sanitizeConfig ();
156
185
157
186
final String invokerFolder = (sourceFolder + File .separator + invokerPackage ).replace ("." , File .separator );
158
187
supportingFiles .add (new SupportingFile ("pom.mustache" , "" , "pom.xml" ));
188
+ supportingFiles .add (new SupportingFile ("build.gradle.mustache" , "" , "build.gradle" ));
189
+ supportingFiles .add (new SupportingFile ("settings.gradle.mustache" , "" , "settings.gradle" ));
190
+ supportingFiles .add (new SupportingFile ("gradle.properties.mustache" , "" , "gradle.properties" ));
159
191
supportingFiles .add (new SupportingFile ("ApiClient.mustache" , invokerFolder , "ApiClient.java" ));
160
192
supportingFiles .add (new SupportingFile ("StringUtil.mustache" , invokerFolder , "StringUtil.java" ));
161
-
193
+
162
194
final String authFolder = (sourceFolder + File .separator + invokerPackage + ".auth" ).replace ("." , File .separator );
163
195
supportingFiles .add (new SupportingFile ("auth/HttpBasicAuth.mustache" , authFolder , "HttpBasicAuth.java" ));
164
196
supportingFiles .add (new SupportingFile ("auth/ApiKeyAuth.mustache" , authFolder , "ApiKeyAuth.java" ));
@@ -172,13 +204,11 @@ public void processOpts() {
172
204
supportingFiles .add (new SupportingFile ("Pair.mustache" , invokerFolder , "Pair.java" ));
173
205
supportingFiles .add (new SupportingFile ("auth/Authentication.mustache" , authFolder , "Authentication.java" ));
174
206
}
175
-
207
+
176
208
// library-specific files
177
209
if ("okhttp-gson" .equals (getLibrary ())) {
178
210
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
179
211
supportingFiles .add (new SupportingFile ("ApiCallback.mustache" , invokerFolder , "ApiCallback.java" ));
180
- // "build.gradle" is for development with Gradle
181
- supportingFiles .add (new SupportingFile ("build.gradle.mustache" , "" , "build.gradle" ));
182
212
// "build.sbt" is for development with SBT
183
213
supportingFiles .add (new SupportingFile ("build.sbt.mustache" , "" , "build.sbt" ));
184
214
} else if ("retrofit" .equals (getLibrary ())) {
@@ -189,25 +219,25 @@ public void processOpts() {
189
219
}
190
220
191
221
private void sanitizeConfig () {
192
- // Sanitize any config options here. We also have to update the additionalProperties because
222
+ // Sanitize any config options here. We also have to update the additionalProperties because
193
223
// the whole additionalProperties object is injected into the main object passed to the mustache layer
194
-
224
+
195
225
this .setApiPackage (sanitizePackageName (apiPackage ));
196
226
if (additionalProperties .containsKey (CodegenConstants .API_PACKAGE )) {
197
227
this .additionalProperties .put (CodegenConstants .API_PACKAGE , apiPackage );
198
228
}
199
-
229
+
200
230
this .setModelPackage (sanitizePackageName (modelPackage ));
201
231
if (additionalProperties .containsKey (CodegenConstants .MODEL_PACKAGE )) {
202
232
this .additionalProperties .put (CodegenConstants .MODEL_PACKAGE , modelPackage );
203
233
}
204
-
234
+
205
235
this .setInvokerPackage (sanitizePackageName (invokerPackage ));
206
236
if (additionalProperties .containsKey (CodegenConstants .INVOKER_PACKAGE )) {
207
237
this .additionalProperties .put (CodegenConstants .INVOKER_PACKAGE , invokerPackage );
208
238
}
209
239
}
210
-
240
+
211
241
@ Override
212
242
public String escapeReservedWord (String name ) {
213
243
return "_" + name ;
@@ -294,10 +324,22 @@ public String getTypeDeclaration(Property p) {
294
324
public String toDefaultValue (Property p ) {
295
325
if (p instanceof ArrayProperty ) {
296
326
final ArrayProperty ap = (ArrayProperty ) p ;
297
- return String .format ("new ArrayList<%s>()" , getTypeDeclaration (ap .getItems ()));
327
+ final String pattern ;
328
+ if (fullJavaUtil ) {
329
+ pattern = "new java.util.ArrayList<%s>()" ;
330
+ } else {
331
+ pattern = "new ArrayList<%s>()" ;
332
+ }
333
+ return String .format (pattern , getTypeDeclaration (ap .getItems ()));
298
334
} else if (p instanceof MapProperty ) {
299
335
final MapProperty ap = (MapProperty ) p ;
300
- return String .format ("new HashMap<String, %s>()" , getTypeDeclaration (ap .getAdditionalProperties ()));
336
+ final String pattern ;
337
+ if (fullJavaUtil ) {
338
+ pattern = "new java.util.HashMap<String, %s>()" ;
339
+ } else {
340
+ pattern = "new HashMap<String, %s>()" ;
341
+ }
342
+ return String .format (pattern , getTypeDeclaration (ap .getAdditionalProperties ()));
301
343
}
302
344
return super .toDefaultValue (p );
303
345
}
@@ -308,7 +350,7 @@ public String getSwaggerType(Property p) {
308
350
String type = null ;
309
351
if (typeMapping .containsKey (swaggerType )) {
310
352
type = typeMapping .get (swaggerType );
311
- if (languageSpecificPrimitives .contains (type )) {
353
+ if (languageSpecificPrimitives .contains (type ) || type . indexOf ( "." ) >= 0 ) {
312
354
return type ;
313
355
}
314
356
} else {
@@ -394,7 +436,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
394
436
}
395
437
return objs ;
396
438
}
397
-
439
+
398
440
public Map <String , Object > postProcessOperations (Map <String , Object > objs ) {
399
441
if ("retrofit" .equals (getLibrary ())) {
400
442
Map <String , Object > operations = (Map <String , Object >) objs .get ("operations" );
@@ -418,6 +460,10 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
418
460
return objs ;
419
461
}
420
462
463
+ protected boolean needToImport (String type ) {
464
+ return super .needToImport (type ) && type .indexOf ("." ) < 0 ;
465
+ }
466
+
421
467
private String findCommonPrefixOfVars (List <String > vars ) {
422
468
String prefix = StringUtils .getCommonPrefix (vars .toArray (new String [vars .size ()]));
423
469
// exclude trailing characters that should be part of a valid variable
@@ -426,7 +472,12 @@ private String findCommonPrefixOfVars(List<String> vars) {
426
472
}
427
473
428
474
private String toEnumVarName (String value ) {
429
- return value .replaceAll ("\\ W+" , "_" ).toUpperCase ();
475
+ String var = value .replaceAll ("\\ W+" , "_" ).toUpperCase ();
476
+ if (var .matches ("\\ d.*" )) {
477
+ return "_" + var ;
478
+ } else {
479
+ return var ;
480
+ }
430
481
}
431
482
432
483
private CodegenModel reconcileInlineEnums (CodegenModel codegenModel , CodegenModel parentCodegenModel ) {
0 commit comments