Skip to content

Commit 48e6138

Browse files
author
zhourenjian@gmail.com
committed
Support generating protocol based array
Optimize merging old sources and generated sources
1 parent cbd13ac commit 48e6138

2 files changed

Lines changed: 157 additions & 46 deletions

File tree

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static String generateHeaderFromInterface(Class<?> interfaceClazz) {
148148
}
149149
} else if (type == String.class) {
150150
try {
151-
source.append("\"" + f.get(interfaceClazz) + "\"");
151+
source.append("@\"" + f.get(interfaceClazz) + "\"");
152152
} catch (Throwable e) {
153153
e.printStackTrace();
154154
}
@@ -340,7 +340,7 @@ public static String generateHeaderFromObject(SimpleSerializable s) {
340340
}
341341
} else if (type == String.class) {
342342
try {
343-
source.append("\"" + f.get(s.getClass()) + "\"");
343+
source.append("@\"" + f.get(s.getClass()) + "\"");
344344
} catch (Throwable e) {
345345
e.printStackTrace();
346346
}
@@ -357,6 +357,14 @@ public static String generateHeaderFromObject(SimpleSerializable s) {
357357
}
358358
source.append("//+$1+\r\n//-$1-\r\n\r\n");
359359

360+
if (s instanceof SimpleSerializable && !(s instanceof SimpleRPCRunnable)) {
361+
source.append("@protocol ");
362+
source.append(simpleClazzName);
363+
source.append("\r\n\r\n");
364+
source.append("//+$8+\r\n//-$8-\r\n\r\n");
365+
source.append("@end\r\n\r\n");
366+
source.append("//+$9+\r\n//-$9-\r\n\r\n");
367+
}
360368
source.append("@interface ");
361369
source.append(simpleClazzName);
362370
source.append(" : ");
@@ -396,14 +404,23 @@ public static String generateHeaderFromObject(SimpleSerializable s) {
396404
} else if (type == byte[].class) {
397405
if (s.bytesCompactMode()) {
398406
source.append("NSData *");
407+
} else {
408+
source.append("NSMutableArray<NSNumber> *");
409+
}
410+
} else if (type == String[].class) {
411+
source.append("NSMutableArray<NSString> *");
412+
} else if (SimpleSerializable.isSubclassOf(type, SimpleSerializable[].class)) {
413+
if (!SimpleSerializable.isSubclassOf(type, SimpleRPCRunnable[].class)) {
414+
source.append("NSMutableArray<");
415+
source.append(type.getComponentType().getSimpleName());
416+
source.append("> *");
399417
} else {
400418
source.append("NSMutableArray *");
401419
}
402420
} else if (type == int[].class || type == long[].class || type == double[].class
403421
|| type == short[].class || type == char[].class
404-
|| type == float[].class || type == boolean[].class || type == String[].class
405-
|| SimpleSerializable.isSubclassOf(type, SimpleSerializable[].class)) {
406-
source.append("NSMutableArray *");
422+
|| type == float[].class || type == boolean[].class) {
423+
source.append("NSMutableArray<NSNumber> *");
407424
} else {
408425
System.out.println("Unsupported type " + type);
409426
}
@@ -448,14 +465,24 @@ public static String generateHeaderFromObject(SimpleSerializable s) {
448465
} else if (type == byte[].class) {
449466
if (s.bytesCompactMode()) {
450467
source.append(", retain) NSData *");
468+
} else {
469+
source.append(", retain) NSMutableArray<NSNumber> *");
470+
}
471+
} else if (type == String[].class) {
472+
source.append(", retain) NSMutableArray<NSString> *");
473+
} else if (SimpleSerializable.isSubclassOf(type, SimpleSerializable[].class)) {
474+
if (!SimpleSerializable.isSubclassOf(type, SimpleRPCRunnable[].class)) {
475+
source.append(", retain) NSMutableArray<");
476+
source.append(type.getComponentType().getSimpleName());
477+
source.append("> *");
451478
} else {
452479
source.append(", retain) NSMutableArray *");
453480
}
454481
} else if (type == int[].class || type == long[].class || type == double[].class
455482
|| type == short[].class || type == char[].class
456483
|| type == float[].class || type == boolean[].class || type == String[].class
457484
|| SimpleSerializable.isSubclassOf(type, SimpleSerializable[].class)) {
458-
source.append(", retain) NSMutableArray *");
485+
source.append(", retain) NSMutableArray<NSNumber> *");
459486
} else {
460487
System.out.println("Unsupported type " + type);
461488
}
@@ -722,6 +749,7 @@ public static void main(String[] args) {
722749
System.out.println(new File(targetFolder, simpleName + ".m").getAbsolutePath());
723750
}
724751
} catch (Throwable e) {
752+
System.out.println("Failed to generate source for " + j2sSimpleClazz);
725753
e.printStackTrace();
726754
}
727755
}

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

