Skip to content

Commit 93d6bf3

Browse files
committed
- Updated to use ClassUtilities.classLoader(clazz.class) to be to choose the json-io module class loader.
1 parent a6f9b6b commit 93d6bf3

File tree

12 files changed

+32
-35
lines changed

12 files changed

+32
-35
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ json-io
66
Useful tool for Java serialization to and from JSON format.
77
Available on [Maven Central](https://central.sonatype.com/search?q=json-io&namespace=com.cedarsoftware).
88
This library has <b>no dependencies</b> on other libraries for runtime other than our own `java-util.`
9-
The `json-io.jar`file is `217K` and `java-util` is `290K.` Compatible with JDK1.8 through JDK 22.
9+
The `json-io.jar`file is `220K` and `java-util` is `336K.` Compatible with JDK1.8 through JDK 23.
1010
## Compatibility
1111

1212
### JPMS (Java Platform Module System)

pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,10 @@
2727
<!-- remove source encoding warnings from maven output -->
2828
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2929

30-
<!-- Java source, target, and release version -->
31-
<maven.compiler.source>1.8</maven.compiler.source>
32-
<maven.compiler.target>1.8</maven.compiler.target>
33-
<maven.compiler.release>8</maven.compiler.release>
34-
3530
<!-- testing only -->
3631
<version.junit-jupiter-params>5.10.0</version.junit-jupiter-params>
3732
<version.assertj-core>3.24.2</version.assertj-core>
38-
<version.java-util>2.17.0</version.java-util>
33+
<version.java-util>2.18.0</version.java-util>
3934
<version.gson>2.11.0</version.gson>
4035
<version.jackson-core>2.18.0</version.jackson-core>
4136

src/main/java/com/cedarsoftware/io/JsonIo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public static ReadOptionsBuilder getReadOptionsBuilder(Map<String, Object> optio
300300
if (loader instanceof ClassLoader) {
301301
classLoader = (ClassLoader) loader;
302302
} else {
303-
classLoader = ClassUtilities.getClassLoader();
303+
classLoader = ClassUtilities.getClassLoader(JsonIo.class);
304304
}
305305
builder.classLoader(classLoader);
306306

@@ -420,7 +420,7 @@ public static WriteOptionsBuilder getWriteOptionsBuilder(Map<String, Object> opt
420420
if (loader instanceof ClassLoader) {
421421
classLoader = (ClassLoader) loader;
422422
} else {
423-
classLoader = ClassUtilities.getClassLoader();
423+
classLoader = ClassUtilities.getClassLoader(JsonIo.class);
424424
}
425425
builder.classLoader(classLoader);
426426

src/main/java/com/cedarsoftware/io/MetaUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ public static String loadResourceAsString(String resourceName) {
688688
* @return Content of the resource file as a byte[].
689689
*/
690690
public static byte[] loadResourceAsBytes(String resourceName) {
691-
try (InputStream inputStream = ClassUtilities.getClassLoader().getResourceAsStream(resourceName)) {
691+
try (InputStream inputStream = ClassUtilities.getClassLoader(MetaUtils.class).getResourceAsStream(resourceName)) {
692692
if (inputStream == null) {
693693
throw new JsonIoException("Resource not found: " + resourceName);
694694
}

src/main/java/com/cedarsoftware/io/ReadOptionsBuilder.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ public ReadOptionsBuilder addNonReferenceableClass(Class<?> clazz) {
758758
*/
759759
private static void loadBaseClassFactory() {
760760
Map<String, String> map = MetaUtils.loadMapDefinition("config/classFactory.txt");
761-
ClassLoader classLoader = ClassUtilities.getClassLoader();
761+
ClassLoader classLoader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
762762

763763
for (Map.Entry<String, String> entry : map.entrySet()) {
764764
String className = entry.getKey();
@@ -795,7 +795,7 @@ private static void loadBaseClassFactory() {
795795
*/
796796
private static void loadBaseReaders() {
797797
Map<String, String> map = MetaUtils.loadMapDefinition("config/customReaders.txt");
798-
ClassLoader classLoader = ClassUtilities.getClassLoader();
798+
ClassLoader classLoader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
799799

800800
for (Map.Entry<String, String> entry : map.entrySet()) {
801801
String className = entry.getKey();
@@ -821,7 +821,7 @@ private static void loadBaseReaders() {
821821
*/
822822
private static void loadBaseCoercedTypes() {
823823
Map<String, String> map = MetaUtils.loadMapDefinition("config/coercedTypes.txt");
824-
ClassLoader classLoader = ClassUtilities.getClassLoader();
824+
ClassLoader classLoader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
825825

826826
for (Map.Entry<String, String> entry : map.entrySet()) {
827827
String srcClassName = entry.getKey();
@@ -844,7 +844,7 @@ public static class DefaultConverterOptions implements ConverterOptions {
844844
private ZoneId zoneId = ZoneId.systemDefault();
845845
private Locale locale = Locale.getDefault();
846846
private Charset charset = StandardCharsets.UTF_8;
847-
private ClassLoader classloader = ClassUtilities.getClassLoader();
847+
private ClassLoader classloader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
848848
private Character trueChar = CommonValues.CHARACTER_ONE;
849849
private Character falseChar = CommonValues.CHARACTER_ZERO;
850850
private Map<String, Object> customOptions = new ConcurrentHashMap<>();
@@ -1295,7 +1295,7 @@ private boolean fieldIsFiltered(Field field) {
12951295
static Map<Class<?>, Set<String>> loadClassToSetOfStrings(String fileName) {
12961296
Map<String, String> map = loadMapDefinition(fileName);
12971297
Map<Class<?>, Set<String>> builtMap = new LinkedHashMap<>();
1298-
ClassLoader classLoader = ClassUtilities.getClassLoader();
1298+
ClassLoader classLoader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
12991299

13001300
for (Map.Entry<String, String> entry : map.entrySet()) {
13011301
String className = entry.getKey();
@@ -1314,7 +1314,7 @@ static Map<Class<?>, Set<String>> loadClassToSetOfStrings(String fileName) {
13141314
static Map<Class<?>, Map<String, String>> loadClassToFieldAliasNameMapping(String fileName) {
13151315
Map<String, String> map = MetaUtils.loadMapDefinition(fileName);
13161316
Map<Class<?>, Map<String, String>> nonStandardMapping = new ConcurrentHashMap<>();
1317-
ClassLoader classLoader = ClassUtilities.getClassLoader();
1317+
ClassLoader classLoader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
13181318

13191319
for (Map.Entry<String, String> entry : map.entrySet()) {
13201320
String className = entry.getKey();
@@ -1340,7 +1340,7 @@ static Map<Class<?>, Map<String, String>> loadClassToFieldAliasNameMapping(Strin
13401340
*/
13411341
private static void loadBaseNonRefs() {
13421342
final Set<String> set = MetaUtils.loadSetDefinition("config/nonRefs.txt");
1343-
final ClassLoader classLoader = ClassUtilities.getClassLoader();
1343+
final ClassLoader classLoader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
13441344

13451345
for (String className : set) {
13461346
Class<?> loadedClass = ClassUtilities.forName(className, classLoader);
@@ -1360,10 +1360,11 @@ public interface AliasApplier {
13601360

13611361
static void loadBaseAliasMappings(AliasApplier aliasApplier) {
13621362
Map<String, String> aliasMappings = MetaUtils.loadMapDefinition("config/aliases.txt");
1363+
ClassLoader classLoader = ClassUtilities.getClassLoader(ReadOptionsBuilder.class);
13631364
for (Map.Entry<String, String> entry : aliasMappings.entrySet()) {
13641365
String className = entry.getKey();
13651366
String alias = entry.getValue();
1366-
Class<?> clazz = ClassUtilities.forName(className, ClassUtilities.getClassLoader());
1367+
Class<?> clazz = ClassUtilities.forName(className, classLoader);
13671368

13681369
if (clazz == null) {
13691370
System.out.println("Could not find class: " + className + " which has associated alias value: " + alias + " config/aliases.txt");

src/main/java/com/cedarsoftware/io/Unsafe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Unsafe
2424
*/
2525
public Unsafe() throws InvocationTargetException {
2626
try {
27-
Constructor<?> unsafeConstructor = forName("sun.misc.Unsafe", ClassUtilities.getClassLoader()).getDeclaredConstructor();
27+
Constructor<?> unsafeConstructor = forName("sun.misc.Unsafe", ClassUtilities.getClassLoader(Unsafe.class)).getDeclaredConstructor();
2828
trySetAccessible(unsafeConstructor);
2929
sunUnsafe = unsafeConstructor.newInstance();
3030
allocateInstance = sunUnsafe.getClass().getMethod("allocateInstance", Class.class);

src/main/java/com/cedarsoftware/io/WriteOptionsBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ static class DefaultWriteOptions implements WriteOptions {
835835
private boolean enumPublicFieldsOnly = false;
836836
private boolean closeStream = true;
837837
private JsonWriter.JsonClassWriter enumWriter = new Writers.EnumsAsStringWriter();
838-
private ClassLoader classLoader = ClassUtilities.getClassLoader();
838+
private ClassLoader classLoader = ClassUtilities.getClassLoader(DefaultWriteOptions.class);
839839
private Map<Class<?>, Set<String>> includedFieldNames = new LinkedHashMap<>();
840840
private Map<Class<?>, Map<String, String>> nonStandardGetters = new LinkedHashMap<>();
841841
private Map<String, String> aliasTypeNames = new LinkedHashMap<>();
@@ -1202,7 +1202,7 @@ private boolean isFieldFiltered(Field field) {
12021202
*/
12031203
private static void loadBaseWriters() {
12041204
Map<String, String> map = MetaUtils.loadMapDefinition("config/customWriters.txt");
1205-
ClassLoader classLoader = ClassUtilities.getClassLoader();
1205+
ClassLoader classLoader = ClassUtilities.getClassLoader(WriteOptionsBuilder.class);
12061206

12071207
for (Map.Entry<String, String> entry : map.entrySet()) {
12081208
String className = entry.getKey();
@@ -1232,8 +1232,9 @@ private static void loadBaseWriters() {
12321232
*/
12331233
static void loadBaseNonRefs() {
12341234
Set<String> set = MetaUtils.loadSetDefinition("config/nonRefs.txt");
1235+
ClassLoader classLoader = ClassUtilities.getClassLoader(WriteOptionsBuilder.class);
12351236
set.forEach((className) -> {
1236-
Class<?> clazz = ClassUtilities.forName(className, ClassUtilities.getClassLoader());
1237+
Class<?> clazz = ClassUtilities.forName(className, classLoader);
12371238
if (clazz == null) {
12381239
System.out.println("Class: " + className + " undefined. Cannot be used as non-referenceable class, listed in resources/nonRefs.txt");
12391240
}

src/test/java/com/cedarsoftware/io/ClassForNameTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ClassForNameTest
2525
@Test
2626
public void testClassForName()
2727
{
28-
Class<?> testObjectClass = ClassUtilities.forName("com.cedarsoftware.io.TestObject", ClassUtilities.getClassLoader());
28+
Class<?> testObjectClass = ClassUtilities.forName("com.cedarsoftware.io.TestObject", ClassUtilities.getClassLoader(ClassForNameTest.class));
2929
assert testObjectClass instanceof Class<?>;
3030
assert "com.cedarsoftware.io.TestObject".equals(testObjectClass.getName());
3131
}
@@ -41,28 +41,28 @@ public void testClassForNameWithClassloader()
4141
@Test
4242
public void testClassForNameNullClassErrorHandling()
4343
{
44-
assert null == ClassUtilities.forName(null, ClassUtilities.getClassLoader());
45-
assert null == ClassUtilities.forName("Smith&Wesson", ClassUtilities.getClassLoader());
44+
assert null == ClassUtilities.forName(null, ClassUtilities.getClassLoader(ClassForNameTest.class));
45+
assert null == ClassUtilities.forName("Smith&Wesson", ClassUtilities.getClassLoader(ClassForNameTest.class));
4646
}
4747

4848
@Test
4949
public void testClassForNameFailOnClassLoaderErrorTrue()
5050
{
51-
assert null == ClassUtilities.forName("foo.bar.baz.Qux", ClassUtilities.getClassLoader());
51+
assert null == ClassUtilities.forName("foo.bar.baz.Qux", ClassUtilities.getClassLoader(ClassForNameTest.class));
5252
}
5353

5454
@Test
5555
public void testClassForNameFailOnClassLoaderErrorFalse()
5656
{
57-
Class<?> testObjectClass = ClassUtilities.forName("foo.bar.baz.Qux", ClassUtilities.getClassLoader());
57+
Class<?> testObjectClass = ClassUtilities.forName("foo.bar.baz.Qux", ClassUtilities.getClassLoader(ClassForNameTest.class));
5858
assert testObjectClass == null;
5959
}
6060

6161
private static class AlternateNameClassLoader extends ClassLoader
6262
{
6363
AlternateNameClassLoader(ClassForNameTest enclosing, String alternateName, Class<?> clazz)
6464
{
65-
super(ClassUtilities.getClassLoader());
65+
super(ClassUtilities.getClassLoader(ClassForNameTest.class));
6666
this.alternateName = alternateName;
6767
this.clazz = clazz;
6868
}

src/test/java/com/cedarsoftware/io/FieldsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public void setName(String name)
446446
this.name = name;
447447
}
448448

449-
private ClassLoader classLoader = ClassUtilities.getClassLoader();
449+
private ClassLoader classLoader = ClassUtilities.getClassLoader(FieldsTest.class);
450450
private String name;
451451
}
452452

src/test/java/com/cedarsoftware/io/PrimitivesTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void testValueAtRootNoType()
170170
@ValueSource(strings = {"java.lang.Byte", "byte"})
171171
void testByteValueAtRoot(String stringType)
172172
{
173-
Class<?> type = ClassUtilities.forName(stringType, ClassUtilities.getClassLoader());
173+
Class<?> type = ClassUtilities.forName(stringType, ClassUtilities.getClassLoader(PrimitivesTest.class));
174174
Object x = TestUtil.toObjects("120.1", type);
175175
assert Byte.class.isAssignableFrom(x.getClass());
176176
assertEquals(x, (byte)120);
@@ -196,7 +196,7 @@ void testByteValueAtRoot(String stringType)
196196
@ValueSource(strings = {"java.lang.Byte", "byte"})
197197
void testByteObjectValueAtRoot(String stringType)
198198
{
199-
Class<?> type = stringType.equals("null") ? null : ClassUtilities.forName(stringType, ClassUtilities.getClassLoader());
199+
Class<?> type = stringType.equals("null") ? null : ClassUtilities.forName(stringType, ClassUtilities.getClassLoader(PrimitivesTest.class));
200200
Object x = TestUtil.toObjects("{\"value\":120.1}", type);
201201
assert Byte.class.isAssignableFrom(x.getClass());
202202
assertEquals(x, (byte)120);
@@ -246,7 +246,7 @@ void testPrimitiveTypeAtRoot()
246246
@ValueSource(strings = {"java.lang.Byte", "byte", "null"})
247247
void testTypedByteObjectValueAtRoot(String stringType)
248248
{
249-
Class<?> type = stringType.equals("null") ? null : ClassUtilities.forName(stringType, ClassUtilities.getClassLoader());
249+
Class<?> type = stringType.equals("null") ? null : ClassUtilities.forName(stringType, ClassUtilities.getClassLoader(PrimitivesTest.class));
250250
Object x = TestUtil.toObjects("{\"@type\":\"byte\",\"value\":120.1}", type);
251251
assert Byte.class.isAssignableFrom(x.getClass());
252252
assertEquals(x, (byte)120);

0 commit comments

Comments
 (0)