当前位置: 首页>>代码示例>>Java>>正文


Java MethodType.methodType方法代码示例

本文整理汇总了Java中java.lang.invoke.MethodType.methodType方法的典型用法代码示例。如果您正苦于以下问题:Java MethodType.methodType方法的具体用法?Java MethodType.methodType怎么用?Java MethodType.methodType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.invoke.MethodType的用法示例。


在下文中一共展示了MethodType.methodType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateMethodTest7

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
/**
 *  Generate test with an invokedynamic, a static bootstrap method with an extra arg that is a
 *  MethodHandle of kind invoke interface. The target method is a method into an interface
 *  that is shadowed by another definition into a sub interfaces.
 */
private void generateMethodTest7(ClassVisitor cv) {
  MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "test7", "()V",
      null, null);
  MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class,
      MethodType.class, MethodHandle.class);
  Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(InvokeCustom.class),
      "bsmCreateCallCallingtargetMethodTest8", mt.toMethodDescriptorString(), false);
  mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(InvokeCustom.class));
  mv.visitInsn(Opcodes.DUP);
  mv.visitMethodInsn(
      Opcodes.INVOKESPECIAL, Type.getInternalName(InvokeCustom.class), "<init>", "()V", false);
  mv.visitInvokeDynamicInsn("targetMethodTest8", "(Linvokecustom/J;)V", bootstrap,
      new Handle(Opcodes.H_INVOKEINTERFACE, Type.getInternalName(J.class),
          "targetMethodTest8", "()V", true));
  mv.visitInsn(Opcodes.RETURN);
  mv.visitMaxs(-1, -1);
}
 
开发者ID:inferjay,项目名称:r8,代码行数:23,代码来源:TestGenerator.java

示例2: randomMethodTypeGenerator

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
/**
 * Routine used to obtain a randomly generated method type.
 *
 * @param arity Arity of returned method type.
 * @return MethodType generated randomly.
 */
