22
22
import java .util .HashSet ;
23
23
import java .util .Iterator ;
24
24
import java .util .List ;
25
+ import java .util .Map ;
25
26
import java .util .Set ;
26
27
27
28
import net .sf .j2s .ajax .SimpleSerializable ;
@@ -192,6 +193,16 @@ public static String generateSourceFromObject(SimpleSerializable s) {
192
193
193
194
boolean hasMoreImports = false ;
194
195
Set <String > importedClasses = new HashSet <String >();
196
+
197
+ Map <String , String > fieldMappings = s .fieldNameMapping ();
198
+ if (fieldMappings != null && fieldMappings .size () > 0 ) {
199
+ hasMoreImports = true ;
200
+ String mapTypeName = "java.util.Map" ;
201
+ source .append ("import " );
202
+ source .append (mapTypeName );
203
+ source .append (";\r \n " );
204
+ importedClasses .add (mapTypeName );
205
+ }
195
206
196
207
Type [] interfaces = s .getClass ().getGenericInterfaces ();
197
208
if (interfaces != null && interfaces .length > 0 ) {
@@ -341,6 +352,23 @@ public int compare(Class<?> c1, Class<?> c2) {
341
352
source .append ("{\r \n \r \n " );
342
353
SourceUtils .insertLineComment (source , "\t " , index ++, true );
343
354
355
+ if (fieldMappings != null && fieldMappings .size () > 0 ) {
356
+ source .append ("\t private static String[] mappings = new String[] {\r \n " );
357
+ for (Iterator <String > itr = fieldMappings .keySet ().iterator (); itr .hasNext ();) {
358
+ String key = (String ) itr .next ();
359
+ String value = fieldMappings .get (key );
360
+ source .append ("\t \t \t \" " );
361
+ source .append (key );
362
+ source .append ("\" , \" " );
363
+ source .append (value );
364
+ source .append ("\" ,\r \n " );
365
+ }
366
+ source .append ("\t };\r \n " );
367
+
368
+ source .append ("\t private static Map<String, String> nameMappings = mappingFromArray(mappings, false);\r \n " );
369
+ source .append ("\t private static Map<String, String> aliasMappings = mappingFromArray(mappings, true);\r \n " );
370
+ }
371
+
344
372
for (int i = 0 ; i < clazzFields .length ; i ++) {
345
373
Field f = clazzFields [i ];
346
374
int modifiers = f .getModifiers ();
@@ -350,6 +378,9 @@ public int compare(Class<?> c1, Class<?> c2) {
350
378
if (type == int .class || type == long .class || type == short .class
351
379
|| type == byte .class || type == char .class || type == double .class
352
380
|| type == float .class || type == boolean .class || type == String .class ) {
381
+ if (!gotStaticFinalFields && fieldMappings != null && fieldMappings .size () > 0 ) {
382
+ source .append ("\r \n " );
383
+ }
353
384
source .append ("\t public static final " );
354
385
source .append (type .getSimpleName ());
355
386
source .append (" " );
@@ -423,6 +454,17 @@ public int compare(Class<?> c1, Class<?> c2) {
423
454
source .append ("\r \n " );
424
455
SourceUtils .insertLineComment (source , "\t " , index ++, true );
425
456
boolean moreCodesAdded = false ;
457
+ if (fieldMappings != null && fieldMappings .size () > 0 ) {
458
+ source .append ("\t @Override\r \n " );
459
+ source .append ("\t protected Map<String, String> fieldNameMapping() {\r \n " );
460
+ source .append ("\t \t return nameMappings;\r \n " );
461
+ source .append ("\t }\r \n \r \n " );
462
+ source .append ("\t @Override\r \n " );
463
+ source .append ("\t protected Map<String, String> fieldAliasMapping() {\r \n " );
464
+ source .append ("\t \t return aliasMappings;\r \n " );
465
+ source .append ("\t }\r \n \r \n " );
466
+ moreCodesAdded = true ;
467
+ }
426
468
if (s .bytesCompactMode ()) {
427
469
source .append ("\t public boolean bytesCompactMode() {\r \n " );
428
470
source .append ("\t \t return true;\r \n " );
0 commit comments