Skip to content

Commit 0c367e9

Browse files
committed
fix referencing private class
1 parent 168eabd commit 0c367e9

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

src/main/java/com/jsoniter/Codegen.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ private synchronized static Decoder gen(String cacheKey, Type type) {
9292
JsoniterSpi.addNewDecoder(cacheKey, decoder);
9393
return decoder;
9494
} catch (Exception e) {
95-
System.err.println("failed to generate decoder for: " + type + " with " + Arrays.toString(typeArgs) + ", exception: " + e);
96-
System.err.println(source);
97-
throw new JsonException(e);
95+
String msg = "failed to generate decoder for: " + type + " with " + Arrays.toString(typeArgs) + ", exception: " + e;
96+
msg = msg + "\n" + source;
97+
throw new JsonException(msg, e);
9898
}
9999
}
100100

src/main/java/com/jsoniter/output/Codegen.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ private static synchronized Encoder gen(String cacheKey, Type type) {
133133
JsoniterSpi.addNewEncoder(cacheKey, encoder);
134134
return encoder;
135135
} catch (Exception e) {
136-
System.err.println("failed to generate encoder for: " + type + " with " + Arrays.toString(typeArgs) + ", exception: " + e);
137-
System.err.println(source);
138-
JsoniterSpi.dump();
139-
throw new JsonException(e);
136+
String msg = "failed to generate encoder for: " + type + " with " + Arrays.toString(typeArgs) + ", exception: " + e;
137+
msg = msg + "\n" + source;
138+
throw new JsonException(msg, e);
140139
}
141140
}
142141

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ private static List<Binding> getFields(Map<String, Type> lookup, Class clazz, bo
279279
if (Modifier.isTransient(field.getModifiers())) {
280280
continue;
281281
}
282+
if (!includingPrivate && !Modifier.isPublic(field.getType().getModifiers())) {
283+
continue;
284+
}
282285
if (includingPrivate) {
283286
field.setAccessible(true);
284287
}
@@ -316,16 +319,7 @@ private static List<Field> getAllFields(Class clazz, boolean includingPrivate) {
316319

317320
private static List<Binding> getSetters(Map<String, Type> lookup, Class clazz, boolean includingPrivate) {
318321
ArrayList<Binding> setters = new ArrayList<Binding>();
319-
List<Method> allMethods = Arrays.asList(clazz.getMethods());
320-
if (includingPrivate) {
321-
allMethods = new ArrayList<Method>();
322-
Class current = clazz;
323-
while (current != null) {
324-
allMethods.addAll(Arrays.asList(current.getDeclaredMethods()));
325-
current = current.getSuperclass();
326-
}
327-
}
328-
for (Method method : allMethods) {
322+
for (Method method : getAllMethods(clazz, includingPrivate)) {
329323
if (Modifier.isStatic(method.getModifiers())) {
330324
continue;
331325
}
@@ -340,6 +334,9 @@ private static List<Binding> getSetters(Map<String, Type> lookup, Class clazz, b
340334
if (paramTypes.length != 1) {
341335
continue;
342336
}
337+
if (!includingPrivate && !Modifier.isPublic(method.getParameterTypes()[0].getModifiers())) {
338+
continue;
339+
}
343340
if (includingPrivate) {
344341
method.setAccessible(true);
345342
}
@@ -358,6 +355,19 @@ private static List<Binding> getSetters(Map<String, Type> lookup, Class clazz, b
358355
return setters;
359356
}
360357

358+
private static List<Method> getAllMethods(Class clazz, boolean includingPrivate) {
359+
List<Method> allMethods = Arrays.asList(clazz.getMethods());
360+
if (includingPrivate) {
361+
allMethods = new ArrayList<Method>();
362+
Class current = clazz;
363+
while (current != null) {
364+
allMethods.addAll(Arrays.asList(current.getDeclaredMethods()));
365+
current = current.getSuperclass();
366+
}
367+
}
368+
return allMethods;
369+
}
370+
361371
private static String translateSetterName(String methodName) {
362372
if (!methodName.startsWith("set")) {
363373
return null;
@@ -371,7 +381,7 @@ private static String translateSetterName(String methodName) {
371381

372382
private static List<Binding> getGetters(Map<String, Type> lookup, Class clazz, boolean includingPrivate) {
373383
ArrayList<Binding> getters = new ArrayList<Binding>();
374-
for (Method method : clazz.getMethods()) {
384+
for (Method method : getAllMethods(clazz, includingPrivate)) {
375385
if (Modifier.isStatic(method.getModifiers())) {
376386
continue;
377387
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,22 @@ public void test_iterator() {
197197
assertEquals(3, iter.value().toInt());
198198
assertFalse(iter.next());
199199
}
200+
201+
public static class PublicSuper {
202+
public String field1;
203+
}
204+
205+
private static class PrivateSub extends PublicSuper {
206+
}
207+
208+
public static class TestObject7 {
209+
public PrivateSub field1;
210+
public void setFieldXXX(PrivateSub obj) {
211+
}
212+
}
213+
214+
public void test_private_ref() throws IOException {
215+
TestObject7 obj = JsonIterator.deserialize("{}", TestObject7.class);
216+
assertNull(obj.field1);
217+
}
200218
}

0 commit comments

Comments
 (0)