Skip to content

Commit ca1351b

Browse files
author
zhourenjian
committed
Support class and field mappings in generating Objective-C sources
Fix bug of serializing object fields Change API name from registerClassShortName to registerShortenClassName
1 parent b548c08 commit ca1351b

File tree

3 files changed

+150
-4
lines changed

3 files changed

+150
-4
lines changed

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static interface IHttpPipeRequestFactory extends IHttpRequestFactory {
4343
* @j2sNative
4444
*/
4545
static {
46-
SimpleSerializable.registerShortClassName(SimplePipeSequence.class.getName(), "SPS");
46+
SimpleSerializable.registerClassShortenName(SimplePipeSequence.class.getName(), "SPS");
4747
}
4848

4949
@J2SIgnore

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleSerializable.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,35 @@ public DeserializeObject(Object object, int index, boolean error) {
9696
};
9797

9898
@J2SIgnore
99-
public static void registerShortClassName(String clazzName, String shortName) {
99+
public static void registerClassShortenName(String clazzName, String shortenName) {
100+
if (shortenName == null || shortenName.length() == 0) {
101+
System.out.println("Invalid shorten class name for " + clazzName);
102+
return;
103+
}
104+
String sName = classMappings.get(clazzName);
105+
if (sName != null && !sName.equals(shortenName)) {
106+
System.out.println("Already existed shorten name " + sName + " for " + clazzName);
107+
}
108+
String fName = rClassMappings.get(shortenName);
109+
if (fName != null && !fName.equals(clazzName)) {
110+
System.out.println("Conficted: shorten name " + shortenName + " for " + fName + " and " + clazzName);
111+
}
100112
synchronized (classMutex) {
101-
classMappings.put(clazzName, shortName);
102-
rClassMappings.put(shortName, clazzName);
113+
classMappings.put(clazzName, shortenName);
114+
rClassMappings.put(shortenName, clazzName);
103115
}
104116
}
105117

118+
@J2SIgnore
119+
public static String getClassShortenName(String clazzName) {
120+
return classMappings.get(clazzName);
121+
}
122+
123+
@J2SIgnore
124+
public static String getClassFullName(String clazzName) {
125+
return rClassMappings.get(clazzName);
126+
}
127+
106128
@J2SIgnore
107129
static Map<String, Field> getSerializableFields(String clazzName, Class<?> clazz, boolean forceUpdate) {
108130
Map<String, Field> fields = forceUpdate ? null : quickFields.get(clazzName);
@@ -1199,6 +1221,7 @@ private void serializeObject(StringBuffer buffer, SimpleSerializable ss, List<Si
11991221
}
12001222
buffer.append('O');
12011223
if (ss != null) {
1224+
ss.simpleVersion = simpleVersion;
12021225
String s = ss.serialize(null, ssObjs);
12031226
int l4 = s.length();
12041227
if (l4 > 52) {

sources/net.sf.j2s.ajax/generator/net/sf/j2s/ajax/SimpleSource4ObjectiveC.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashSet;
2121
import java.util.Iterator;
2222
import java.util.List;
23+
import java.util.Map;
2324
import java.util.Set;
2425

2526
import net.sf.j2s.ajax.SimpleSerializable;
@@ -597,6 +598,49 @@ public static String generateImplementation(SimpleSerializable s) {
597598
source.append("- (NSMutableArray *) fields {\r\n");
598599
source.append("\tNSMutableArray *arr = [super fields];\r\n");
599600
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+
}
600644
for (Iterator<Field> itr = fields.iterator(); itr.hasNext();) {
601645
Field field = (Field) itr.next();
602646
String name = field.getName();
@@ -656,6 +700,18 @@ public static String generateImplementation(SimpleSerializable s) {
656700
} else {
657701
System.out.println("Unsupported type " + type);
658702
}
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+
}
659715
source.append("]];\r\n");
660716
}
661717
SourceUtils.insertLineComment(source, "\t", index++, false);
@@ -826,6 +882,8 @@ public static void main(String[] args) {
826882
source.append("}\r\n\r\n");
827883
SourceUtils.insertLineComment(source, "", index++, true);
828884
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");
829887
source.append("\r\n");
830888
SourceUtils.insertLineComment(source, "", index++, true);
831889
source.append("@end\r\n");
@@ -903,6 +961,71 @@ public static void main(String[] args) {
903961
source.append("}\r\n");
904962
source.append("\r\n");
905963
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+
9061029
source.append("@end\r\n");
9071030

9081031
SourceUtils.updateSourceContent(new File(targetFolder, simpleClazzName + ".m"), source.toString());

0 commit comments

Comments
 (0)