java.lang.invoke.MethodHandle.invokeExact()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(213)

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

MethodHandle.invokeExact介绍

[英]Invokes the method handle, allowing any caller type descriptor, but requiring an exact type match. The symbolic type descriptor at the call site of invokeExact must exactly match this method handle's #type. No conversions are allowed on arguments or return values.

When this method is observed via the Core Reflection API, it will appear as a single native method, taking an object array and returning an object. If this native method is invoked directly via java.lang.reflect.Method#invoke, via JNI, or indirectly via java.lang.invoke.MethodHandles.Lookup#unreflect, it will throw an UnsupportedOperationException.
[中]

代码示例

代码示例来源:origin: apache/kafka

public static void unmap(String resourceDescription, MappedByteBuffer buffer) throws IOException {
  if (!buffer.isDirect())
    throw new IllegalArgumentException("Unmapping only works with direct buffers");
  if (UNMAP == null)
    throw UNMAP_NOT_SUPPORTED_EXCEPTION;
  try {
    UNMAP.invokeExact((ByteBuffer) buffer);
  } catch (Throwable throwable) {
    throw new IOException("Unable to unmap the mapped buffer: " + resourceDescription, throwable);
  }
}

代码示例来源:origin: neo4j/neo4j

private Object positionLock( FileChannel channel )
{
  sun.nio.ch.FileChannelImpl impl = (FileChannelImpl) channel;
  try
  {
    return (Object) positionLockGetter.invokeExact( impl );
  }
  catch ( Throwable th )
  {
    throw new LinkageError( "No getter for FileChannel.positionLock", th );
  }
}

代码示例来源:origin: prestodb/presto

/**
 * Parse a string (optionally containing a zone) as a value of TIMESTAMP type.
 * If the string specifies a zone, the zone is discarded.
 * <p>
 * For example: {@code "2000-01-01 01:23:00"} is parsed to TIMESTAMP {@code 2000-01-01T01:23:00}
 * and {@code "2000-01-01 01:23:00 +01:23"} is also parsed to TIMESTAMP {@code 2000-01-01T01:23:00.000}.
 *
 * @return stack representation of TIMESTAMP type
 */
public static long parseTimestampWithoutTimeZone(String value)
{
  LocalDateTime localDateTime = TIMESTAMP_WITH_OR_WITHOUT_TIME_ZONE_FORMATTER.parseLocalDateTime(value);
  try {
    return (long) getLocalMillis.invokeExact(localDateTime);
  }
  catch (Throwable e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: prestodb/presto

private static int getHashPosition(Block keyBlock, int position, MethodHandle keyBlockHashCode, int hashTableSize)
{
  if (keyBlock.isNull(position)) {
    throw new IllegalArgumentException("map keys cannot be null");
  }
  long hashCode;
  try {
    hashCode = (long) keyBlockHashCode.invokeExact(keyBlock, position);
  }
  catch (RuntimeException e) {
    throw e;
  }
  catch (Throwable throwable) {
    throw new RuntimeException(throwable);
  }
  return computePosition(hashCode, hashTableSize);
}

代码示例来源:origin: prestodb/presto

private static int getHashPosition(Block keyBlock, int position, MethodHandle keyBlockHashCode, int hashTableSize)
{
  if (keyBlock.isNull(position)) {
    throw new IllegalArgumentException("map keys cannot be null");
  }
  long hashCode;
  try {
    hashCode = (long) keyBlockHashCode.invokeExact(keyBlock, position);
  }
  catch (RuntimeException e) {
    throw e;
  }
  catch (Throwable throwable) {
    throw new RuntimeException(throwable);
  }
  return computePosition(hashCode, hashTableSize);
}

代码示例来源:origin: prestodb/presto

@Override
public int hashCode(Object value)
{
  try {
    return toIntExact(Long.hashCode((long) hashCodeHandle.invokeExact(value)));
  }
  catch (Throwable t) {
    throwIfInstanceOf(t, Error.class);
    throwIfInstanceOf(t, PrestoException.class);
    throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
  }
}

代码示例来源:origin: prestodb/presto

@Override
public int hashCode(long value)
{
  try {
    return Long.hashCode((long) hashCodeHandle.invokeExact(value));
  }
  catch (Throwable t) {
    throwIfInstanceOf(t, Error.class);
    throwIfInstanceOf(t, PrestoException.class);
    throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
  }
}

代码示例来源:origin: prestodb/presto

@Override
public int hashCode(double value)
{
  try {
    return Long.hashCode((long) hashCodeHandle.invokeExact(value));
  }
  catch (Throwable t) {
    throwIfInstanceOf(t, Error.class);
    throwIfInstanceOf(t, PrestoException.class);
    throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
  }
}

代码示例来源:origin: prestodb/presto

private static boolean invokeGetResult(MethodHandle getResultMethodHandle, int comparisonResult)
{
  try {
    return (boolean) getResultMethodHandle.invokeExact(comparisonResult);
  }
  catch (Throwable t) {
    throwIfInstanceOf(t, Error.class);
    throwIfInstanceOf(t, PrestoException.class);
    throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
  }
}

代码示例来源:origin: prestodb/presto

@Override
  public boolean equals(double a, double b)
  {
    try {
      Boolean result = (Boolean) equalsHandle.invokeExact(a, b);
      // FastutilHashSet is not intended be used for indeterminate values lookup
      verify(result != null, "result is null");
      return TRUE.equals(result);
    }
    catch (Throwable t) {
      throwIfInstanceOf(t, Error.class);
      throwIfInstanceOf(t, PrestoException.class);
      throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
    }
  }
}

代码示例来源:origin: prestodb/presto

@Override
  public boolean equals(Object a, Object b)
  {
    try {
      Boolean result = (Boolean) equalsHandle.invokeExact(a, b);
      // FastutilHashSet is not intended be used for indeterminate values lookup
      verify(result != null, "result is null");
      return TRUE.equals(result);
    }
    catch (Throwable t) {
      throwIfInstanceOf(t, Error.class);
      throwIfInstanceOf(t, PrestoException.class);
      throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
    }
  }
}

