|
20 | 20 | import java.util.HashSet; |
21 | 21 | import java.util.Iterator; |
22 | 22 | import java.util.List; |
| 23 | +import java.util.Map; |
23 | 24 | import java.util.Set; |
24 | 25 |
|
25 | 26 | import net.sf.j2s.ajax.SimpleSerializable; |
@@ -597,6 +598,49 @@ public static String generateImplementation(SimpleSerializable s) { |
597 | 598 | source.append("- (NSMutableArray *) fields {\r\n"); |
598 | 599 | source.append("\tNSMutableArray *arr = [super fields];\r\n"); |
599 | 600 | SourceUtils.insertLineComment(source, "\t", index++, false); |
| 601 | + |
| 602 | + s.setSimpleVersion(SimpleSerializable.LATEST_SIMPLE_VERSION); |
| 603 | + String[] fieldMapping = s.fieldMapping(); |
| 604 | + if (fieldMapping != null) { |
| 605 | + Map<String, Field> allFields = SimpleSerializable.getSerializableFields(clazzName, clazz, false); |
| 606 | + for (Iterator<String> itr = allFields.keySet().iterator(); itr.hasNext();) { |
| 607 | + String name = itr.next(); |
| 608 | + boolean existed = false; |
| 609 | + for (int i = 0; i < fieldMapping.length / 2; i++) { |
| 610 | + String fName = fieldMapping[i + i]; |
| 611 | + //String sName = fieldMapping[i + i + 1]; |
| 612 | + if (fName.equals(name)) { |
| 613 | + existed = true; |
| 614 | + break; |
| 615 | + } |
| 616 | + } |
| 617 | + if (!existed) { |
| 618 | + System.err.println("[ERROR] Class " + clazzName + " field mappings does not contains field " + name); |
| 619 | + break; |
| 620 | + } |
| 621 | + } |
| 622 | + Set<String> names = new HashSet<String>(); |
| 623 | + for (int i = 0; i < fieldMapping.length / 2; i++) { |
| 624 | + String fName = fieldMapping[i + i]; |
| 625 | + String sName = fieldMapping[i + i + 1]; |
| 626 | + if (names.contains(sName)) { |
| 627 | + System.err.println("[ERROR] Class " + clazzName + " field mappings shorten name " + sName + " duplicatedd."); |
| 628 | + } |
| 629 | + names.add(sName); |
| 630 | + boolean existed = false; |
| 631 | + for (Iterator<String> itr = allFields.keySet().iterator(); itr.hasNext();) { |
| 632 | + String name = itr.next(); |
| 633 | + if (fName.equals(name)) { |
| 634 | + existed = true; |
| 635 | + break; |
| 636 | + } |
| 637 | + } |
| 638 | + if (!existed) { |
| 639 | + System.err.println("[ERROR] Class " + clazzName + " field mappings contains non-field " + fName); |
| 640 | + break; |
| 641 | + } |
| 642 | + } |
| 643 | + } |
600 | 644 | for (Iterator<Field> itr = fields.iterator(); itr.hasNext();) { |
601 | 645 | Field field = (Field) itr.next(); |
602 | 646 | String name = field.getName(); |
@@ -656,6 +700,18 @@ public static String generateImplementation(SimpleSerializable s) { |
656 | 700 | } else { |
657 | 701 | System.out.println("Unsupported type " + type); |
658 | 702 | } |
| 703 | + if (fieldMapping != null) { |
| 704 | + for (int i = 0; i < fieldMapping.length / 2; i++) { |
| 705 | + String fieldName = fieldMapping[i + i]; |
| 706 | + String fieldAlias = fieldMapping[i + i + 1]; |
| 707 | + if (name.equals(fieldName)) { |
| 708 | + source.append(" withAlias:@\""); |
| 709 | + source.append(fieldAlias); |
| 710 | + source.append("\""); |
| 711 | + break; |
| 712 | + } |
| 713 | + } |
| 714 | + } |
659 | 715 | source.append("]];\r\n"); |
660 | 716 | } |
661 | 717 | SourceUtils.insertLineComment(source, "\t", index++, false); |
@@ -826,6 +882,8 @@ public static void main(String[] args) { |
826 | 882 | source.append("}\r\n\r\n"); |
827 | 883 | SourceUtils.insertLineComment(source, "", index++, true); |
828 | 884 | source.append("- (id) createInstanceByClassName:(NSString *) className;\r\n"); |
| 885 | + source.append("- (NSString *) getClassShortenName:(NSString *) className;\r\n"); |
| 886 | + source.append("- (NSString *) getClassFullName:(NSString *) className;\r\n"); |
829 | 887 | source.append("\r\n"); |
830 | 888 | SourceUtils.insertLineComment(source, "", index++, true); |
831 | 889 | source.append("@end\r\n"); |
@@ -903,6 +961,71 @@ public static void main(String[] args) { |
903 | 961 | source.append("}\r\n"); |
904 | 962 | source.append("\r\n"); |
905 | 963 | SourceUtils.insertLineComment(source, "", index++, true); |
| 964 | + source.append("- (id) getClassShortenName:(NSString *) className {\r\n"); |
| 965 | + SourceUtils.insertLineComment(source, "\t", index++, false); |
| 966 | + |
| 967 | + for (int i = 1 + 4; i < args.length; i++) { |
| 968 | + String j2sSimpleClazz = args[i]; |
| 969 | + try { |
| 970 | + Class<?> clazz = Class.forName(j2sSimpleClazz); |
| 971 | + if (clazz.isInterface()) { |
| 972 | + continue; |
| 973 | + } |
| 974 | + Object inst = clazz.newInstance(); |
| 975 | + if (inst instanceof SimpleSerializable) { |
| 976 | + String shortenName = SimpleSerializable.getClassShortenName(j2sSimpleClazz); |
| 977 | + if (shortenName != null) { |
| 978 | + source.append("\tif ([className compare:@\""); |
| 979 | + source.append(j2sSimpleClazz); |
| 980 | + source.append("\"] == 0) {\r\n"); |
| 981 | + source.append("\t\treturn @\""); |
| 982 | + source.append(shortenName); |
| 983 | + source.append("\";\r\n"); |
| 984 | + source.append("\t}\r\n"); |
| 985 | + } |
| 986 | + } |
| 987 | + } catch (Throwable e) { |
| 988 | + e.printStackTrace(); |
| 989 | + } |
| 990 | + } |
| 991 | + SourceUtils.insertLineComment(source, "\t", index++, false); |
| 992 | + source.append("\treturn nil;\r\n"); |
| 993 | + source.append("}\r\n"); |
| 994 | + source.append("\r\n"); |
| 995 | + SourceUtils.insertLineComment(source, "", index++, true); |
| 996 | + source.append("- (id) getClassFullName:(NSString *) className {\r\n"); |
| 997 | + SourceUtils.insertLineComment(source, "\t", index++, false); |
| 998 | + |
| 999 | + for (int i = 1 + 4; i < args.length; i++) { |
| 1000 | + String j2sSimpleClazz = args[i]; |
| 1001 | + try { |
| 1002 | + Class<?> clazz = Class.forName(j2sSimpleClazz); |
| 1003 | + if (clazz.isInterface()) { |
| 1004 | + continue; |
| 1005 | + } |
| 1006 | + Object inst = clazz.newInstance(); |
| 1007 | + if (inst instanceof SimpleSerializable) { |
| 1008 | + String shortenName = SimpleSerializable.getClassShortenName(j2sSimpleClazz); |
| 1009 | + if (shortenName != null) { |
| 1010 | + source.append("\tif ([className compare:@\""); |
| 1011 | + source.append(shortenName); |
| 1012 | + source.append("\"] == 0) {\r\n"); |
| 1013 | + source.append("\t\treturn @\""); |
| 1014 | + source.append(j2sSimpleClazz); |
| 1015 | + source.append("\";\r\n"); |
| 1016 | + source.append("\t}\r\n"); |
| 1017 | + } |
| 1018 | + } |
| 1019 | + } catch (Throwable e) { |
| 1020 | + e.printStackTrace(); |
| 1021 | + } |
| 1022 | + } |
| 1023 | + SourceUtils.insertLineComment(source, "\t", index++, false); |
| 1024 | + source.append("\treturn nil;\r\n"); |
| 1025 | + source.append("}\r\n"); |
| 1026 | + source.append("\r\n"); |
| 1027 | + SourceUtils.insertLineComment(source, "", index++, true); |
| 1028 | + |
906 | 1029 | source.append("@end\r\n"); |
907 | 1030 |
|
908 | 1031 | SourceUtils.updateSourceContent(new File(targetFolder, simpleClazzName + ".m"), source.toString()); |
|
0 commit comments