Skip to content

Commit 113afc7

Browse files
committed
json-iterator#28 avoid reference non public class in codegen
1 parent 818aaf2 commit 113afc7

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.FileOutputStream;
1111
import java.io.IOException;
1212
import java.io.OutputStreamWriter;
13+
import java.lang.reflect.Modifier;
1314
import java.lang.reflect.ParameterizedType;
1415
import java.lang.reflect.Type;
1516
import java.util.*;
@@ -114,6 +115,7 @@ private static synchronized Encoder gen(String cacheKey, Type type) {
114115
throw new JsonException("static gen should provide the encoder we need, but failed to create the encoder", e);
115116
}
116117
}
118+
clazz = chooseAccessibleSuper(clazz);
117119
CodegenResult source = genSource(clazz, typeArgs);
118120
if ("true".equals(System.getenv("JSONITER_DEBUG"))) {
119121
System.out.println(">>> " + cacheKey);
@@ -138,6 +140,13 @@ private static synchronized Encoder gen(String cacheKey, Type type) {
138140
}
139141
}
140142

143+
private static Class chooseAccessibleSuper(Class clazz) {
144+
if (Modifier.isPublic(clazz.getModifiers())) {
145+
return clazz;
146+
}
147+
return chooseAccessibleSuper(clazz.getSuperclass());
148+
}
149+
141150
public static CodegenResult getGeneratedSource(String cacheKey) {
142151
return generatedSources.get(cacheKey);
143152
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
import java.io.ByteArrayOutputStream;
77
import java.io.IOException;
8-
import java.util.ArrayList;
9-
import java.util.Arrays;
10-
import java.util.HashSet;
11-
import java.util.List;
8+
import java.util.*;
129

1310
public class TestArray extends TestCase {
1411

@@ -111,4 +108,12 @@ public void test_hash_set() throws IOException {
111108
set.add(1);
112109
assertEquals("[1]", JsonStream.serialize(set));
113110
}
111+
112+
public void test_arrays_as_list() throws IOException {
113+
assertEquals("[1,2,3]", JsonStream.serialize(Arrays.asList(1, 2, 3)));
114+
}
115+
116+
public void test_default_empty_collection() throws IOException {
117+
assertEquals("[]", JsonStream.serialize(Collections.emptySet()));
118+
}
114119
}

0 commit comments

Comments
 (0)