@Override public Checksum create() { try { return (Checksum) CONSTRUCTOR.invoke(); } catch (Throwable throwable) { // Should never happen throw new RuntimeException(throwable); } } }
public Runnable embedVersion(Runnable runnable) { requireNonNull(runnable, "runnable is null"); try { return (Runnable) runnableConstructor.invoke(runnable); } catch (Throwable throwable) { throwIfUnchecked(throwable); throw new RuntimeException(throwable); } } }
private static int offset( String value ) { try { return getOffset == null ? 0 : (int) getOffset.invoke( value ); } catch ( Throwable e ) { throw new AssertionError( "This encoder depends being able to access the offset in the char[] array in java.lang.String, " + "which failed: " + e.getMessage(), e ); } }
@Override public OutputStream wrapForOutput(ByteBufferOutputStream buffer, byte messageVersion) { try { return (OutputStream) ZstdConstructors.OUTPUT.invoke(buffer); } catch (Throwable e) { throw new KafkaException(e); } }
@Override public OutputStream wrapForOutput(ByteBufferOutputStream buffer, byte messageVersion) { try { return (OutputStream) SnappyConstructors.OUTPUT.invoke(buffer); } catch (Throwable e) { throw new KafkaException(e); } }
@Override public Work<Block> project(ConnectorSession session, DriverYieldSignal yieldSignal, Page page, SelectedPositions selectedPositions) { blockBuilder = blockBuilder.newBlockBuilderLike(null); try { return (Work<Block>) pageProjectionWorkFactory.invoke(blockBuilder, session, page, selectedPositions); } catch (Throwable e) { throw new RuntimeException(e); } }
@Override public void close() { try { cleanHandle.invoke( cleaner ); } catch ( Throwable throwable ) { throw new LinkageError( "Unable to clean cleaner.", throwable ); } }
private Object getValue( Object record ) throws ProcedureException { try { return getter.invoke( record ); } catch ( Throwable throwable ) { throw new ProcedureException( Status.Procedure.ProcedureCallFailed, throwable, "Unable to read value from record `%s`: %s", record, throwable.getMessage() ); } } }
private static Object globalCleaner() { MethodHandles.Lookup lookup = MethodHandles.lookup(); try { Class<?> newCleaner = Class.forName( "java.lang.ref.Cleaner" ); MethodHandle createInstance = lookup.findStatic( newCleaner, "create", MethodType.methodType( newCleaner ) ); return createInstance.invoke(); } catch ( Throwable throwable ) { return null; } }
private static boolean isDeterminateConstant(RowExpression expression, MethodHandle isIndeterminateFunction) { if (!(expression instanceof ConstantExpression)) { return false; } ConstantExpression constantExpression = (ConstantExpression) expression; Object value = constantExpression.getValue(); boolean isNull = value == null; if (isNull) { return false; } try { return !(boolean) isIndeterminateFunction.invoke(value, false); } catch (Throwable t) { throwIfUnchecked(t); throw new RuntimeException(t); } } }
@Override public InputStream wrapForInput(ByteBuffer buffer, byte messageVersion, BufferSupplier decompressionBufferSupplier) { try { return (InputStream) SnappyConstructors.INPUT.invoke(new ByteBufferInputStream(buffer)); } catch (Throwable e) { throw new KafkaException(e); } } },
@Override public InputStream wrapForInput(ByteBuffer buffer, byte messageVersion, BufferSupplier decompressionBufferSupplier) { try { return (InputStream) ZstdConstructors.INPUT.invoke(new ByteBufferInputStream(buffer)); } catch (Throwable e) { throw new KafkaException(e); } } };
void apply( org.neo4j.kernel.api.proc.Context ctx, Object object ) throws ProcedureException { try { setter.invoke( object, provider.apply( ctx ) ); } catch ( Throwable e ) { throw new ProcedureException( Status.Procedure.ProcedureCallFailed, e, "Unable to inject component to field `%s`, please ensure it is public and non-final: %s", field.getName(), e.getMessage() ); } } }
private static MethodHandle bindInstanceFactory(MethodHandle method, ScalarFunctionImplementation implementation) { if (!implementation.getInstanceFactory().isPresent()) { return method; } try { return method.bindTo(implementation.getInstanceFactory().get().invoke()); } catch (Throwable throwable) { throw propagate(throwable); } }
private static AccumulatorStateSerializer<?> getAccumulatorStateSerializer(AggregationImplementation implementation, BoundVariables variables, TypeManager typeManager, FunctionRegistry functionRegistry, Class<?> stateClass, DynamicClassLoader classLoader) { AccumulatorStateSerializer<?> stateSerializer; Optional<MethodHandle> stateSerializerFactory = implementation.getStateSerializerFactory(); if (stateSerializerFactory.isPresent()) { try { MethodHandle factoryHandle = bindDependencies(stateSerializerFactory.get(), implementation.getStateSerializerFactoryDependencies(), variables, typeManager, functionRegistry); stateSerializer = (AccumulatorStateSerializer<?>) factoryHandle.invoke(); } catch (Throwable t) { throwIfUnchecked(t); throw new RuntimeException(t); } } else { stateSerializer = generateStateSerializer(stateClass, classLoader); } return stateSerializer; }
@Test public void shouldGenerateDefaultConstructor() throws Throwable { // given ClassHandle handle; try ( ClassGenerator simple = generateClass( NamedBase.class, "SimpleClass" ) ) { handle = simple.handle(); } // when Object instance = constructor( handle.loadClass() ).invoke(); Object constructorCalled = instanceMethod( instance, "defaultConstructorCalled" ).invoke(); // then assertTrue( (Boolean) constructorCalled ); }
@TypeParameter("T") @SqlType(StandardTypes.BIGINT) public static long hash( @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.invoke(readNativeValue(type, block, i))); } return hash; } catch (Throwable t) { throw internalError(t); } }
@Test public void testSelectsMethodBasedOnReturnType() throws Throwable { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarcharCreateSliceWithExtraParameterLength")) .implementation(methodsGroup -> methodsGroup .methods("varcharToBigintReturnExtraParameter") .withExtraParameters(context -> ImmutableList.of(42)))) .build(); ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); assertEquals(functionImplementation.getMethodHandle().invoke(INPUT_SLICE), VARCHAR_TO_BIGINT_RETURN_VALUE); }
@Test public void testSelectsMethodBasedOnArgumentTypes() throws Throwable { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("bigintToBigintReturnExtraParameter")) .implementation(methodsGroup -> methodsGroup .methods("varcharToBigintReturnExtraParameter") .withExtraParameters(context -> ImmutableList.of(context.getLiteral("x"))))) .build(); ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); assertEquals(functionImplementation.getMethodHandle().invoke(INPUT_SLICE), INPUT_VARCHAR_LENGTH); }
@Test public void testSameLiteralInArgumentsAndReturnValue() throws Throwable { Signature signature = Signature.builder() .name("foo") .kind(SCALAR) .returnType(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))) .argumentTypes(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))) .build(); SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))) .build(); ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); Slice slice = (Slice) functionImplementation.getMethodHandle().invoke(INPUT_SLICE); assertEquals(slice, VARCHAR_TO_VARCHAR_RETURN_VALUE); }