net.openhft.chronicle.core.Jvm.setAccessible()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(88)

本文整理了Java中net.openhft.chronicle.core.Jvm.setAccessible()方法的一些代码示例,展示了Jvm.setAccessible()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Jvm.setAccessible()方法的具体详情如下:
包路径:net.openhft.chronicle.core.Jvm
类名称:Jvm
方法名:setAccessible

Jvm.setAccessible介绍

暂无

代码示例

代码示例来源:origin: OpenHFT/Chronicle-Queue

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);
    }
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Queue

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);
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Queue

@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());
}

代码示例来源:origin: net.openhft/chronicle-queue

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);
    }
  }
}

代码示例来源:origin: net.openhft/chronicle-map

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;
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Core

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();
  }
}

代码示例来源:origin: net.openhft/chronicle-bytes

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);
  }
}

代码示例来源:origin: net.openhft/chronicle-core

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();
  }
}

代码示例来源:origin: net.openhft/chronicle-bytes

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);
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Core

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) {

代码示例来源:origin: net.openhft/chronicle-core

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) {

代码示例来源:origin: net.openhft/chronicle-bytes

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);
  }
}

代码示例来源:origin: net.openhft/chronicle-core

/**
 * 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);
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Core

/**
 * 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);
  }
}

代码示例来源:origin: net.openhft/chronicle-core

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;
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Core

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;
  }
}

代码示例来源:origin: net.openhft/chronicle-core

@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);
  }
}

代码示例来源:origin: OpenHFT/Chronicle-Core

@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);
  }
}

代码示例来源:origin: net.openhft/chronicle-bytes

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);
  }
}

相关文章