public static MethodType randomMethodTypeGenerator(int arity) {
    final Class<?>[] CLASSES = {
        Object.class,
        int.class,
        boolean.class,
        byte.class,
        short.class,
        char.class,
        long.class,
        float.class,
        double.class
    };
    if (arity > MAX_ARITY) {
        throw new IllegalArgumentException(
                String.format("Arity should not exceed %d!", MAX_ARITY));
    }
    List<Class<?>> list = randomClasses(CLASSES, arity);
    list = getParams(list, false, arity);
    int i = RNG.nextInt(CLASSES.length + 1);
    Class<?> rtype = i == CLASSES.length ? void.class : CLASSES[i];
    return MethodType.methodType(rtype, list);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:29,代码来源:Helper.java

示例3: getPropertyTest2

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "flags")
public void getPropertyTest2(final boolean publicLookup) throws Throwable {
    final MethodType mt = MethodType.methodType(Object.class, Object.class);
    final CallSite cs = createCallSite(publicLookup, GET_PROPERTY, "class", mt);
    Assert.assertEquals(cs.getTarget().invoke(new Object()), Object.class);
    Assert.assertEquals(cs.getTarget().invoke(new Date()), Date.class);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:BeanLinkerTest.java

示例4: zeroConstantFunction

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static MethodHandle zeroConstantFunction(Wrapper wrap) {
    WrapperCache cache = CONSTANT_FUNCTIONS[0];
    MethodHandle mh = cache.get(wrap);
    if (mh != null) {
        return mh;
    }
    // slow path
    MethodType type = MethodType.methodType(wrap.primitiveType());
    switch (wrap) {
        case VOID:
            mh = EMPTY;
            break;
        case OBJECT:
        case INT: case LONG: case FLOAT: case DOUBLE:
            try {
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
            } catch (ReflectiveOperationException ex) {
                mh = null;
            }
            break;
    }
    if (mh != null) {
        return cache.put(wrap, mh);
    }

    // use zeroInt and cast the result
    if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
        mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
        return cache.put(wrap, mh);
    }
    throw new IllegalArgumentException("cannot find zero constant for " + wrap);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:33,代码来源:ValueConversions.java

示例5: testFindConstructor

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
void testFindConstructor(boolean positive, Lookup lookup,
                         Class<?> defc, Class<?>... params) throws Throwable {
    countTest(positive);
    MethodType type = MethodType.methodType(void.class, params);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" <init>"+type);
        target = lookup.findConstructor(defc, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertTrue(noAccess.getClass().getName(), noAccess instanceof IllegalAccessException);
    }
    if (verbosity >= 3)
        System.out.println("findConstructor "+defc.getName()+".<init>/"+type+" => "+target
                           +(target == null ? "" : target.type())
                           +(noAccess == null ? "" : " !! "+noAccess));
    if (positive && noAccess != null)  throw noAccess;
    assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
    if (!positive)  return; // negative test failed as expected
    assertEquals(type.changeReturnType(defc), target.type());
    Object[] args = randomArgs(params);
    printCalled(target, defc.getSimpleName(), args);
    Object obj = target.invokeWithArguments(args);
    if (!(defc == Example.class && params.length < 2))
        assertCalled(defc.getSimpleName()+".<init>", args);
    assertTrue("instance of "+defc.getName(), defc.isInstance(obj));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:MethodHandlesTest.java

示例6: testMake_MethodType

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
/**
 * Test of make method, of class MethodType.
 */
@Test
public void testMake_MethodType() {
    System.out.println("make (from rtype, MethodType)");
    MethodType expResult = mt_iO2;
    MethodType result = MethodType.methodType(int.class, mt_IO2);
    assertSame(expResult, result);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:MethodTypeTest.java

示例7: testBind

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
void testBind(boolean positive, Lookup lookup, Class<?> defc, Class<?> ret, String name, Class<?>... params) throws Throwable {
    countTest(positive);
    String methodName = name.substring(1 + name.indexOf('/'));  // foo/bar => foo
    MethodType type = MethodType.methodType(ret, params);
    Object receiver = randomArg(defc);
    MethodHandle target = null;
    Exception noAccess = null;
    try {
        if (verbosity >= 4)  System.out.println("lookup via "+lookup+" of "+defc+" "+name+type);
        target = maybeMoveIn(lookup, defc).bind(receiver, methodName, type);
    } catch (ReflectiveOperationException ex) {
        noAccess = ex;
        assertExceptionClass(
            (name.contains("bogus") || INIT_REF_CAUSES_NSME && name.contains("<init>"))
            ?   NoSuchMethodException.class
            :   IllegalAccessException.class,
            noAccess);
        if (verbosity >= 5)  ex.printStackTrace(System.out);
    }
    if (verbosity >= 3)
        System.out.println("bind "+receiver+"."+name+"/"+type+" => "+target
                +(noAccess == null ? "" : " !! "+noAccess));
    if (positive && noAccess != null)  throw noAccess;
    assertEquals(positive ? "positive test" : "negative test erroneously passed", positive, target != null);
    if (!positive)  return; // negative test failed as expected
    assertEquals(type, target.type());
    Object[] args = randomArgs(params);
    printCalled(target, name, args);
    target.invokeWithArguments(args);
    Object[] argsWithReceiver = cat(array(Object[].class, receiver), args);
    assertCalled(name, argsWithReceiver);
    if (verbosity >= 1)
        System.out.print(':');
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:MethodHandlesTest.java

示例8: varargsArray

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static MethodHandle varargsArray(Class<?> arrayType, int nargs) {
    Class<?> elemType = arrayType.getComponentType();
    MethodType vaType = MethodType.methodType(arrayType, Collections.<Class<?>>nCopies(nargs, elemType));
    MethodHandle mh = varargsArray(nargs);
    if (arrayType != Object[].class)
        mh = MethodHandles.filterReturnValue(mh, CHANGE_ARRAY_TYPE.bindTo(arrayType));
    return mh.asType(vaType);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:MethodHandlesTest.java

示例9: testInvokePolymorphicRange

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public void testInvokePolymorphicRange() {
  MethodType mt = MethodType.methodType(String.class, byte.class, char.class, short.class,
      float.class, double.class, long.class, Integer.class, int.class, String.class);
  MethodHandles.Lookup lk = MethodHandles.lookup();

  try {
    MethodHandle mh = lk.findVirtual(getClass(), "buildString", mt);
    System.out.println(
        mh.invoke(this, (byte) 2, 'a', (short) 0xFFFF, 1.1f, 2.24d, 12345678L, null,
            1, "string"));
  } catch (Throwable t) {
    t.printStackTrace();
  }
}
 
开发者ID:inferjay,项目名称:r8,代码行数:15,代码来源:InvokePolymorphic.java

示例10: getArrayCreatorMethodType

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
private static MethodType getArrayCreatorMethodType(final MethodType type) {
    final Class<?>[] callParamTypes = type.parameterArray();
    for(int i = 0; i < callParamTypes.length; ++i) {
        callParamTypes[i] = getNashornParamType(callParamTypes[i], true);
    }
    return MethodType.methodType(Object[].class, callParamTypes);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:JavaAdapterBytecodeGenerator.java

示例11: main

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
    MethodHandle mh = MethodHandles.lookup().findVirtual(CharSequence.class, "toString", MethodType.methodType(String.class));
    MethodType mt = MethodType.methodType(Object.class, CharSequence.class);
    mh = mh.asType(mt);

    Object res = mh.invokeExact((CharSequence)"123");

    System.out.println("TEST PASSED");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:ObjectMethodInInterfaceTest.java

示例12: getPropertyNegativeTest

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Test(dataProvider = "flags")
public void getPropertyNegativeTest(final boolean publicLookup) throws Throwable {
    final MethodType mt = MethodType.methodType(Object.class, Object.class, String.class);
    final CallSite cs = createCallSite(publicLookup, GET_PROPERTY, mt);
    Assert.assertNull(cs.getTarget().invoke(new Object(), "DOES_NOT_EXIST"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:7,代码来源:BeanLinkerTest.java

示例13: invokeMethod

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
protected void invokeMethod(Class<?> vcls, int expected) throws Throwable {
    MethodType mt = MethodType.methodType(int.class);
    MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
    Assert.assertEquals(expected, (int) mh.invoke(vcls.newInstance()));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:MultiReleaseJarProperties.java

示例14: methodType

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
public static MethodType methodType(Method method) {
    return MethodType.methodType(method.getReturnType(), method.getParameterTypes());
}
 
开发者ID:OvercastNetwork,项目名称:ProjectAres,代码行数:4,代码来源:Methods.java

示例15: type

import java.lang.invoke.MethodType; //导入方法依赖的package包/类
@Override
public MethodType type(final Class<?> returnType, final Class<?>... paramTypes) {
    final MethodType mt = MethodType.methodType(returnType, paramTypes);
    log.log(TRACE_LEVEL, "methodType ", returnType, " ", Arrays.toString(paramTypes), " ", mt);
    return mt;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:7,代码来源:MethodHandleFactory.java


注:本文中的java.lang.invoke.MethodType.methodType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。