當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。