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


Java MethodHandleProxies.asInterfaceInstance方法代码示例

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


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

示例1: testRunnableProxy0

import java.lang.invoke.MethodHandleProxies; //导入方法依赖的package包/类
public void testRunnableProxy0() throws Throwable {
    if (CAN_SKIP_WORKING)  return;
    startTest("testRunnableProxy");
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle run = lookup.findStatic(lookup.lookupClass(), "runForRunnable", MethodType.methodType(void.class));
    Runnable r = MethodHandleProxies.asInterfaceInstance(Runnable.class, run);
    testRunnableProxy(r);
    assertCalled("runForRunnable");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:MethodHandlesTest.java

示例2: testRunnableMethodHandle

import java.lang.invoke.MethodHandleProxies; //导入方法依赖的package包/类
/**
 * MethodHandleProxies will add qualified export of sun.invoke from java.base
 * to a dynamic module
 */
@Test
public static void testRunnableMethodHandle() throws Exception {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType mt = MethodType.methodType(void.class);
    MethodHandle mh = lookup.findStatic(ProxyForMethodHandle.class, "runForRunnable", mt);
    Runnable proxy = MethodHandleProxies.asInterfaceInstance(Runnable.class, mh);
    proxy.run();

    Class<?> proxyClass = proxy.getClass();
    Module target = proxyClass.getModule();
    assertDynamicModule(target, proxyClass.getClassLoader(), proxyClass);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ProxyForMethodHandle.java

示例3: testRunnableMethodHandle

import java.lang.invoke.MethodHandleProxies; //导入方法依赖的package包/类
/**
 * MethodHandleProxies will add qualified export of sun.invoke from java.base
 * to a dynamic module
 */
@Test
static void testRunnableMethodHandle() throws Exception {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodType mt = MethodType.methodType(void.class);
    MethodHandle mh = lookup.findStatic(ProxyForMethodHandle.class, "runForRunnable", mt);
    Runnable proxy = MethodHandleProxies.asInterfaceInstance(Runnable.class, mh);
    proxy.run();

    Class<?> proxyClass = proxy.getClass();
    Module target = proxyClass.getModule();
    assertDynamicModule(target, proxyClass.getClassLoader(), proxyClass);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:17,代码来源:ProxyForMethodHandle.java

示例4: testProxy

import java.lang.invoke.MethodHandleProxies; //导入方法依赖的package包/类
public void testProxy() throws NoSuchMethodException, IllegalAccessException {
    MethodHandle m = MethodHandles.lookup().findStatic(MHProxiesTest.class, "m",
                                                       MethodType.methodType(Byte.class, int.class));
    Sam s = MethodHandleProxies.asInterfaceInstance(Sam.class, m);
    assertEquals(66d, s.m(66));
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:7,代码来源:MHProxiesTest.java

示例5: proxy

import java.lang.invoke.MethodHandleProxies; //导入方法依赖的package包/类
private static Object proxy(Class<?> interfaze, MethodHandle target) {
  return MethodHandleProxies.asInterfaceInstance(interfaze, target);
}
 
开发者ID:forax,项目名称:8to6,代码行数:4,代码来源:Enhancements.java

示例6: forInterface

import java.lang.invoke.MethodHandleProxies; //导入方法依赖的package包/类
public <I> I forInterface (final Class<I> iface) {
	if (iface == null) {
		throw new IllegalArgumentException ("iface cannot be null");
	}
	if (!iface.isInterface ()) {
		throw new IllegalArgumentException (String.format ("%s must be an interface", iface.toString ()));
	}
	
	final Method[] methods = iface.getMethods ();
	if (methods.length != 1) {
		throw new IllegalArgumentException (String.format ("%s must have exactly one method", iface.toString ()));
	}
	
	final Method method = methods[0];
	final ExecutableExpression<C, ?> lastExpression = plan.getExecutionSteps().get (plan.getExecutionSteps ().size () - 1).executor.expression;

	// Return types must match:
	if (!lastExpression.getResultType ().equals (method.getReturnType ())) {
		throw new IllegalArgumentException (String.format (
				"Method %s has unexpected return type %s, while expecting %s", 
				method.toString (), 
				lastExpression.getResultType ().toString (), 
				method.getReturnType ().toString ()
			));
	}
	
	// Interface must have proper input types:
	final Class<?>[] parameterTypes = method.getParameterTypes ();
	if (parameterTypes.length != inputClasses.size () + 1) {
		throw new IllegalArgumentException (String.format (
				"Method %s has an invalid number of arguments %d, expected %d",
				method.toString (),
				parameterTypes.length,
				inputClasses.size () + 1
			));
	}
	if (!parameterTypes[0].isAssignableFrom (contextClass)) {
		throw new IllegalArgumentException (String.format (
				"First parameter of %s must is of unexpected type %s, expecting (a subclass of) %s",
				method.toString (),
				parameterTypes[0].toString (),
				contextClass.toString ()
			));
	}
	final Class<?> actualContextClass = parameterTypes[0];
	for (int i = 0; i < inputClasses.size (); ++ i) {
		if (!parameterTypes[i + 1].isAssignableFrom (inputClasses.get (i))) {
			throw new IllegalArgumentException (String.format (
					"Parameter %d of %s must be assignable from %s",
					i + 1,
					method.toString (),
					inputClasses.get (i).toString ()
				));
		}
	}
	
	// Bind the execute handle to the
	final MethodHandle executeThisHandle = executeHandle.bindTo (this);
	
	// Convert to a specific type:
	final Class<?> returnType = lastExpression.getResultType ();
	final Class<?>[] castParameterTypes = new Class<?>[inputClasses.size () + 1];
	
	castParameterTypes[0] = actualContextClass;
	for (int i = 0; i < inputClasses.size (); ++ i) {
		castParameterTypes[i + 1] = inputClasses.get (i);
	}
	
	final MethodHandle castExecuteHandle = executeThisHandle
			.asVarargsCollector (Object[].class)
			.asType (MethodType.methodType (returnType, castParameterTypes));
	
	// Create an interface wrapper for the resulting method handle:
	return MethodHandleProxies.asInterfaceInstance (iface, castExecuteHandle);
}
 
开发者ID:CDS-INSPIRE,项目名称:InSpider,代码行数:76,代码来源:Executor.java

示例7: asInterfaceInstance

import java.lang.invoke.MethodHandleProxies; //导入方法依赖的package包/类
/**
 * Turns a function reference into an instance of a single-method interface.
 *
 * @param interfaceClass the target single-method interface class.
 * @param target         the implementation function reference.
 * @return an instance of {@code interfaceClass}.
 * @see java.lang.invoke.MethodHandleProxies#asInterfaceInstance(Class, java.lang.invoke.MethodHandle)
 */
public static Object asInterfaceInstance(Object interfaceClass, Object target) {
  require(interfaceClass instanceof Class, "interfaceClass must be a Class");
  require(target instanceof FunctionReference, "target must be a FunctionReference");
  return MethodHandleProxies.asInterfaceInstance((Class<?>) interfaceClass, ((FunctionReference) target).handle());
}
 
开发者ID:dynamid,项目名称:golo-lang-insa-citilab-historical-reference,代码行数:14,代码来源:Predefined.java


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