Java Code Examples for java.lang.invoke.MethodHandles#permuteArguments()

The following examples show how to use java.lang.invoke.MethodHandles#permuteArguments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PermuteArgsReturnVoidTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnFromArg() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = dropArguments(
            identity(String.class), 1, int.class, int.class);
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 2
Source File: PermuteArgsReturnVoidTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnOnStack() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeIdentity",
            MethodType.methodType(String.class, String.class, int.class, int.class));
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 3
Source File: PermuteArgsReturnVoidTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnFromArg() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = dropArguments(
            identity(String.class), 1, int.class, int.class);
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 4
Source File: PermuteArgsReturnVoidTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnOnStack() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeIdentity",
            MethodType.methodType(String.class, String.class, int.class, int.class));
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 5
Source File: PermuteArgsReturnVoidTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnFromArg() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = dropArguments(
            identity(String.class), 1, int.class, int.class);
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 6
Source File: PermuteArgsReturnVoidTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnOnStack() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeIdentity",
            MethodType.methodType(String.class, String.class, int.class, int.class));
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 7
Source File: PermuteArgsReturnVoidTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnOnStack() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeIdentity",
            MethodType.methodType(String.class, String.class, int.class, int.class));
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 8
Source File: PermuteArgsReturnVoidTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnFromArg() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = dropArguments(
            identity(String.class), 1, int.class, int.class);
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 9
Source File: IndyArrayAccess.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static MethodHandle buildSetter(Class<?> arrayClass) {
    MethodHandle set = MethodHandles.arrayElementSetter(arrayClass);
    MethodHandle fallback = MethodHandles.explicitCastArguments(set, set.type().changeParameterType(0, Object.class));

    fallback = MethodHandles.dropArguments(fallback, 3, int.class);
    MethodType reorderType = fallback.type().
            insertParameterTypes(0, int.class).
            dropParameterTypes(4, 5);
    fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 3, 0);

    fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
    fallback = MethodHandles.explicitCastArguments(fallback, set.type());

    MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
    MethodHandle handle = MethodHandles.guardWithTest(guard, set, fallback);
    return handle;
}
 
Example 10
Source File: PermuteArgsReturnVoidTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnFromArg() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = dropArguments(
            identity(String.class), 1, int.class, int.class);
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 11
Source File: PermuteArgsReturnVoidTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testReturnFromArg() throws Throwable {
    MethodHandles.Lookup l = MethodHandles.lookup();

    MethodHandle consumeIdentity = dropArguments(
            identity(String.class), 1, int.class, int.class);
    MethodHandle consumeVoid = l.findStatic(
            PermuteArgsReturnVoidTest.class, "consumeVoid",
            MethodType.methodType(void.class, String.class, int.class, int.class));

    MethodHandle f = MethodHandles.foldArguments(consumeIdentity, consumeVoid);

    MethodHandle p = MethodHandles.permuteArguments(f, MethodType.methodType(String.class, String.class, int.class, int.class), 0, 2, 1);

    String s = (String) p.invoke("IN", 0, 0);
    Assert.assertEquals(s.getClass(), String.class);
    Assert.assertEquals(s, "IN");
}
 
Example 12
Source File: MethodHandleUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * @param f (U, V)R
 * @param g (S1, S2, ..., Sm)U
 * @param h (T1, T2, ..., Tn)V
 * @return (S1, S2, ..., Sm, T1, T2, ..., Tn)R
 */
