本文整理汇总了Java中java.lang.invoke.ConstantCallSite类的典型用法代码示例。如果您正苦于以下问题:Java ConstantCallSite类的具体用法?Java ConstantCallSite怎么用?Java ConstantCallSite使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConstantCallSite类属于java.lang.invoke包,在下文中一共展示了ConstantCallSite类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: registerCallSitePlugins
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
private static void registerCallSitePlugins(InvocationPlugins plugins) {
InvocationPlugin plugin = new InvocationPlugin() {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
ValueNode callSite = receiver.get();
ValueNode folded = CallSiteTargetNode.tryFold(GraphUtil.originalValue(callSite), b.getMetaAccess(), b.getAssumptions());
if (folded != null) {
b.addPush(JavaKind.Object, folded);
} else {
b.addPush(JavaKind.Object, new CallSiteTargetNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), callSite));
}
return true;
}
@Override
public boolean inlineOnly() {
return true;
}
};
plugins.register(plugin, ConstantCallSite.class, "getTarget", Receiver.class);
plugins.register(plugin, MutableCallSite.class, "getTarget", Receiver.class);
plugins.register(plugin, VolatileCallSite.class, "getTarget", Receiver.class);
}
示例2: bootstrapMethod
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
/**
* A bootstrap method for invokedynamic
* @param lookup a lookup object
* @param methodName methodName
* @param type method type
* @return CallSite for method
*/
public static CallSite bootstrapMethod(MethodHandles.Lookup lookup,
String methodName, MethodType type) throws IllegalAccessException,
NoSuchMethodException {
MethodType mtype = MethodType.methodType(boolean.class,
new Class<?>[]{int.class, long.class, float.class,
double.class, String.class});
return new ConstantCallSite(lookup.findVirtual(lookup.lookupClass(),
methodName, mtype));
}
示例3: bsm_op
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
public static CallSite bsm_op(Lookup lookup, String name, MethodType methodType, Linker linker) {
//System.out.println("link op " + name + methodType);
MethodHandle[] mhs = Ops.OP_MAP.get(name);
if (mhs == null) {
throw new UnsupportedOperationException(name + methodType);
}
MethodHandle target;
if (mhs[0].type() == methodType) {
target = mhs[0];
} else {
target = mhs[1];
if (target.type().returnType() != methodType.returnType()) {
target = MethodHandles.filterReturnValue(target, checkTypeAndConvert(methodType.returnType()));
}
target = target.asType(methodType);
}
return new ConstantCallSite(target);
}
示例4: getCallSite
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
public CallSite getCallSite(String name, MethodType methodType) {
// try built-ins first
if (name.equals("print")) {
MethodHandle mh;
try {
mh = MethodHandles.publicLookup().findVirtual(PrintStream.class, "println",
MethodType.methodType(void.class, methodType.parameterType(0)));
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new AssertionError(e);
}
mh = mh.bindTo(System.out);
return new ConstantCallSite(mh.asType(mh.type().changeReturnType(methodType.returnType())));
}
Function function = functionMap.get(name);
if (function == null || function.fn.parameters().size() != methodType.parameterCount()) {
throw new IllegalStateException("no function matching " + name + methodType + " found");
}
return function.createCallSite(this, name, methodType);
}
示例5: bsm_optimistic_failure
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
/**
* Bootstrap method called called in a deoptimisation path when a return value
* doesn't fit in the return type.
*
* @param lookup the lookup object.
* @param name the name of the method
* @param methodType always (Object)OptimisiticError
* @param deoptRet the callback to cause to indicate a deopt error.
* @param deoptRetCsts the constant arguments of deoptRet
* @return a call site
* @throws Throwable if an error occurs
*/
// called by generated code
public static CallSite bsm_optimistic_failure(Lookup lookup, String name, MethodType methodType, MethodHandle deoptRet, Object... deoptRetCsts) throws Throwable {
//System.out.println("bsm_optimistic_failure " + name + " with " + deoptRet + "/" + deoptRetCsts.length);
// do some checks
//if (!deoptRet.type().equals(MethodType.methodType(boolean.class, Object.class))) {
// throw new WrongMethodTypeException("invalid deop callback signature ! " + deoptRet.type());
//}
// prepend lookup/name/methodType
deoptRet = MethodHandles.insertArguments(deoptRet, 0, lookup, name, methodType);
// bundle deoptRet constant arguments with deoptRet if necessary
if (deoptRetCsts.length != 0) {
deoptRet = MethodHandles.insertArguments(deoptRet, 1, deoptRetCsts);
}
return new ConstantCallSite(
MethodHandles.filterReturnValue(OPTIMISTIC_ERROR_VALUE,
deoptCallback(MethodHandles.identity(Object.class), deoptRet)));
}
示例6: reduceBy
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
/**
* Reduce by functional support for int arrays.
* @param array array of items to reduce by
* @param object object that contains the reduce by function
* @param <T> the type of object
* @return the final reduction
*/
public static <T> long reduceBy( final long[] array, T object ) {
if (object.getClass().isAnonymousClass()) {
return reduceByR(array, object );
}
try {
ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
MethodHandle methodHandle = callSite.dynamicInvoker();
try {
long sum = 0;
for ( long v : array ) {
sum = (long) methodHandle.invokeExact( sum, v );
}
return sum;
} catch (Throwable throwable) {
return handle(Long.class, throwable, "Unable to perform reduceBy");
}
} catch (Exception ex) {
return reduceByR(array, object);
}
}
示例7: reduceBy
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
/**
* Reduce by functional support for int arrays.
* @param array array of items to reduce by
* @param object object that contains the reduce by function
* @param <T> the type of object
* @return the final reduction
*/
public static <T> double reduceBy( final float[] array, T object ) {
if (object.getClass().isAnonymousClass()) {
return reduceByR(array, object );
}
try {
ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
MethodHandle methodHandle = callSite.dynamicInvoker();
try {
double sum = 0;
for ( float v : array ) {
sum = (double) methodHandle.invokeExact( sum, v );
}
return sum;
} catch (Throwable throwable) {
return Exceptions.handle(Long.class, throwable, "Unable to perform reduceBy");
}
} catch (Exception ex) {
return reduceByR(array, object);
}
}
示例8: reduceBy
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
/**
* Reduce by functional support for int arrays.
* @param array array of items to reduce by
* @param object object that contains the reduce by function
* @param <T> the type of object
* @return the final reduction
*/
public static <T> long reduceBy( final int[] array, T object ) {
if (object.getClass().isAnonymousClass()) {
return reduceByR(array, object );
}
try {
ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
MethodHandle methodHandle = callSite.dynamicInvoker();
try {
long sum = 0;
for ( int v : array ) {
sum = (long) methodHandle.invokeExact( sum, v );
}
return sum;
} catch (Throwable throwable) {
return handle(Long.class, throwable, "Unable to perform reduceBy");
}
} catch (Exception ex) {
return reduceByR(array, object);
}
}
示例9: reduceBy
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
/**
* Reduce by functional support for int arrays.
* @param array array of items to reduce by
* @param object object that contains the reduce by function
* @param <T> the type of object
* @return the final reduction
*/
public static <T> double reduceBy( final double[] array, T object ) {
if (object.getClass().isAnonymousClass()) {
return reduceByR(array, object );
}
try {
ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object);
MethodHandle methodHandle = callSite.dynamicInvoker();
try {
double sum = 0;
for ( double v : array ) {
sum = (double) methodHandle.invokeExact( sum, v );
}
return sum;
} catch (Throwable throwable) {
return Exceptions.handle(Long.class, throwable, "Unable to perform reduceBy");
}
} catch (Exception ex) {
return reduceByR(array, object);
}
}
示例10: bootstrap
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
public static CallSite bootstrap(MethodHandles.Lookup callerLookup, String name, MethodType type, long bindingId)
{
try {
ClassLoader classLoader = callerLookup.lookupClass().getClassLoader();
checkArgument(classLoader instanceof DynamicClassLoader, "Expected %s's classloader to be of type %s", callerLookup.lookupClass().getName(), DynamicClassLoader.class.getName());
DynamicClassLoader dynamicClassLoader = (DynamicClassLoader) classLoader;
MethodHandle target = dynamicClassLoader.getCallSiteBindings().get(bindingId);
checkArgument(target != null, "Binding %s for function %s%s not found", bindingId, name, type.parameterList());
return new ConstantCallSite(target);
}
catch (Throwable e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw Throwables.propagate(e);
}
}
示例11: testCallBinding
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
@Test
public void testCallBinding() throws Throwable {
CountDownLatch latch = new CountDownLatch(1);
MethodHandle invokee = LOOKUP.findVirtual(CountDownLatch.class, "countDown", MethodType.methodType(Void.TYPE))
.bindTo(latch);
MethodHandle boundInvokee = MethodHandles.dropArguments(invokee, 0, Object.class);
DynamicInvocationHandler handler = (lookup, name, type, superMethod) -> new ConstantCallSite(boundInvokee.asType(type));
DynamicProxy proxy = DynamicProxy.builder()
.withInterfaces(OneMethodInterface.class)
.withInvocationHandler(handler)
.build();
((OneMethodInterface)proxy.supplier().get()).foo();
assertEquals(0, latch.getCount());
}
示例12: whenDefaultImplementationAvailable_itCanBeOverridden
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
@Test
public void whenDefaultImplementationAvailable_itCanBeOverridden() throws Throwable {
DynamicInvocationHandler handler = (lookup, name, type, superMethod) -> {
MethodHandle h;
if (name.equals("foo")) {
h = MethodHandles.dropArguments(
MethodHandles.constant(String.class, "bar"),
0,
Object.class
).asType(type);
} else {
h = superMethod.asType(type);
}
return new ConstantCallSite(h);
};
I1 obj = (I1) DynamicProxy.builder()
.withInvocationHandler(handler)
.withInterfaces(I1.class)
.build()
.constructor()
.invoke();
assertEquals("bar", obj.foo());
}
示例13: whenInterfacesHaveMultipleInheritance_interfaceMethodsAreProxied
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
@Test
public void whenInterfacesHaveMultipleInheritance_interfaceMethodsAreProxied() throws Throwable {
DynamicInvocationHandler handler = (lookup, name, type, supermethod) -> new ConstantCallSite(
MethodHandles.dropArguments(
MethodHandles.constant(type.returnType(), null),
0,
type.parameterList()
)
);
IFaceC obj = DynamicProxy.builder()
.withInvocationHandler(handler)
.withInterfaces(IFaceC.class)
.build()
.construct();
obj.a();
obj.b();
}
示例14: bsm
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
public static CallSite bsm(Lookup lookup, String name, MethodType methodType) {
int index = name.indexOf(':');
String protocol = name.substring(0, index);
String op = name.substring(index + 1);
MethodHandle target;
switch(protocol) {
case "call": // function call
case "binary": // binary op
case "instanceof": // instanceof op
case "asthrowable": // wrap into an error if needed
JSFunction function = (JSFunction)GLOBAL_MAP.get(lookup.lookupClass()).get(op);
if (function == null) { // add an error message if the object is not a function
throw new Error("fail to do " + protocol + " on " + op);
}
target = function.getFunctionTarget(genericMethodType(methodType.parameterCount())).asType(methodType);
break;
case "truth": // give me the truth
target = identity(Object.class).asType(methodType(boolean.class, Object.class));
break;
default:
throw new Error("unknown protocol " + protocol + ":" + op);
}
return new ConstantCallSite(target.asType(methodType));
}
示例15: main
import java.lang.invoke.ConstantCallSite; //导入依赖的package包/类
public static void main(String[] args) {
ProxyFactory<IntBinaryOperator> factory = Proxy2.createAnonymousProxyFactory(IntBinaryOperator.class, new Class<?>[] { IntBinaryOperator.class },
new ProxyHandler.Default() {
@Override
public CallSite bootstrap(ProxyContext context) throws Throwable {
MethodHandle target =
methodBuilder(context.type())
.dropFirst()
.before(b -> b
.dropFirst()
.unreflect(publicLookup(), Intercept.class.getMethod("intercept", int.class, int.class)))
.unreflect(publicLookup(), context.method());
return new ConstantCallSite(target);
}
});
//IntBinaryOperator op = (a, b) -> a + b;
IntBinaryOperator op = (a, b) -> {
throw null;
};
IntBinaryOperator op2 = factory.create(op);
System.out.println(op2.applyAsInt(1, 2));
}