本文整理匯總了Java中java.lang.invoke.CallSite.dynamicInvoker方法的典型用法代碼示例。如果您正苦於以下問題:Java CallSite.dynamicInvoker方法的具體用法?Java CallSite.dynamicInvoker怎麽用?Java CallSite.dynamicInvoker使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.CallSite
的用法示例。
在下文中一共展示了CallSite.dynamicInvoker方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testOneType
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
/** calls toString() on integers, twice */
public void testOneType() throws Throwable {
CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(),
"toString",
MethodType.methodType(String.class, Object.class),
0,
DefBootstrap.METHOD_CALL, "");
MethodHandle handle = site.dynamicInvoker();
assertDepthEquals(site, 0);
// invoke with integer, needs lookup
assertEquals("5", (String)handle.invokeExact((Object)5));
assertDepthEquals(site, 1);
// invoked with integer again: should be cached
assertEquals("6", (String)handle.invokeExact((Object)6));
assertDepthEquals(site, 1);
}
示例2: testTwoTypes
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
public void testTwoTypes() throws Throwable {
CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(),
"toString",
MethodType.methodType(String.class, Object.class),
0,
DefBootstrap.METHOD_CALL, "");
MethodHandle handle = site.dynamicInvoker();
assertDepthEquals(site, 0);
assertEquals("5", (String)handle.invokeExact((Object)5));
assertDepthEquals(site, 1);
assertEquals("1.5", (String)handle.invokeExact((Object)1.5f));
assertDepthEquals(site, 2);
// both these should be cached
assertEquals("6", (String)handle.invokeExact((Object)6));
assertDepthEquals(site, 2);
assertEquals("2.5", (String)handle.invokeExact((Object)2.5f));
assertDepthEquals(site, 2);
}
示例3: testTooManyTypes
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
public void testTooManyTypes() throws Throwable {
// if this changes, test must be rewritten
assertEquals(5, DefBootstrap.PIC.MAX_DEPTH);
CallSite site = DefBootstrap.bootstrap(MethodHandles.publicLookup(),
"toString",
MethodType.methodType(String.class, Object.class),
0,
DefBootstrap.METHOD_CALL, "");
MethodHandle handle = site.dynamicInvoker();
assertDepthEquals(site, 0);
assertEquals("5", (String)handle.invokeExact((Object)5));
assertDepthEquals(site, 1);
assertEquals("1.5", (String)handle.invokeExact((Object)1.5f));
assertDepthEquals(site, 2);
assertEquals("6", (String)handle.invokeExact((Object)6L));
assertDepthEquals(site, 3);
assertEquals("3.2", (String)handle.invokeExact((Object)3.2d));
assertDepthEquals(site, 4);
assertEquals("foo", (String)handle.invokeExact((Object)"foo"));
assertDepthEquals(site, 5);
assertEquals("c", (String)handle.invokeExact((Object)'c'));
assertDepthEquals(site, 5);
}
示例4: check_bootstrap_varargs
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void check_bootstrap_varargs() throws Throwable {
MethodHandle handle = lookup().findStatic(ClosureCallSupportTest.class, "concat", genericMethodType(0, true));
FunctionReference funRef = new FunctionReference(handle);
CallSite callSite = ClosureCallSupport.bootstrap(lookup(), "closure", methodType(Object.class, FunctionReference.class, Object.class, Object.class), 0);
MethodHandle invoker = callSite.dynamicInvoker();
assertThat((String) invoker.invokeWithArguments(funRef, 1, 2), is("12"));
handle = lookup().findStatic(ClosureCallSupportTest.class, "concat", genericMethodType(0, true));
funRef = new FunctionReference(handle);
callSite = ClosureCallSupport.bootstrap(lookup(), "closure", methodType(Object.class, FunctionReference.class, Object.class), 0);
invoker = callSite.dynamicInvoker();
assertThat((String) invoker.invokeWithArguments(funRef, 1), is("1"));
handle = lookup().findStatic(ClosureCallSupportTest.class, "concat", genericMethodType(0, true));
funRef = new FunctionReference(handle);
callSite = ClosureCallSupport.bootstrap(lookup(), "closure", methodType(Object.class, FunctionReference.class), 0);
invoker = callSite.dynamicInvoker();
assertThat((String) invoker.invokeWithArguments(funRef), is(""));
handle = lookup().findStatic(ClosureCallSupportTest.class, "concat", genericMethodType(0, true));
funRef = new FunctionReference(handle);
callSite = ClosureCallSupport.bootstrap(lookup(), "closure", methodType(Object.class, FunctionReference.class, Object.class), 0);
invoker = callSite.dynamicInvoker();
assertThat((String) invoker.invokeWithArguments(funRef, new Object[]{1,2}), is("12"));
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:27,代碼來源:ClosureCallSupportTest.java
示例5: dynamic_object_smoke_tests
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void dynamic_object_smoke_tests() throws Throwable {
DynamicObject a = new DynamicObject();
DynamicObject b = new DynamicObject();
CallSite plopper = MethodInvocationSupport.bootstrap(lookup(), "plop", methodType(Object.class, Object.class, Object.class), 1);
MethodHandle invoker = plopper.dynamicInvoker();
invoker.invoke(a, 1);
assertThat(a.get("plop"), is((Object) 1));
invoker.invoke(b, 1);
assertThat(b.get("plop"), is((Object) 1));
invoker.invoke(a, 10);
assertThat(a.get("plop"), is((Object) 10));
assertThat(b.get("plop"), is((Object) 1));
assertThat(invoker.invoke(new Ploper(), 666), is((Object) "666"));
b.undefine("plop");
Object result = invoker.invoke(b, 1);
assertThat(result, is((Object) b));
assertThat(b.get("plop"), is((Object) 1));
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:25,代碼來源:MethodInvocationSupportTest.java
示例6: createComposableInvoker
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
private MethodHandle createComposableInvoker(final boolean isConstructor) {
final MethodHandle handle = getInvokerOrConstructor(isConstructor);
// If compiled function is not optimistic, it can't ever change its invoker/constructor, so just return them
// directly.
if(!canBeDeoptimized()) {
return handle;
}
// Otherwise, we need a new level of indirection; need to introduce a mutable call site that can relink itslef
// to the compiled function's changed target whenever the optimistic assumptions are invalidated.
final CallSite cs = new MutableCallSite(handle.type());
relinkComposableInvoker(cs, this, isConstructor);
return cs.dynamicInvoker();
}
示例7: createComposableInvoker
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
private MethodHandle createComposableInvoker(final boolean isConstructor) {
final MethodHandle handle = getInvokerOrConstructor(isConstructor);
// If compiled function is not optimistic, it can't ever change its invoker/constructor, so just return them
// directly.
if(!canBeDeoptimized()) {
return handle;
}
// Otherwise, we need a new level of indirection; need to introduce a mutable call site that can relink itself
// to the compiled function's changed target whenever the optimistic assumptions are invalidated.
final CallSite cs = new MutableCallSite(handle.type());
relinkComposableInvoker(cs, this, isConstructor);
return cs.dynamicInvoker();
}
示例8: getMethodHandle
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
public static MethodHandle getMethodHandle(Method m, Class<?>... paramTypes) {
try {
Class[] methodParamTypes = m.getParameterTypes();
if (methodParamTypes != null) {
if (methodParamTypes.length == paramTypes.length) {
for (int i = 0; i < methodParamTypes.length; i++) {
if (!paramTypes[i].isAssignableFrom(methodParamTypes[i])) {
// for groovy and other languages that do not do type check at compile time
if (!methodParamTypes[i].equals(Object.class)) {
return null;
}
}
}
} else {
return null;
}
} else {
return null;
}
MethodHandle methodHandle = lookup.unreflect(m);
CallSite callSite = new ConstantCallSite(methodHandle);
return callSite.dynamicInvoker();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
示例9: check_bootstrap
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void check_bootstrap() throws Throwable {
MethodHandle handle = lookup().findStatic(ClosureCallSupportTest.class, "objectToString", genericMethodType(1));
FunctionReference funRef = new FunctionReference(handle);
CallSite callSite = ClosureCallSupport.bootstrap(lookup(), "closure", methodType(Object.class, FunctionReference.class, Object.class), 0);
MethodHandle invoker = callSite.dynamicInvoker();
assertThat((String) invoker.invokeWithArguments(funRef, 123), is("123"));
assertThat((String) invoker.invokeWithArguments(funRef, 123), is("123"));
handle = lookup().findStatic(ClosureCallSupportTest.class, "objectToStringDecorated", genericMethodType(1));
funRef = new FunctionReference(handle);
assertThat((String) invoker.invokeWithArguments(funRef, 123), is("[123]"));
assertThat((String) invoker.invokeWithArguments(funRef, 123), is("[123]"));
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:16,代碼來源:ClosureCallSupportTest.java
示例10: check_bootstrap_besides_Object
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void check_bootstrap_besides_Object() throws Throwable {
MethodHandle handle = lookup().findStatic(ClosureCallSupportTest.class, "parseIntWrap", methodType(Integer.class, String.class));
FunctionReference funRef = new FunctionReference(handle);
CallSite callSite = ClosureCallSupport.bootstrap(lookup(), "closure", methodType(Object.class, FunctionReference.class, Object.class), 0);
MethodHandle invoker = callSite.dynamicInvoker();
assertThat((Integer) invoker.invokeWithArguments(funRef, "123"), is(123));
assertThat((Integer) invoker.invokeWithArguments(funRef, "123"), is(123));
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:10,代碼來源:ClosureCallSupportTest.java
示例11: check_primitive_argument_allowance
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void check_primitive_argument_allowance() throws Throwable {
List<String> list = Arrays.asList("a", "b", "c");
CallSite get = MethodInvocationSupport.bootstrap(lookup(), "get", methodType(Object.class, Object.class, Object.class), 0);
MethodHandle handle = get.dynamicInvoker();
assertThat(((String) handle.invokeWithArguments(list, 0)), is("a"));
assertThat(((String) handle.invokeWithArguments(list, 1)), is("b"));
assertThat(((String) handle.invokeWithArguments(list, 2)), is("c"));
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:11,代碼來源:MethodInvocationSupportTest.java
示例12: nullsafe_invocation
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void nullsafe_invocation() throws Throwable {
CallSite toString = MethodInvocationSupport.bootstrap(lookup(), "toString", methodType(Object.class, Object.class), 1);
MethodHandle invoker = toString.dynamicInvoker();
assertThat(invoker.invoke(null), nullValue());
assertThat((String) invoker.invoke("a"), is("a"));
assertThat((String) invoker.invoke("b"), is("b"));
assertThat(invoker.invoke(null), nullValue());
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:11,代碼來源:MethodInvocationSupportTest.java
示例13: nullsafe_megamorphic_invocation
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void nullsafe_megamorphic_invocation() throws Throwable {
CallSite toString = MethodInvocationSupport.bootstrap(lookup(), "toString", methodType(Object.class, Object.class), 1);
MethodInvocationSupport.InlineCache pic = (MethodInvocationSupport.InlineCache) toString;
pic.depth = MethodInvocationSupport.InlineCache.MEGAMORPHIC_THRESHOLD + 10;
MethodHandle invoker = toString.dynamicInvoker();
assertThat(invoker.invoke(null), nullValue());
assertThat((String) invoker.invoke("a"), is("a"));
assertThat((String) invoker.invoke(1), is("1"));
assertThat((String) invoker.invoke(1L), is("1"));
assertThat((String) invoker.invoke(Arrays.asList()), is("[]"));
assertThat((String) invoker.invoke(new Object()), startsWith("java.lang.Object"));
assertThat(invoker.invoke(null), nullValue());
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:16,代碼來源:MethodInvocationSupportTest.java
示例14: check_many_to_string
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void check_many_to_string() throws Throwable {
CallSite toString = MethodInvocationSupport.bootstrap(lookup(), "toString", methodType(Object.class, Object.class), 0);
MethodHandle toStringMH = toString.dynamicInvoker();
for (int i = 0; i < 5; i++) {
String result = (String) toStringMH.invokeWithArguments(julien());
assertThat(result, is("Person{name='Julien', email='[email protected]'}"));
result = (String) toStringMH.invokeWithArguments("foo");
assertThat(result, is("foo"));
result = (String) toStringMH.invokeWithArguments(666);
assertThat(result, is("666"));
result = (String) toStringMH.invokeWithArguments(666L);
assertThat(result, is("666"));
result = (String) toStringMH.invokeWithArguments("foo");
assertThat(result, is("foo"));
result = (String) toStringMH.invokeWithArguments(new BigInteger("1234"));
assertThat(result, is("1234"));
result = (String) toStringMH.invokeWithArguments(new Object() {
@Override
public String toString() {
return "Damn";
}
});
assertThat(result, is("Damn"));
result = (String) toStringMH.invokeWithArguments(new Object() {
@Override
public String toString() {
return "Plop";
}
});
assertThat(result, is("Plop"));
result = (String) toStringMH.invokeWithArguments(new Object() {
@Override
public String toString() {
return "Hey!";
}
});
assertThat(result, is("Hey!"));
}
}
開發者ID:dynamid,項目名稱:golo-lang-insa-citilab-historical-reference,代碼行數:50,代碼來源:MethodInvocationSupportTest.java