Package-private util to deal with modules. @author xxDark
| 15 | * @author xxDark |
| 16 | */ |
| 17 | final class Java9Util { |
| 18 | |
| 19 | private static final MethodHandle CLASS_MODULE; |
| 20 | private static final MethodHandle CLASS_LOADER_MODULE; |
| 21 | private static final MethodHandle METHOD_MODIFIERS; |
| 22 | |
| 23 | /** |
| 24 | * Deny all constructions. |
| 25 | */ |
| 26 | private Java9Util() { |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param klass {@link Class} to get module from. |
| 31 | * @return {@link Module} of the class. |
| 32 | */ |
| 33 | static Module getClassModule(Class<?> klass) { |
| 34 | try { |
| 35 | return (Module) CLASS_MODULE.invokeExact(klass); |
| 36 | } catch (Throwable t) { |
| 37 | // That should never happen. |
| 38 | throw new AssertionError(t); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param loader {@link ClassLoader} to get module from. |
| 44 | * @return {@link Module} of the class. |
| 45 | */ |
| 46 | static Module getLoaderModule(ClassLoader loader) { |
| 47 | try { |
| 48 | return (Module) CLASS_LOADER_MODULE.invokeExact(loader); |
| 49 | } catch (Throwable t) { |
| 50 | // That should never happen. |
| 51 | throw new AssertionError(t); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * @param method {@link Method} to change modifiers for. |
| 58 | * @param modifiers new modifiers. |
| 59 | */ |
| 60 | static void setMethodModifiers(Method method, int modifiers) { |
| 61 | try { |
| 62 | METHOD_MODIFIERS.invokeExact(method, modifiers); |
| 63 | } catch (Throwable t) { |
| 64 | // That should never happen. |
| 65 | throw new AssertionError(t); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static { |
| 71 | try { |
| 72 | Field field = Unsafe.class.getDeclaredField("theUnsafe"); |
| 73 | field.setAccessible(true); |
| 74 | Unsafe unsafe = (Unsafe) field.get(null); |