From f8232f0851d3ed04a55d0487a75dd675e687232a Mon Sep 17 00:00:00 2001 From: Marian Jureczko Date: Sat, 23 Nov 2024 13:36:09 +0100 Subject: [PATCH] Ensure compatibility with Android's limited Java SE subset Android utilizes a subset of the Java SE library, which excludes several packages and classes. This commit removes calls to methods unavailable on Android or wraps them in `try-catch` blocks with appropriate fallbacks. These changes ensure the Easy Random library functions on Android platforms. --- .../java/org/jeasy/random/util/ReflectionUtils.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java b/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java index 1b42926d..94c3387e 100644 --- a/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java +++ b/easy-random-core/src/main/java/org/jeasy/random/util/ReflectionUtils.java @@ -151,9 +151,14 @@ public static void setProperty(final Object object, final Field field, final Obj */ public static void setFieldValue(final Object object, final Field field, final Object value) throws IllegalAccessException { - boolean access = field.trySetAccessible(); - field.set(object, value); - field.setAccessible(access); + try { + boolean access = field.trySetAccessible(); + field.set(object, value); + field.setAccessible(access); + } catch (NoSuchMethodError e) { + field.setAccessible(true); + field.set(object, value); + } } /**