@@ -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,6 +155,32 @@ 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 );
@@ -161,7 +190,7 @@ public void processOpts() {
161
190
supportingFiles .add (new SupportingFile ("gradle.properties.mustache" , "" , "gradle.properties" ));
162
191
supportingFiles .add (new SupportingFile ("ApiClient.mustache" , invokerFolder , "ApiClient.java" ));
163
192
supportingFiles .add (new SupportingFile ("StringUtil.mustache" , invokerFolder , "StringUtil.java" ));
164
-
193
+
165
194
final String authFolder = (sourceFolder + File .separator + invokerPackage + ".auth" ).replace ("." , File .separator );
166
195
supportingFiles .add (new SupportingFile ("auth/HttpBasicAuth.mustache" , authFolder , "HttpBasicAuth.java" ));
167
196
supportingFiles .add (new SupportingFile ("auth/ApiKeyAuth.mustache" , authFolder , "ApiKeyAuth.java" ));
@@ -175,7 +204,7 @@ public void processOpts() {
175
204
supportingFiles .add (new SupportingFile ("Pair.mustache" , invokerFolder , "Pair.java" ));
176
205
supportingFiles .add (new SupportingFile ("auth/Authentication.mustache" , authFolder , "Authentication.java" ));
177
206
}
178
-
207
+
179
208
// library-specific files
180
209
if ("okhttp-gson" .equals (getLibrary ())) {
181
210
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
@@ -190,25 +219,25 @@ public void processOpts() {
190
219
}
191
220
192
221
private void sanitizeConfig () {
193
- // 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
194
223
// the whole additionalProperties object is injected into the main object passed to the mustache layer
195
-
224
+
196
225
this .setApiPackage (sanitizePackageName (apiPackage ));
197
226
if (additionalProperties .containsKey (CodegenConstants .API_PACKAGE )) {
198
227
this .additionalProperties .put (CodegenConstants .API_PACKAGE , apiPackage );
199
228
}
200
-
229
+
201
230
this .setModelPackage (sanitizePackageName (modelPackage ));
202
231
if (additionalProperties .containsKey (CodegenConstants .MODEL_PACKAGE )) {
203
232
this .additionalProperties .put (CodegenConstants .MODEL_PACKAGE , modelPackage );
204
233
}
205
-
234
+
206
235
this .setInvokerPackage (sanitizePackageName (invokerPackage ));
207
236
if (additionalProperties .containsKey (CodegenConstants .INVOKER_PACKAGE )) {
208
237
this .additionalProperties .put (CodegenConstants .INVOKER_PACKAGE , invokerPackage );
209
238
}
210
239
}
211
-
240
+
212
241
@ Override
213
242
public String escapeReservedWord (String name ) {
214
243
return "_" + name ;
@@ -295,10 +324,22 @@ public String getTypeDeclaration(Property p) {
295
324
public String toDefaultValue (Property p ) {
296
325
if (p instanceof ArrayProperty ) {
297
326
final ArrayProperty ap = (ArrayProperty ) p ;
298
- 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 ()));
299
334
} else if (p instanceof MapProperty ) {
300
335
final MapProperty ap = (MapProperty ) p ;
301
- 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 ()));
302
343
}
303
344
return super .toDefaultValue (p );
304
345
}
@@ -309,7 +350,7 @@ public String getSwaggerType(Property p) {
309
350
String type = null ;
310
351
if (typeMapping .containsKey (swaggerType )) {
311
352
type = typeMapping .get (swaggerType );
312
- if (languageSpecificPrimitives .contains (type )) {
353
+ if (languageSpecificPrimitives .contains (type ) || type . indexOf ( "." ) >= 0 ) {
313
354
return type ;
314
355
}
315
356
} else {
@@ -395,7 +436,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
395
436
}
396
437
return objs ;
397
438
}
398
-
439
+
399
440
public Map <String , Object > postProcessOperations (Map <String , Object > objs ) {
400
441
if ("retrofit" .equals (getLibrary ())) {
401
442
Map <String , Object > operations = (Map <String , Object >) objs .get ("operations" );
@@ -419,6 +460,10 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
419
460
return objs ;
420
461
}
421
462
463
+ protected boolean needToImport (String type ) {
464
+ return super .needToImport (type ) && type .indexOf ("." ) < 0 ;
465
+ }
466
+
422
467
private String findCommonPrefixOfVars (List <String > vars ) {
423
468
String prefix = StringUtils .getCommonPrefix (vars .toArray (new String [vars .size ()]));
424
469
// exclude trailing characters that should be part of a valid variable
0 commit comments