diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-04-24 00:40:44 +0200 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-04-24 00:41:27 +0200 |
commit | 4cbe67361ba58f86e8847926f07a5172db1f6d2f (patch) | |
tree | f50f4ba0e362afdb984825269f71fbd373d0b8f8 /src/utils/lombok/permit | |
parent | ad93bb07049e80a2063c1231f236cd12118ec784 (diff) | |
download | lombok-4cbe67361ba58f86e8847926f07a5172db1f6d2f.tar.gz lombok-4cbe67361ba58f86e8847926f07a5172db1f6d2f.tar.bz2 lombok-4cbe67361ba58f86e8847926f07a5172db1f6d2f.zip |
[fixes #2085] JDK12 compatibility. Also acknowledging @nqzero for the permit-reflect library which is inspiring our shenanigans :)
Diffstat (limited to 'src/utils/lombok/permit')
-rw-r--r-- | src/utils/lombok/permit/Permit.java | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/utils/lombok/permit/Permit.java b/src/utils/lombok/permit/Permit.java index 9f0434b8..b7c5f0d9 100644 --- a/src/utils/lombok/permit/Permit.java +++ b/src/utils/lombok/permit/Permit.java @@ -46,8 +46,7 @@ public class Permit { Throwable ex; try { - f = AccessibleObject.class.getDeclaredField("override"); - g = UNSAFE.objectFieldOffset(f); + g = getOverrideFieldOffset(); ex = null; } catch (Throwable t) { f = null; @@ -74,6 +73,30 @@ public class Permit { return accessor; } + private static long getOverrideFieldOffset() throws Throwable { + Field f = null; + Throwable saved = null; + try { + f = AccessibleObject.class.getDeclaredField("override"); + } catch (Throwable t) { + saved = t; + } + + if (f != null) { + return UNSAFE.objectFieldOffset(f); + } + // The below seems very risky, but for all AccessibleObjects in java today it does work, and starting with JDK12, making the field accessible is no longer possible. + try { + return UNSAFE.objectFieldOffset(Fake.class.getDeclaredField("override")); + } catch (Throwable t) { + throw saved; + } + } + + static class Fake { + boolean override; + } + public static Method getMethod(Class<?> c, String mName, Class<?>... parameterTypes) throws NoSuchMethodException { Method m = null; Class<?> oc = c; |