public static MethodHandle compose(MethodHandle f, MethodHandle g, MethodHandle h)
{
    if (f.type().parameterCount() != 2) {
        throw new IllegalArgumentException(format("f.parameterCount != 2. f: %s", f.type()));
    }
    if (f.type().parameterType(0) != g.type().returnType()) {
        throw new IllegalArgumentException(format("f.parameter(0) != g.return. f: %s  g: %s", f.type(), g.type()));
    }
    if (f.type().parameterType(1) != h.type().returnType()) {
        throw new IllegalArgumentException(format("f.parameter(0) != h.return. f: %s  h: %s", f.type(), h.type()));
    }

    // (V, T1, T2, ..., Tn, U)R
    MethodType typeVTU = f.type().dropParameterTypes(0, 1).appendParameterTypes(h.type().parameterList()).appendParameterTypes(f.type().parameterType(0));
    // Semantics: f => f
    // Type: (U, V)R => (V, T1, T2, ..., Tn, U)R
    MethodHandle fVTU = MethodHandles.permuteArguments(f, typeVTU, h.type().parameterCount() + 1, 0);
    // Semantics: f => fh
    // Type: (V, T1, T2, ..., Tn, U)R => (T1, T2, ..., Tn, U)R
    MethodHandle fhTU = MethodHandles.foldArguments(fVTU, h);

    // reorder: [m+1, m+2, ..., m+n, 0]
    int[] reorder = new int[fhTU.type().parameterCount()];
    for (int i = 0; i < reorder.length - 1; i++) {
        reorder[i] = i + 1 + g.type().parameterCount();
    }
    reorder[reorder.length - 1] = 0;

    // (U, S1, S2, ..., Sm, T1, T2, ..., Tn)R
    MethodType typeUST = f.type().dropParameterTypes(1, 2).appendParameterTypes(g.type().parameterList()).appendParameterTypes(h.type().parameterList());
    // Semantics: f.h => f.h
    // Type: (T1, T2, ..., Tn, U)R => (U, S1, S2, ..., Sm, T1, T2, ..., Tn)R
    MethodHandle fhUST = MethodHandles.permuteArguments(fhTU, typeUST, reorder);

    // Semantics: fh => fgh
    // Type: (U, S1, S2, ..., Sm, T1, T2, ..., Tn)R => (S1, S2, ..., Sm, T1, T2, ..., Tn)R
    return MethodHandles.foldArguments(fhUST, g);
}
 
Example 13
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static MethodHandle permute(MethodHandle h) {
    // The handles representing virtual methods need to be rearranged to match the required order of arguments
    // (loop-local state comes first, then loop arguments). As the receiver comes first in the signature but is
    // a loop argument, it must be moved to the appropriate position in the signature.
    return MethodHandles.permuteArguments(h,
            methodType(h.type().returnType(), int.class, int.class, LOOP_WITH_VIRTUALS, int.class), 2, 0, 1, 3);
}
 
Example 14
Source File: ObjectMethods.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a method handle for the {@code hashCode} method for a given data class
 * @param receiverClass   the data class
 * @param getters         the list of getters
 * @return the method handle
 */
private static MethodHandle makeHashCode(Class<?> receiverClass,
                                        List<MethodHandle> getters) {
    MethodHandle accumulator = MethodHandles.dropArguments(ZERO, 0, receiverClass); // (R)I

    // @@@ Use loop combinator instead?
    for (MethodHandle getter : getters) {
        MethodHandle hasher = hasher(getter.type().returnType()); // (T)I
        MethodHandle hashThisField = MethodHandles.filterArguments(hasher, 0, getter);    // (R)I
        MethodHandle combineHashes = MethodHandles.filterArguments(HASH_COMBINER, 0, accumulator, hashThisField); // (RR)I
        accumulator = MethodHandles.permuteArguments(combineHashes, accumulator.type(), 0, 0); // adapt (R)I to (RR)I
    }

    return accumulator;
}
 
Example 15
Source File: MethodStubResult_N.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
protected MethodHandle initMethodHandle(ServicesAmp ampManager,
                                        Method method)
  throws IllegalAccessException
{
  Class<?> []paramTypes = method.getParameterTypes();
  int paramLen = paramTypes.length;
  int resultOffset = findResultOffset(paramTypes);

  method.setAccessible(true);
  MethodHandle mh = MethodHandles.lookup().unreflect(method);
  
  int []permute = new int[paramLen + 1];
  
  permute[0] = 0;
  for (int i = 0; i < resultOffset; i++) {
    permute[i + 1] = i + 2;
  }
  for (int i = resultOffset + 1; i < paramLen; i++) {
    permute[i + 1] = i + 1;
  }
  permute[resultOffset + 1] = 1;
  
  MethodType type = MethodType.genericMethodType(paramLen + 1);
  type = type.changeReturnType(void.class);
  
  mh = mh.asType(type);
  
  mh = filterMethod(ampManager,
                    mh,
                    method);
   
  mh = MethodHandles.permuteArguments(mh, type, permute);
  
  /*
  if (paramLen > 0 && ! Object[].class.equals(paramTypes[paramLen - 1])) {
  }
  */
  mh = mh.asSpreader(Object[].class, paramLen - 1);
  
  type = MethodType.methodType(void.class, 
                               Object.class,
                               getResultClass(),
                               Object[].class);

  return mh.asType(type);
}
 
