diff options
author | LexManos <LexManos@gmail.com> | 2020-04-03 15:00:46 -0700 |
---|---|---|
committer | LexManos <LexManos@gmail.com> | 2020-04-03 15:00:46 -0700 |
commit | c5ae801c3a51aa6abf05c2eb5d6b3ebe592d02ab (patch) | |
tree | c604a21049122f11abaf262e022f460dd560174d | |
parent | d3c01e7be2247cce9f6fe3456db73dd0a7ef1ccf (diff) | |
download | Artifactural-c5ae801c3a51aa6abf05c2eb5d6b3ebe592d02ab.tar.gz Artifactural-c5ae801c3a51aa6abf05c2eb5d6b3ebe592d02ab.tar.bz2 Artifactural-c5ae801c3a51aa6abf05c2eb5d6b3ebe592d02ab.zip |
Be a bit more verbose when trying to reflectively set a final field failing.
-rw-r--r-- | src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java b/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java index a559a4f..d3078d5 100644 --- a/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java +++ b/src/gradlecomp/java/com/amadornes/artifactural/gradle/ReflectionUtils.java @@ -37,7 +37,14 @@ public class ReflectionUtils { } Field f = findField(target.getClass(), name); if (f == null) throw new IllegalStateException("Could not find '" + name + "'"); - f.set(target, operator.apply((T)f.get(target))); + + T oldV = (T)f.get(target); + T newV = operator.apply(oldV); + f.set(target, newV); + + if (f.get(target) != newV) { + throw new IllegalStateException("Failed to set new value on " + f.getDeclaringClass().getName() + "." + f.getName()); + } } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } @@ -62,14 +69,8 @@ public class ReflectionUtils { for (Field f : clazz.getDeclaredFields()) { if (f.getName().equals(name)) { f.setAccessible(true); - if ((f.getModifiers() & Modifier.FINAL) != 0) { - try { - Field modifiers = Field.class.getDeclaredField("modifiers"); - modifiers.setAccessible(true); - modifiers.setInt(f, f.getModifiers() & ~Modifier.FINAL); - } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { - throw new RuntimeException(e); - } + if (!definalize(f)) { + System.out.println("Could not definalize field " + f.getDeclaringClass().getName() + "." + f.getName() + " Exception ate, lets see if it works"); } return f; } @@ -79,6 +80,34 @@ public class ReflectionUtils { return null; } + private static Field MODIFIER_ACCESS = null; + private static boolean accessAttempted = false; + private static synchronized boolean definalize(Field f) { + if ((f.getModifiers() & Modifier.FINAL) == 0) { + return true; + } + + if (MODIFIER_ACCESS == null && !accessAttempted) { + try { + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + MODIFIER_ACCESS = modifiers; + } catch (NoSuchFieldException e) { + System.out.println("Could not access Field.modifiers to definalize reflection object. This happens on JVMs > 12, going to see if things work, if not use JVM 8-11"); + } + accessAttempted = true; + } + if (MODIFIER_ACCESS != null) { + try { + MODIFIER_ACCESS.setInt(f, f.getModifiers() & ~Modifier.FINAL); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new RuntimeException("Could not definalize field " + f.getDeclaringClass().getName() + "." + f.getName(), e); + } + return true; + } + return false; + } + /** * Invokes a method (can be private). */ |