Lines changed: 123 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,72 @@
55
import java.io.FileInputStream;
66
import java.io.FileOutputStream;
77
import java.io.IOException;
8+
import java.util.ArrayList;
89
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.List;
912
import java.util.Map;
13+
import java.util.regex.Matcher;
14+
import java.util.regex.Pattern;
1015

1116
public class SourceUtils {
1217

18+
public static void insertLineComment(StringBuffer source, String indent, int index, boolean blankLine) {
19+
source.append(indent);
20+
source.append("//+$");
21+
source.append(index);
22+
source.append("+\r\n");
23+
source.append(indent);
24+
source.append("//-$");
25+
source.append(index);
26+
source.append("-\r\n");
27+
if (blankLine) {
28+
source.append("\r\n");
29+
}
30+
}
31+
32+
public static void wrapALineWithLineComment(StringBuffer source, String indent, int index, String line, boolean blankLine) {
33+
source.append(indent);
34+
source.append("//+$");
35+
source.append(index);
36+
source.append("+\r\n");
37+
source.append(indent);
38+
source.append(line);
39+
source.append("\r\n");
40+
source.append(indent);
41+
source.append("//-$");
42+
source.append(index);
43+
source.append("-\r\n");
44+
if (blankLine) {
45+
source.append("\r\n");
46+
}
47+
}
48+
49+
public static void openLineComment(StringBuffer source, String indent, int index) {
50+
source.append(indent);
51+
source.append("//+$");
52+
source.append(index);
53+
source.append("+\r\n");
54+
}
55+
56+
public static void closeLineComment(StringBuffer source, String indent, int index, boolean blankLine) {
57+
source.append(indent);
58+
source.append("//-$");
59+
source.append(index);
60+
source.append("-\r\n");
61+
if (blankLine) {
62+
source.append("\r\n");
63+
}
64+
}
65+
66+
public static void insertBlockComment(StringBuffer source, int index) {
67+
source.append(" /*+$");
68+
source.append(index);
69+
source.append("+*/ /*-$");
70+
source.append(index);
71+
source.append("-*/ ");
72+
}
73+
1374
public static void updateSourceContent(File file, String source) {
1475
FileInputStream fis = null;
1576
byte[] buffer = new byte[8096];
@@ -32,68 +93,90 @@ public static void updateSourceContent(File file, String source) {
3293
}
3394
}
3495
String oldSource = baos.toString();
35-
Map<Integer, String> map = new HashMap<Integer, String>();
36-
for (int i = 0; i < 100; i++) {
37-
boolean got = false;
38-
String key1 = "//+$" + i + "+";
39-
int idx1 = oldSource.indexOf(key1);
40-
if (idx1 != -1) {
41-
String key2 = "//-$" + i + "-";
42-
int idx2 = oldSource.indexOf(key2);
43-
if (idx2 != -1) {
44-
String s = oldSource.substring(idx1 + key1.length(), idx2);
45-
map.put(i, s);
46-
got = true;
47-
}
96+
if (oldSource.length() > 0) {
97+
Pattern regExp = Pattern.compile("\\/[\\/|\\*]\\+\\$(\\d+)\\+");
98+
Matcher matcher = regExp.matcher(source);
99+
List<String> allIndexes = new ArrayList<String>();
100+
while (matcher.find()) {
101+
String indexStr = matcher.group(1);
102+
allIndexes.add(indexStr);
48103
}
49-
if (!got) {
50-
key1 = "/*+$" + i + "+*/";
51-
idx1 = oldSource.indexOf(key1);
104+
int position = 0;
105+
Map<String, String> map = new HashMap<String, String>();
106+
for (Iterator<String> itr = allIndexes.iterator(); itr.hasNext();) {
107+
String i = (String) itr.next();
108+
boolean got = false;
109+
String key1 = "//+$" + i + "+";
110+
int idx1 = oldSource.indexOf(key1, position);
52111
if (idx1 != -1) {
53-
String key2 = "/*-$" + i + "-*/";
54-
int idx2 = oldSource.indexOf(key2);
112+
String key2 = "//-$" + i + "-";
113+
int idx2 = oldSource.indexOf(key2, position + key1.length());
55114
if (idx2 != -1) {
56115
String s = oldSource.substring(idx1 + key1.length(), idx2);
57116
map.put(i, s);
58117
got = true;
118+
position = idx2 + key2.length();
59119
}
60120
}
61-
}
62-
}
63-
for (int i = 0; i < 100; i++) {
64-
boolean changed = false;
65-
String key1 = "//+$" + i + "+";
66-
int idx1 = source.indexOf(key1);
67-
if (idx1 != -1) {
68-
String key2 = "//-$" + i + "-";
69-
int idx2 = source.indexOf(key2);
70-
if (idx2 != -1) {
71-
String ss = source.substring(idx1 + key1.length(), idx2);
72-
String s = map.get(i);
73-
if (s != null && !s.equals(ss)) {
74-
source = source.substring(0, idx1 + key1.length()) + s + source.substring(idx2);
75-
changed = true;
121+
if (!got) {
122+
key1 = "/*+$" + i + "+*/";
123+
idx1 = oldSource.indexOf(key1, position);
124+
if (idx1 != -1) {
125+
String key2 = "/*-$" + i + "-*/";
126+
int idx2 = oldSource.indexOf(key2, position + key1.length());
127+
if (idx2 != -1) {
128+
String s = oldSource.substring(idx1 + key1.length(), idx2);
129+
map.put(i, s);
130+
got = true;
131+
position = idx2 + key2.length();
132+
}
76133
}
77134
}
78135
}
79-
if (!changed) {
80-
key1 = "/*+$" + i + "+*/";
81-
idx1 = source.indexOf(key1);
136+
position = 0;
137+
for (Iterator<String> itr = allIndexes.iterator(); itr.hasNext();) {
138+
String i = (String) itr.next();
139+
boolean changed = false;
140+
String key1 = "//+$" + i + "+";
141+
int idx1 = source.indexOf(key1, position);
82142
if (idx1 != -1) {
83-
String key2 = "/*-$" + i + "-*/";
84-
int idx2 = source.indexOf(key2);
143+
String key2 = "//-$" + i + "-";
144+
int idx2 = source.indexOf(key2, position + key1.length());
85145
if (idx2 != -1) {
86146
String ss = source.substring(idx1 + key1.length(), idx2);
87147
String s = map.get(i);
88-
if (s != null && !s.equals(ss)) {
148+
if (s != null && !s.equals(ss)
149+
&& !(s.trim().length() == 0 && ss.trim().length() == 0)) {
89150
source = source.substring(0, idx1 + key1.length()) + s + source.substring(idx2);
90151
changed = true;
91152
}
153+
position = idx2 + key2.length();
92154
}
93155
}
156+
if (!changed) {
157+
key1 = "/*+$" + i + "+*/";
158+
idx1 = source.indexOf(key1, position);
159+
if (idx1 != -1) {
160+
String key2 = "/*-$" + i + "-*/";
161+
int idx2 = source.indexOf(key2, position + key1.length());
162+
if (idx2 != -1) {
163+
String ss = source.substring(idx1 + key1.length(), idx2);
164+
String s = map.get(i);
165+
if (s != null && !s.equals(ss)
166+
&& !(s.trim().length() == 0 && ss.trim().length() == 0)) {
167+
source = source.substring(0, idx1 + key1.length()) + s + source.substring(idx2);
168+
changed = true;
169+
}
170+
}
171+
position = idx2 + key2.length();
172+
}
173+
}
174+
}
175+
176+
if (source.equals(oldSource)) {
177+
return;
94178
}
95179
}
96-
97180
FileOutputStream fos = null;
98181
try {
99182
fos = new FileOutputStream(file);

0 commit comments

Comments
 (0)