@@ -194,6 +194,7 @@ public static String generateSourceFromObject(SimpleSerializable s) {
194194 boolean hasMoreImports = false ;
195195 Set <String > importedClasses = new HashSet <String >();
196196
197+ s .setSimpleVersion (SimpleSerializable .LATEST_SIMPLE_VERSION );
197198 Map <String , String > fieldMappings = s .fieldNameMapping ();
198199 if (fieldMappings != null && fieldMappings .size () > 0 ) {
199200 hasMoreImports = true ;
@@ -352,6 +353,47 @@ public int compare(Class<?> c1, Class<?> c2) {
352353 source .append ("{\r \n \r \n " );
353354 SourceUtils .insertLineComment (source , "\t " , index ++, true );
354355
356+ String [] fieldMapping = s .fieldMapping ();
357+ if (fieldMapping != null ) {
358+ Map <String , Field > allFields = SimpleSerializable .getSerializableFields (clazzName , clazz , false );
359+ for (Iterator <String > itr = allFields .keySet ().iterator (); itr .hasNext ();) {
360+ String name = itr .next ();
361+ boolean existed = false ;
362+ for (int i = 0 ; i < fieldMapping .length / 2 ; i ++) {
363+ String fName = fieldMapping [i + i ];
364+ //String sName = fieldMapping[i + i + 1];
365+ if (fName .equals (name )) {
366+ existed = true ;
367+ break ;
368+ }
369+ }
370+ if (!existed ) {
371+ System .err .println ("[ERROR] Class " + clazzName + " field mappings does not contains field " + name );
372+ break ;
373+ }
374+ }
375+ Set <String > names = new HashSet <String >();
376+ for (int i = 0 ; i < fieldMapping .length / 2 ; i ++) {
377+ String fName = fieldMapping [i + i ];
378+ String sName = fieldMapping [i + i + 1 ];
379+ if (names .contains (sName )) {
380+ System .err .println ("[ERROR] Class " + clazzName + " field mappings shorten name " + sName + " duplicatedd." );
381+ }
382+ names .add (sName );
383+ boolean existed = false ;
384+ for (Iterator <String > itr = allFields .keySet ().iterator (); itr .hasNext ();) {
385+ String name = itr .next ();
386+ if (fName .equals (name )) {
387+ existed = true ;
388+ break ;
389+ }
390+ }
391+ if (!existed ) {
392+ System .err .println ("[ERROR] Class " + clazzName + " field mappings contains non-field " + fName );
393+ break ;
394+ }
395+ }
396+ }
355397 if (fieldMappings != null && fieldMappings .size () > 0 ) {
356398 source .append ("\t private static String[] mappings = new String[] {\r \n " );
357399 for (Iterator <String > itr = fieldMappings .keySet ().iterator (); itr .hasNext ();) {
@@ -369,8 +411,26 @@ public int compare(Class<?> c1, Class<?> c2) {
369411 source .append ("\t private static Map<String, String> aliasMappings = mappingFromArray(mappings, true);\r \n " );
370412 }
371413
414+ Set <String > j2sIgnoredFileds = new HashSet <String >();
415+
416+ for (int i = 0 ; i < clazzFields .length ; i ++) {
417+ Field f = clazzFields [i ];
418+ int modifiers = f .getModifiers ();
419+ if ((modifiers & (Modifier .PUBLIC /* | Modifier.PROTECTED*/ )) != 0
420+ && (modifiers & (Modifier .TRANSIENT | Modifier .STATIC )) == 0 ) {
421+ fields .add (f );
422+ if ((modifiers & Modifier .PROTECTED ) != 0 ) {
423+ j2sIgnoredFileds .add (f .getName ());
424+ }
425+ }
426+ }
372427 for (int i = 0 ; i < clazzFields .length ; i ++) {
373428 Field f = clazzFields [i ];
429+ String name = f .getName ();
430+ if (j2sIgnoredFileds .contains (name )) {
431+ System .out .println ("Ignoring ..." + name );
432+ continue ;
433+ }
374434 int modifiers = f .getModifiers ();
375435 if ((modifiers & (Modifier .PUBLIC /* | Modifier.PROTECTED*/ )) != 0
376436 && (modifiers & Modifier .STATIC ) != 0 && (modifiers & Modifier .FINAL ) != 0 ) {
@@ -558,9 +618,10 @@ public int compare(Class<?> c1, Class<?> c2) {
558618 /**
559619 * @param args
560620 */
621+ @ SuppressWarnings ("deprecation" )
561622 public static void main (String [] args ) {
562- if (args == null || args .length < 3 ) {
563- System .out .println ("Usage: " + SimpleSource4Java .class .getName () + " <sources folder> <author> <orgization or company> <class> [class ...]" );
623+ if (args == null || args .length < 4 ) {
624+ System .out .println ("Usage: " + SimpleSource4Java .class .getName () + " <sources folder> <author> <orgization or company> <mapping> < class> [class ...]" );
564625 return ;
565626 }
566627 String targetFolder = args [0 ];
@@ -580,8 +641,9 @@ public static void main(String[] args) {
580641 folder = f .getName ();
581642 author = args [1 ];
582643 company = args [2 ];
644+ String mappingClass = args [3 ];
583645
584- for (int i = 1 + 2 ; i < args .length ; i ++) {
646+ for (int i = 1 + 2 + 1 ; i < args .length ; i ++) {
585647 String j2sSimpleClazz = args [i ];
586648 try {
587649 Class <?> clazz = Class .forName (j2sSimpleClazz );
@@ -598,7 +660,7 @@ public static void main(String[] args) {
598660 }
599661 }
600662
601- for (int i = 1 + 2 ; i < args .length ; i ++) {
663+ for (int i = 1 + 2 + 1 ; i < args .length ; i ++) {
602664 String j2sSimpleClazz = args [i ];
603665 try {
604666 Class <?> clazz = Class .forName (j2sSimpleClazz );
@@ -663,6 +725,101 @@ public static void main(String[] args) {
663725 e .printStackTrace ();
664726 }
665727 }
728+
729+ {
730+ String clazzName = mappingClass ;
731+ StringBuffer source = new StringBuffer ();
732+ Date date = new Date ();
733+ source .append ("/**\r \n " );
734+ source .append (" * Generated by Java2Script.\r \n " );
735+ source .append (" * Copyright (c) " );
736+ source .append (date .getYear () + 1900 );
737+ source .append (" " );
738+ source .append (company );
739+ source .append (". All rights reserved.\r \n " );
740+ source .append (" */\r \n " );
741+ source .append ("\r \n " );
742+
743+ int index = 0 ;
744+ SourceUtils .insertLineComment (source , "" , index ++, true );
745+
746+ String simpleClazzName = clazzName ;
747+ int idx = clazzName .lastIndexOf ('.' );
748+ if (idx != -1 ) {
749+ source .append ("package " );
750+ source .append (simpleClazzName .substring (0 , idx ));
751+ source .append (";\r \n " );
752+ source .append ("\r \n " );
753+
754+ simpleClazzName = clazzName .substring (idx + 1 );
755+ }
756+
757+ SourceUtils .insertLineComment (source , "" , index ++, true );
758+ source .append ("import net.sf.j2s.ajax.SimpleSerializable;\r \n " );
759+ source .append ("\r \n " );
760+
761+ SourceUtils .insertLineComment (source , "" , index ++, true );
762+ source .append ("public class " );
763+ source .append (simpleClazzName );
764+ SourceUtils .insertBlockComment (source , index ++);
765+ source .append ("{\r \n " );
766+ source .append ("\r \n " );
767+ SourceUtils .insertLineComment (source , "\t " , index ++, true );
768+
769+ source .append ("\t public static void initializeMappings() {\r \n " );
770+ SourceUtils .insertLineComment (source , "\t \t " , index ++, false );
771+ for (int i = 1 + 2 + 1 ; i < args .length ; i ++) {
772+ String j2sSimpleClazz = args [i ];
773+ try {
774+ Class <?> clazz = Class .forName (j2sSimpleClazz );
775+ if (clazz .isInterface ()) {
776+ continue ;
777+ }
778+ Object inst = clazz .newInstance ();
779+ if (inst instanceof SimpleSerializable ) {
780+ String shortenName = SimpleSerializable .getClassShortenName (j2sSimpleClazz );
781+ if (shortenName != null && shortenName .length () > 0 ) {
782+ source .append ("\t \t SimpleSerializable.registerClassShortenName(\" " );
783+ source .append (j2sSimpleClazz );
784+ source .append ("\" , \" " );
785+ source .append (shortenName );
786+ source .append ("\" );\r \n " );
787+ }
788+ }
789+ } catch (Throwable e ) {
790+ e .printStackTrace ();
791+ }
792+ }
793+ SourceUtils .insertLineComment (source , "\t \t " , index ++, false );
794+ source .append ("\t }\r \n " );
795+
796+ source .append ("\r \n " );
797+ SourceUtils .insertLineComment (source , "\t " , index ++, true );
798+
799+ source .append ("}\r \n " );
800+
801+ String simpleName = mappingClass ;
802+ String packageName = null ;
803+ idx = mappingClass .lastIndexOf ('.' );
804+ if (idx != -1 ) {
805+ packageName = mappingClass .substring (0 , idx );
806+ packageName = packageName .replace ('.' , File .separatorChar );
807+ simpleName = mappingClass .substring (idx + 1 );
808+ }
809+ if (packageName != null ) {
810+ if (targetFolder .endsWith (File .separator )) {
811+ targetFolder = targetFolder + packageName ;
812+ } else {
813+ targetFolder = targetFolder + File .separator + packageName ;
814+ }
815+ File folder = new File (targetFolder );
816+ if (!folder .exists ()) {
817+ folder .mkdirs ();
818+ }
819+ }
820+ File javaFile = new File (targetFolder , simpleName + ".java" );
821+ SourceUtils .updateSourceContent (javaFile , source .toString ());
822+ }
666823
667824 }
668825
0 commit comments