2222import java .util .HashSet ;
2323import java .util .Iterator ;
2424import java .util .List ;
25+ import java .util .Map ;
2526import java .util .Set ;
2627
2728import net .sf .j2s .ajax .SimpleSerializable ;
@@ -192,6 +193,16 @@ public static String generateSourceFromObject(SimpleSerializable s) {
192193
193194 boolean hasMoreImports = false ;
194195 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+ }
195206
196207 Type [] interfaces = s .getClass ().getGenericInterfaces ();
197208 if (interfaces != null && interfaces .length > 0 ) {
@@ -341,6 +352,23 @@ public int compare(Class<?> c1, Class<?> c2) {
341352 source .append ("{\r \n \r \n " );
342353 SourceUtils .insertLineComment (source , "\t " , index ++, true );
343354
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+
344372 for (int i = 0 ; i < clazzFields .length ; i ++) {
345373 Field f = clazzFields [i ];
346374 int modifiers = f .getModifiers ();
@@ -350,6 +378,9 @@ public int compare(Class<?> c1, Class<?> c2) {
350378 if (type == int .class || type == long .class || type == short .class
351379 || type == byte .class || type == char .class || type == double .class
352380 || type == float .class || type == boolean .class || type == String .class ) {
381+ if (!gotStaticFinalFields && fieldMappings != null && fieldMappings .size () > 0 ) {
382+ source .append ("\r \n " );
383+ }
353384 source .append ("\t public static final " );
354385 source .append (type .getSimpleName ());
355386 source .append (" " );
@@ -423,6 +454,17 @@ public int compare(Class<?> c1, Class<?> c2) {
423454 source .append ("\r \n " );
424455 SourceUtils .insertLineComment (source , "\t " , index ++, true );
425456 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+ }
426468 if (s .bytesCompactMode ()) {
427469 source .append ("\t public boolean bytesCompactMode() {\r \n " );
428470 source .append ("\t \t return true;\r \n " );
0 commit comments