Example 16
Source File: Guards.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle constantBoolean(final Boolean value, final MethodType type) {
    return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value),
            type.changeReturnType(Boolean.TYPE));
}
 
Example 17
Source File: Guards.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle constantBoolean(final Boolean value, final MethodType type) {
    return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value),
            type.changeReturnType(Boolean.TYPE));
}
 
Example 18
Source File: ArrayJoin.java    From presto with Apache License 2.0 4 votes vote down vote up
private static ScalarFunctionImplementation specializeArrayJoin(BoundVariables types, Metadata metadata, List<Boolean> nullableArguments, MethodHandle methodHandle)
{
    Type type = types.getTypeVariable("T");
    List<ArgumentProperty> argumentProperties = nullableArguments.stream()
            .map(nullable -> nullable
                    ? valueTypeArgumentProperty(USE_BOXED_TYPE)
                    : valueTypeArgumentProperty(RETURN_NULL_ON_NULL))
            .collect(toImmutableList());

    if (type instanceof UnknownType) {
        return new ScalarFunctionImplementation(
                false,
                argumentProperties,
                methodHandle.bindTo(null),
                Optional.of(STATE_FACTORY));
    }
    else {
        try {
            ResolvedFunction resolvedFunction = metadata.getCoercion(type, VARCHAR);
            MethodHandle cast = metadata.getScalarFunctionInvoker(resolvedFunction, Optional.empty()).getMethodHandle();

            MethodHandle getter;
            Class<?> elementType = type.getJavaType();
            if (elementType == boolean.class) {
                getter = GET_BOOLEAN;
            }
            else if (elementType == double.class) {
                getter = GET_DOUBLE;
            }
            else if (elementType == long.class) {
                getter = GET_LONG;
            }
            else if (elementType == Slice.class) {
                getter = GET_SLICE;
            }
            else {
                throw new UnsupportedOperationException("Unsupported type: " + elementType.getName());
            }

            // if the cast doesn't take a ConnectorSession, create an adapter that drops the provided session
            if (cast.type().parameterArray()[0] != ConnectorSession.class) {
                cast = MethodHandles.dropArguments(cast, 0, ConnectorSession.class);
            }

            // Adapt a target cast that takes (ConnectorSession, ?) to one that takes (Block, int, ConnectorSession), which will be invoked by the implementation
            // The first two arguments (Block, int) are filtered through the element type's getXXX method to produce the underlying value that needs to be passed to
            // the cast.
            cast = MethodHandles.permuteArguments(cast, MethodType.methodType(Slice.class, cast.type().parameterArray()[1], cast.type().parameterArray()[0]), 1, 0);
            cast = MethodHandles.dropArguments(cast, 1, int.class);
            cast = MethodHandles.dropArguments(cast, 1, Block.class);
            cast = MethodHandles.foldArguments(cast, getter.bindTo(type));

            MethodHandle target = MethodHandles.insertArguments(methodHandle, 0, cast);
            return new ScalarFunctionImplementation(
                    false,
                    argumentProperties,
                    target,
                    Optional.of(STATE_FACTORY));
        }
        catch (PrestoException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Input type %s not supported", type), e);
        }
    }
}
 
Example 19
Source File: Guards.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle constantBoolean(final Boolean value, final MethodType type) {
    return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value),
            type.changeReturnType(Boolean.TYPE));
}
 
Example 20
Source File: Guards.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle constantBoolean(Boolean value, MethodType type) {
    return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value),
            type.changeReturnType(Boolean.TYPE));
}