Skip to content

Commit 700df88

Browse files
author
zhourenjian@gmail.com
committed
Support generate field mappings Java generated Java sources
1 parent 47f54e9 commit 700df88

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashSet;
2323
import java.util.Iterator;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.Set;
2627

2728
import 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("\tprivate 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("\tprivate static Map<String, String> nameMappings = mappingFromArray(mappings, false);\r\n");
369+
source.append("\tprivate 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("\tpublic 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("\tprotected Map<String, String> fieldNameMapping() {\r\n");
460+
source.append("\t\treturn nameMappings;\r\n");
461+
source.append("\t}\r\n\r\n");
462+
source.append("\t@Override\r\n");
463+
source.append("\tprotected Map<String, String> fieldAliasMapping() {\r\n");
464+
source.append("\t\treturn aliasMappings;\r\n");
465+
source.append("\t}\r\n\r\n");
466+
moreCodesAdded = true;
467+
}
426468
if (s.bytesCompactMode()) {
427469
source.append("\tpublic boolean bytesCompactMode() {\r\n");
428470
source.append("\t\treturn true;\r\n");

0 commit comments

Comments
 (0)