代码示例来源:origin: prestodb/presto

@Override
  public boolean equals(long a, long b)
  {
    try {
      Boolean result = (Boolean) equalsHandle.invokeExact(a, b);
      // FastutilHashSet is not intended be used for indeterminate values lookup
      verify(result != null, "result is null");
      return TRUE.equals(result);
    }
    catch (Throwable t) {
      throwIfInstanceOf(t, Error.class);
      throwIfInstanceOf(t, PrestoException.class);
      throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
    }
  }
}

代码示例来源:origin: prestodb/presto

private static void compareAndUpdateState(MethodHandle methodHandle, NullableLongState state, long value)
{
  if (state.isNull()) {
    state.setNull(false);
    state.setLong(value);
    return;
  }
  try {
    if ((boolean) methodHandle.invokeExact(value, state.getLong())) {
      state.setLong(value);
    }
  }
  catch (Throwable t) {
    throw internalError(t);
  }
}

代码示例来源:origin: prestodb/presto

private static void compareAndUpdateState(MethodHandle methodHandle, NullableDoubleState state, double value)
{
  if (state.isNull()) {
    state.setNull(false);
    state.setDouble(value);
    return;
  }
  try {
    if ((boolean) methodHandle.invokeExact(value, state.getDouble())) {
      state.setDouble(value);
    }
  }
  catch (Throwable t) {
    throw internalError(t);
  }
}

代码示例来源:origin: prestodb/presto

private static void compareAndUpdateState(MethodHandle methodHandle, NullableBooleanState state, boolean value)
  {
    if (state.isNull()) {
      state.setNull(false);
      state.setBoolean(value);
      return;
    }
    try {
      if ((boolean) methodHandle.invokeExact(value, state.getBoolean())) {
        state.setBoolean(value);
      }
    }
    catch (Throwable t) {
      throw internalError(t);
    }
  }
}

代码示例来源:origin: prestodb/presto

@Test
public void testCompose2withoutS()
    throws Throwable
{
  MethodHandle fU2R = methodHandle(TestMethodHandleUtil.class, "fU2R", U.class);
  MethodHandle fT2U = methodHandle(TestMethodHandleUtil.class, "fT2U", T1.class, T2.class);
  MethodHandle composed = compose(fU2R, fT2U);
  assertEquals((R) composed.invokeExact(new T1(2), new T2(3)), new R(6));
}

代码示例来源:origin: prestodb/presto

@Test
public void testCompose2()
    throws Throwable
{
  MethodHandle fUS2R = methodHandle(TestMethodHandleUtil.class, "fUS2R", U.class, S1.class, S2.class);
  MethodHandle fT2U = methodHandle(TestMethodHandleUtil.class, "fT2U", T1.class, T2.class);
  MethodHandle composed = compose(fUS2R, fT2U);
  assertEquals((R) composed.invokeExact(new T1(2), new T2(3), new S1(5), new S2(7)), new R(210));
}

代码示例来源:origin: prestodb/presto

@Test
public void testCompose3()
    throws Throwable
{
  MethodHandle fUV2R = methodHandle(TestMethodHandleUtil.class, "fUV2R", U.class, V.class);
  MethodHandle fS2U = methodHandle(TestMethodHandleUtil.class, "fS2U", S1.class, S2.class);
  MethodHandle fT2V = methodHandle(TestMethodHandleUtil.class, "fT2V", T1.class, T2.class);
  MethodHandle composed = compose(fUV2R, fS2U, fT2V);
  assertEquals((R) composed.invokeExact(new S1(2), new S2(3), new T1(5), new T2(7)), new R(210));
}

代码示例来源:origin: prestodb/presto

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType(StandardTypes.BIGINT)
public static long hashLong(
    @OperatorDependency(operator = HASH_CODE, returnType = StandardTypes.BIGINT, argumentTypes = {"T"}) MethodHandle hashFunction,
    @TypeParameter("T") Type type,
    @SqlType("array(T)") Block block)
{
  try {
    long hash = 0;
    for (int i = 0; i < block.getPositionCount(); i++) {
      hash = CombineHashFunction.getHash(hash, block.isNull(i) ? NULL_HASH_CODE : (long) hashFunction.invokeExact(type.getLong(block, i)));
    }
    return hash;
  }
  catch (Throwable t) {
    throw internalError(t);
  }
}

代码示例来源:origin: prestodb/presto

@SqlType(StandardTypes.INTEGER)
  public static long testRegularConvention(
      @FunctionDependency(name = "add",
          returnType = StandardTypes.INTEGER,
          argumentTypes = {StandardTypes.INTEGER, StandardTypes.INTEGER},
          convention = @Convention(arguments = {NEVER_NULL, NEVER_NULL}, result = FAIL_ON_NULL)) MethodHandle function,
      @SqlType(StandardTypes.INTEGER) long left,
      @SqlType(StandardTypes.INTEGER) long right)
  {
    try {
      return (long) function.invokeExact(left, right);
    }
    catch (Throwable t) {
      throwIfInstanceOf(t, Error.class);
      throwIfInstanceOf(t, PrestoException.class);
      throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
    }
  }
}

相关文章