Java 8 MethodHandle - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 MethodHandle class.

This code example shows how to use the following methods:invokeExact
              MethodHandle mh_vi,
              MethodHandle mh_vv) throws Throwable {
        Object o; String s; int i;  // for return type testing
 
        // next five must have sig = (String,int)Object 
        mh_SiO.invokeExact("world", 123);
        mh_SiO.invokeExact("mundus", 456);
        Object k = "kosmos"; 
        mh_SiO.invokeExact((String)k, 789);
        o = mh_SiO.invokeExact((String)null, 000);
        o = (Object) mh_SiO.invokeExact("arda", -123);
 
        // sig = ()String 
        s = (String) mh_vS.invokeExact();
 
        // sig = ()int 
        i = (int) mh_vi.invokeExact();
        o = (int) mh_vi.invokeExact();
 
        // sig = ()void 
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:asType, bindTo, type
            methodHandle = METHOD_HANDLE_SLICE;
        } 
        else { 
            methodHandle = METHOD_HANDLE_OBJECT;
        } 
        methodHandle = methodHandle.bindTo(keyEqualsMethod).bindTo(keyType).bindTo(valueType);
 
        // this casting is necessary because otherwise presto byte code generator will generate illegal byte code 
        if (valueType.getJavaType() == void.class) {
            methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(void.class));
        } 
        else { 
            methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(Primitives.wrap(valueType.getJavaType())));
        } 
 
        return new ScalarFunctionImplementation(true, ImmutableList.of(false, false), methodHandle, isDeterministic());
    } 
 
    @UsedByGeneratedCode 
    public static Object subscript(MethodHandle keyEqualsMethod, Type keyType, Type valueType, Block map, boolean key)
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:asType, bindTo, type
            methodHandle = METHOD_HANDLE_SLICE;
        } 
        else { 
            methodHandle = METHOD_HANDLE_OBJECT;
        } 
        methodHandle = methodHandle.bindTo(keyEqualsMethod).bindTo(keyType).bindTo(valueType);
 
        // this casting is necessary because otherwise presto byte code generator will generate illegal byte code 
        if (valueType.getJavaType() == void.class) {
            methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(void.class));
        } 
        else { 
            methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(Primitives.wrap(valueType.getJavaType())));
        } 
 
        return new ScalarFunctionImplementation(true, ImmutableList.of(false, false), methodHandle, isDeterministic());
    } 
 
    @UsedByGeneratedCode 
    public static Object elementAt(MethodHandle keyEqualsMethod, Type keyType, Type valueType, Block map, boolean key)
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:asType, type
     * Apply a transformer as filter. 
     * The filter may not match exactly in the types. In this case needed 
     * additional type transformations are done by {@link MethodHandle#asType(MethodType)}  
     */ 
    public static MethodHandle applyUnsharpFilter(MethodHandle handle, int pos, MethodHandle transformer) {
        MethodType type = transformer.type();
        Class given = handle.type().parameterType(pos);
        if (type.returnType() != given || type.parameterType(0) != given) {
            transformer = transformer.asType(MethodType.methodType(given, type.parameterType(0)));
        } 
        return MethodHandles.filterArguments(handle, pos, transformer);
    } 
 
    /** 
     * returns a transformer later applied as filter to transform one 
     * number into another 
     */ 
    private static MethodHandle selectNumberTransformer(Class param, Object arg) {
        param = TypeHelper.getWrapperClass(param);
 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:type
    private final Map<Long, MethodHandle> bindings = new HashMap<>();
 
    public Binding bind(MethodHandle method)
    { 
        long bindingId = nextId++;
        Binding binding = new Binding(bindingId, method.type());
 
        bindings.put(bindingId, method);
        return binding;
    } 
 
    public Binding bind(Object constant, Class<?> type)
    { 
        return bind(MethodHandles.constant(type, constant));
    } 
 
    public Map<Long, MethodHandle> getBindings()
    { 
        return ImmutableMap.copyOf(bindings);
    } 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:asType, type
This code example shows how to use the following methods:type
import static java.util.Objects.requireNonNull;
 
public final class CachedInstanceBinder
{ 
    private final ClassDefinition classDefinition;
    private final CallSiteBinder callSiteBinder;
    private final Map<FieldDefinition, MethodHandle> initializers = new HashMap<>();
    private int nextId;
 
    public CachedInstanceBinder(ClassDefinition classDefinition, CallSiteBinder callSiteBinder)
    { 
        this.classDefinition = requireNonNull(classDefinition, "classDefinition is null");
        this.callSiteBinder = requireNonNull(callSiteBinder, "callSiteBinder is null");
    } 
 
    public CallSiteBinder getCallSiteBinder()
    { 
        return callSiteBinder;
    } 
 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:asType, type
        } 
        else if (toType.getJavaType() == Slice.class) {
            methodHandle = METHOD_HANDLE_SLICE;
        } 
        else { 
            methodHandle = METHOD_HANDLE_OBJECT.asType(METHOD_HANDLE_OBJECT.type().changeReturnType(toType.getJavaType()));
        } 
        return new ScalarFunctionImplementation(true, ImmutableList.of(true), methodHandle, isDeterministic());
    } 
 
    @UsedByGeneratedCode 
    public static Long toLong(Void arg)
    { 
        return null; 
    } 
 
    @UsedByGeneratedCode 
    public static Double toDouble(Void arg)
    { 
        return null; 
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:invokeWithArguments
    } 
 
    @Override 
    public Object invokeHandle(Object handle, Object[] args) throws Throwable {
        MethodHandle mh = (MethodHandle) handle;
        return mh.invokeWithArguments(args);
    } 
} 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:bindTo
See Code Examples for Java 8 MethodHandle Methods: