Skip to content

Commit bc30e45

Browse files
committed
Merge branch 'caoli5288-pull/fix-86'
2 parents 435e4f9 + 58fb2a8 commit bc30e45

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

src/main/java/com/jsoniter/spi/ClassDescriptor.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.lang.reflect.*;
44
import java.util.*;
55

6+
import static java.lang.reflect.Modifier.isTransient;
7+
68
public class ClassDescriptor {
79

810
public ClassInfo classInfo;
@@ -30,9 +32,9 @@ public static ClassDescriptor getDecodingClassDescriptor(ClassInfo classInfo, bo
3032
desc.clazz = clazz;
3133
desc.lookup = lookup;
3234
desc.ctor = getCtor(clazz);
33-
desc.fields = getFields(lookup, classInfo, includingPrivate);
3435
desc.setters = getSetters(lookup, classInfo, includingPrivate);
3536
desc.getters = new ArrayList<Binding>();
37+
desc.fields = getFields(lookup, classInfo, includingPrivate);
3638
desc.bindingTypeWrappers = new ArrayList<WrapperDescriptor>();
3739
desc.keyValueTypeWrappers = new ArrayList<Method>();
3840
desc.unwrappers = new ArrayList<UnwrapperDescriptor>();
@@ -111,6 +113,7 @@ public static ClassDescriptor getEncodingClassDescriptor(ClassInfo classInfo, bo
111113
return desc;
112114
}
113115

116+
// TODO: do not remove, set fromNames to []
114117
private static void decodingDeduplicate(ClassDescriptor desc) {
115118
HashMap<String, Binding> byName = new HashMap<String, Binding>();
116119
for (Binding field : desc.fields) {
@@ -178,6 +181,7 @@ private static void decodingDeduplicate(ClassDescriptor desc) {
178181
}
179182
}
180183

184+
// TODO: do not remove, set toNames to []
181185
private static void encodingDeduplicate(ClassDescriptor desc) {
182186
HashMap<String, Binding> byName = new HashMap<String, Binding>();
183187
for (Binding field : desc.fields) {
@@ -188,7 +192,6 @@ private static void encodingDeduplicate(ClassDescriptor desc) {
188192
byName.put(toName, field);
189193
}
190194
}
191-
192195
for (Binding getter : new ArrayList<Binding>(desc.getters)) {
193196
for (String toName : getter.toNames) {
194197
Binding existing = byName.get(toName);
@@ -229,16 +232,17 @@ private static List<Binding> getFields(Map<String, Type> lookup, ClassInfo class
229232
if (Modifier.isStatic(field.getModifiers())) {
230233
continue;
231234
}
232-
if (Modifier.isTransient(field.getModifiers())) {
233-
continue;
234-
}
235235
if (!includingPrivate && !Modifier.isPublic(field.getType().getModifiers())) {
236236
continue;
237237
}
238238
if (includingPrivate) {
239239
field.setAccessible(true);
240240
}
241241
Binding binding = createBindingFromField(lookup, classInfo, field);
242+
if (isTransient(field.getModifiers())) {
243+
binding.toNames = new String[0];
244+
binding.fromNames = new String[0];
245+
}
242246
bindings.add(binding);
243247
}
244248
return bindings;
@@ -296,12 +300,22 @@ private static List<Binding> getSetters(Map<String, Type> lookup, ClassInfo clas
296300
}
297301
try {
298302
String fromName = translateSetterName(methodName);
299-
Binding binding = new Binding(classInfo, lookup, paramTypes[0]);
300-
binding.fromNames = new String[]{fromName};
301-
binding.name = fromName;
302-
binding.method = method;
303-
binding.annotations = method.getAnnotations();
304-
setters.add(binding);
303+
Field field = null;
304+
try {
305+
field = method.getDeclaringClass().getDeclaredField(fromName);
306+
} catch (NoSuchFieldException e) {
307+
// ignore
308+
}
309+
Binding setter = new Binding(classInfo, lookup, paramTypes[0]);
310+
if (field != null && isTransient(field.getModifiers())) {
311+
setter.fromNames = new String[0];
312+
} else {
313+
setter.fromNames = new String[]{fromName};
314+
}
315+
setter.name = fromName;
316+
setter.method = method;
317+
setter.annotations = method.getAnnotations();
318+
setters.add(setter);
305319
} catch (Exception e) {
306320
throw new JsonException("failed to create binding from setter: " + method, e);
307321
}
@@ -353,11 +367,21 @@ private static List<Binding> getGetters(Map<String, Type> lookup, ClassInfo clas
353367
continue;
354368
}
355369
String toName = methodName.substring("get".length());
356-
char[] fromNameChars = toName.toCharArray();
357-
fromNameChars[0] = Character.toLowerCase(fromNameChars[0]);
358-
toName = new String(fromNameChars);
370+
char[] toNameChars = toName.toCharArray();
371+
toNameChars[0] = Character.toLowerCase(toNameChars[0]);
372+
toName = new String(toNameChars);
359373
Binding getter = new Binding(classInfo, lookup, method.getGenericReturnType());
360-
getter.toNames = new String[]{toName};
374+
Field field = null;
375+
try {
376+
field = method.getDeclaringClass().getDeclaredField(toName);
377+
} catch (NoSuchFieldException e) {
378+
// ignore
379+
}
380+
if (field != null && isTransient(field.getModifiers())) {
381+
getter.toNames = new String[0];
382+
} else {
383+
getter.toNames = new String[]{toName};
384+
}
361385
getter.name = toName;
362386
getter.method = method;
363387
getter.annotations = method.getAnnotations();

src/test/java/com/jsoniter/output/TestObject.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,17 @@ public void test_omit_selft_defined() {
417417
.build();
418418
assertEquals("{}", JsonStream.serialize(cfg, new TestObject18()));
419419
}
420+
421+
public static class TestObject19 {
422+
public transient int hello;
423+
424+
public int getHello() {
425+
return hello;
426+
}
427+
}
428+
429+
public void test_transient_field_getter() {
430+
String output = JsonStream.serialize(new TestObject19());
431+
assertEquals("{}", output);
432+
}
420433
}

0 commit comments

Comments
 (0)