From e6e8096baa997c59c57bbf64257feb7fc9147540 Mon Sep 17 00:00:00 2001 From: Toastertaster Date: Thu, 17 Jul 2025 21:05:42 -0600 Subject: [PATCH] Add feature to Utils class to support a custom ClassLoader configuration when the SystemClassLoader is not able to access classes of interest. --- .../java/org/astonbitecode/j4rs/utils/Utils.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/java/src/main/java/org/astonbitecode/j4rs/utils/Utils.java b/java/src/main/java/org/astonbitecode/j4rs/utils/Utils.java index 7a705c0..07b5002 100644 --- a/java/src/main/java/org/astonbitecode/j4rs/utils/Utils.java +++ b/java/src/main/java/org/astonbitecode/j4rs/utils/Utils.java @@ -25,6 +25,7 @@ public class Utils { private static boolean IsAndroid; + private static ClassLoader classloader = ClassLoader.getSystemClassLoader(); static { try { @@ -57,7 +58,7 @@ public static Class forNameEnhanced(final String className) throws ClassNotFo return void.class; default: if (!IsAndroid) { - return Class.forName(className, true, ClassLoader.getSystemClassLoader()); + return Class.forName(className, true, classloader); } else { return Class.forName(className); } @@ -87,4 +88,13 @@ public static String throwableToString(Throwable throwable) { return "Cannot create String out of a null Throwable"; } } + + /** When the System ClassLoader is not able to load or retrieve your desired class, + * then use this method to update the behavior of Utils.forNameEnhanced() + * + * @param cl a ClassLoader with access to your class of interest. + */ + public static void setClassloader(ClassLoader cl) { + classloader = cl; + } }