private static void forceUnlock(AbstractTSQueueLock lock) { try { Method forceUnlock = AbstractTSQueueLock.class.getDeclaredMethod("forceUnlock"); Jvm.setAccessible(forceUnlock); forceUnlock.invoke(lock); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); System.exit(2); } } }
private SingleChronicleQueueStore loadStore(Wire wire) { try { Method loadStoreMethod = SingleChronicleQueueBuilder.class.getDeclaredMethod("loadStore", Wire.class); Jvm.setAccessible(loadStoreMethod); return (SingleChronicleQueueStore) loadStoreMethod.invoke(null, wire); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }
@Test public void tailerToEndIncreasesRefCount() throws Exception { String path = OS.TARGET + "/toEndIncRefCount-" + System.nanoTime(); IOTools.shallowDeleteDirWithFiles(path); SetTimeProvider time = new SetTimeProvider(); long now = System.currentTimeMillis(); time.currentTimeMillis(now); ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path) .testBlockSize() .rollCycle(RollCycles.TEST_SECONDLY) .timeProvider(time) .build(); final SingleChronicleQueueExcerpts.StoreAppender appender = (SingleChronicleQueueExcerpts.StoreAppender) queue.acquireAppender(); Field storeF1 = SingleChronicleQueueExcerpts.StoreAppender.class.getDeclaredField("store"); Jvm.setAccessible(storeF1); SingleChronicleQueueStore store1 = (SingleChronicleQueueStore) storeF1.get(appender); System.out.println(store1); appender.writeDocument(wire -> wire.write(() -> "msg").int32(1)); final SingleChronicleQueueExcerpts.StoreTailer tailer = (SingleChronicleQueueExcerpts.StoreTailer) queue.createTailer(); System.out.println(tailer); tailer.toEnd(); System.out.println(tailer); Field storeF2 = SingleChronicleQueueExcerpts.StoreTailer.class.getDeclaredField("store"); Jvm.setAccessible(storeF2); SingleChronicleQueueStore store2 = (SingleChronicleQueueStore) storeF2.get(tailer); // the reference count here is 1, the queue itself assertEquals(1, store2.refCount()); }
private static void forceUnlock(AbstractTSQueueLock lock) { try { Method forceUnlock = AbstractTSQueueLock.class.getDeclaredMethod("forceUnlock"); Jvm.setAccessible(forceUnlock); forceUnlock.invoke(lock); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); System.exit(2); } } }
private static int getNativeFileDescriptor(FileDescriptor descriptor) { try { final Field field = descriptor.getClass().getDeclaredField("fd"); Jvm.setAccessible(field); return (int) field.get(descriptor); } catch (final Exception e) { LOG.warn("unsupported FileDescriptor implementation: e={}", e.getLocalizedMessage()); return -1; } }
private static MethodHandles.Lookup acquireLookup(Class<?> c) { try { // try to create one using a constructor Constructor<MethodHandles.Lookup> lookupConstructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE); if (!lookupConstructor.isAccessible()) { Jvm.setAccessible(lookupConstructor); } return lookupConstructor.newInstance(c, MethodHandles.Lookup.PRIVATE); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { } try { // Try to grab an internal one, final Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"); Jvm.setAccessible(field); return (MethodHandles.Lookup) field.get(null); } catch (Exception e) { // use the default to produce an error message. return MethodHandles.lookup(); } }
public static void getAllField(@NotNull Class clazz, @NotNull Map<String, Field> map) { if (clazz != Object.class) getAllField(clazz.getSuperclass(), map); for (@NotNull Field field : clazz.getDeclaredFields()) { if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) continue; Jvm.setAccessible(field); map.put(field.getName(), field); } }
private static MethodHandles.Lookup acquireLookup(Class<?> c) { try { // try to create one using a constructor Constructor<MethodHandles.Lookup> lookupConstructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE); if (!lookupConstructor.isAccessible()) { Jvm.setAccessible(lookupConstructor); } return lookupConstructor.newInstance(c, MethodHandles.Lookup.PRIVATE); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ignored) { } try { // Try to grab an internal one, final Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"); Jvm.setAccessible(field); return (MethodHandles.Lookup) field.get(null); } catch (Exception e) { // use the default to produce an error message. return MethodHandles.lookup(); } }
private void doNotCloseOnInterrupt(FileChannel fc) { try { Field field = AbstractInterruptibleChannel.class .getDeclaredField("interruptor"); Jvm.setAccessible(field); field.set(fc, (Interruptible) thread -> System.err.println(getClass().getName() + " - " + fc + " not closed on interrupt")); } catch (Throwable e) { Jvm.warn().on(getClass(), "Couldn't disable close on interrupt", e); } }
try { Method valueOf = c.getDeclaredMethod("valueOf", String.class); Jvm.setAccessible(valueOf); return s -> valueOf.invoke(null, s); } catch (NoSuchMethodException e) { Jvm.setAccessible(parse); return s -> parse.invoke(null, s); Jvm.setAccessible(fromString); return s -> fromString.invoke(null, s); Jvm.setAccessible(constructor); return constructor::newInstance; } catch (Exception e) {
try { Method valueOf = c.getDeclaredMethod("valueOf", String.class); Jvm.setAccessible(valueOf); return s -> valueOf.invoke(null, s); } catch (NoSuchMethodException e) { Jvm.setAccessible(parse); return s -> parse.invoke(null, s); Jvm.setAccessible(fromString); return s -> fromString.invoke(null, s); Jvm.setAccessible(constructor); return constructor::newInstance; } catch (Exception e) {
private void doNotCloseOnInterrupt9(FileChannel fc) { try { Field field = AbstractInterruptibleChannel.class.getDeclaredField("interruptor"); Class<?> interruptibleClass = field.getType(); Jvm.setAccessible(field); field.set(fc, Proxy.newProxyInstance( interruptibleClass.getClassLoader(), new Class[]{interruptibleClass}, (p, m, a) -> { System.err.println(getClass().getName() + " - " + fc + " not closed on interrupt"); return null; })); } catch (Throwable e) { Jvm.warn().on(getClass(), "Couldn't disable close on interrupt", e); } }
/** * Get the Field for a class by name. * * @param clazz to get the field for * @param name of the field * @return the Field. */ public static Field getField(@NotNull Class clazz, @NotNull String name) { try { Field field = clazz.getDeclaredField(name); setAccessible(field); return field; } catch (NoSuchFieldException e) { Class superclass = clazz.getSuperclass(); if (superclass != null) try { return getField(superclass, name); } catch (Exception ignored) { } throw new AssertionError(e); } }
/** * Get the Field for a class by name. * * @param clazz to get the field for * @param name of the field * @return the Field. */ public static Field getField(@NotNull Class clazz, @NotNull String name) { try { Field field = clazz.getDeclaredField(name); setAccessible(field); return field; } catch (NoSuchFieldException e) { Class superclass = clazz.getSuperclass(); if (superclass != null) try { return getField(superclass, name); } catch (Exception ignored) { } throw new AssertionError(e); } }
private static Method getMethod0(@NotNull Class clazz, @NotNull String name, Class[] args, boolean first) { try { Method field = clazz.getDeclaredMethod(name, args); setAccessible(field); return field; } catch (NoSuchMethodException e) { Class superclass = clazz.getSuperclass(); if (superclass != null) try { Method m = getMethod0(superclass, name, args, false); if (m != null) return m; } catch (Exception ignored) { } if (first) throw new AssertionError(e); return null; } }
private static Method getMethod0(@NotNull Class clazz, @NotNull String name, Class[] args, boolean first) { try { Method field = clazz.getDeclaredMethod(name, args); setAccessible(field); return field; } catch (NoSuchMethodException e) { Class superclass = clazz.getSuperclass(); if (superclass != null) try { Method m = getMethod0(superclass, name, args, false); if (m != null) return m; } catch (Exception ignored) { } if (first) throw new AssertionError(e); return null; } }
@Override public void clean(final ByteBuffer buffer) { try { final Method cleanerMethod; cleanerMethod = DirectBuffer.class. getDeclaredMethod("cleaner"); Jvm.setAccessible(cleanerMethod); final Object cleaner = cleanerMethod.invoke(buffer); final String cleanerClassname = Jvm.isJava9Plus() ? JDK9_CLEANER_CLASS_NAME : JDK8_CLEANER_CLASS_NAME; final Method cleanMethod = Jvm.getMethod(Class.forName(cleanerClassname), "clean"); cleanMethod.invoke(cleaner); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) { LOGGER.warn("Failed to clean buffer", e); } }
@Override public void clean(final ByteBuffer buffer) { try { final Method cleanerMethod; cleanerMethod = DirectBuffer.class. getDeclaredMethod("cleaner"); Jvm.setAccessible(cleanerMethod); final Object cleaner = cleanerMethod.invoke(buffer); final String cleanerClassname = Jvm.isJava9Plus() ? JDK9_CLEANER_CLASS_NAME : JDK8_CLEANER_CLASS_NAME; final Method cleanMethod = Jvm.getMethod(Class.forName(cleanerClassname), "clean"); cleanMethod.invoke(cleaner); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) { LOGGER.warn("Failed to clean buffer", e); } }
private void addEncoder(Object object, Method method, MethodEncoder encoder) { Jvm.setAccessible(method); Class<?>[] parameterTypes = method.getParameterTypes(); int count = parameterTypes.length; BytesMarshallable[][] array = new BytesMarshallable[1][count]; for (int i = 0; i < count; i++) { array[0][i] = (BytesMarshallable) ObjectUtils.newInstance(parameterTypes[i]); } Consumer<BytesIn> reader = in -> { array[0] = (BytesMarshallable[]) encoder.decode(array[0], in); try { method.invoke(object, array[0]); } catch (IllegalAccessException | InvocationTargetException e) { Jvm.warn().on(getClass(), "Exception calling " + method + " " + Arrays.toString(array[0]), e); } }; long messageId = encoder.messageId(); if (messageId >= 0 && messageId < 1000) { while (methodEncoders.size() <= messageId) methodEncoders.add(null); methodEncoders.set((int) messageId, reader); } else { methodEncoderMap.put(messageId, reader); } }