本文整理匯總了Java中java.lang.invoke.CallSite.getTarget方法的典型用法代碼示例。如果您正苦於以下問題:Java CallSite.getTarget方法的具體用法?Java CallSite.getTarget怎麽用?Java CallSite.getTarget使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.invoke.CallSite
的用法示例。
在下文中一共展示了CallSite.getTarget方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: accessInstanceMethodWithFunction
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void accessInstanceMethodWithFunction() throws Throwable {
SimpleBean simpleBeanInstance = new SimpleBean();
MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType getter = MethodType.methodType(Object.class);
MethodHandle target = caller.findVirtual(SimpleBean.class, "getObj", getter);
MethodType func = target.type();
System.out.println(func);
System.out.println(func.generic());
CallSite site = LambdaMetafactory.metafactory(caller, "apply", MethodType.methodType(Function.class), func.generic(), target, func);
MethodHandle factory = site.getTarget();
Function r = (Function) factory.invoke();
assertEquals("myCustomObject", r.apply(simpleBeanInstance));
}
示例2: accessObjectInstanceMethod
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void accessObjectInstanceMethod() throws Throwable {
SimpleBean simpleBeanInstance = new SimpleBean();
MethodHandles.Lookup caller = MethodHandles.lookup();
Method reflected = SimpleBean.class.getDeclaredMethod("getObj");
MethodHandle methodHandle = caller.unreflect(reflected);
CallSite site = LambdaMetafactory.metafactory(caller, "get", MethodType.methodType(Supplier.class, SimpleBean.class),
MethodType.methodType(Object.class), methodHandle, MethodType.methodType(Object.class));
MethodHandle factory = site.getTarget();
factory = factory.bindTo(simpleBeanInstance);
Supplier r = (Supplier) factory.invoke();
assertEquals(simpleBeanInstance.getObj(), r.get());
}
示例3: getCallSiteTarget
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
public JavaConstant getCallSiteTarget(Assumptions assumptions) {
if (object instanceof CallSite) {
CallSite callSite = (CallSite) object;
MethodHandle target = callSite.getTarget();
if (!(callSite instanceof ConstantCallSite)) {
if (assumptions == null) {
return null;
}
assumptions.record(new Assumptions.CallSiteTargetValue(callSite, target));
}
return HotSpotObjectConstantImpl.forObject(target);
}
return null;
}
示例4: makeDeserializationFunc
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
private ParseFromBytes makeDeserializationFunc(Class<?> type, ParseFromBytes func) {
try {
MethodType originFuncArgs = MethodType.methodType(type, byte[].class);
MethodType targetFuncArgs = MethodType.methodType(Object.class, byte[].class);
MethodType targetClass = MethodType.methodType(ParseFromBytes.class);
MethodHandle originFuncHandle = caller.findStatic(type, "parseFrom", originFuncArgs);
CallSite callSite = LambdaMetafactory.metafactory(caller, "parseFormData", targetClass, targetFuncArgs,
originFuncHandle, originFuncArgs);
MethodHandle factory = callSite.getTarget();
func = (ParseFromBytes) factory.invoke();
} catch (Throwable e) {
log.error("makeDeserializationFunc error ..", e);
}
return func;
}
示例5: buildFunction
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
private void buildFunction(final MethodHandles.Lookup caller, final MethodHandle methodHandle) {
try {
final MethodType func = methodHandle.type();
final CallSite site = LambdaMetafactory.metafactory(caller, "apply", MethodType.methodType(Function.class),
MethodType.methodType(Object.class, Object.class), methodHandle, MethodType.methodType(func.returnType(), func.parameterArray()));
final MethodHandle factory = site.getTarget();
function = (Function<BEAN, R>) factory.invoke();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
示例6: build
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
private void build(final MethodHandles.Lookup caller, final MethodHandle methodHandle) {
try {
final MethodType func = methodHandle.type();
final CallSite site = LambdaMetafactory.metafactory(caller, "apply", MethodType.methodType(BiFunction.class),
MethodType.methodType(Object.class, Object.class, Object.class), methodHandle, MethodType.methodType(func.returnType(), func.parameterArray()));
final MethodHandle factory = site.getTarget();
function = (BiFunction<BEAN, R, BEAN>) factory.invoke();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
示例7: build
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
private void build(final MethodHandles.Lookup caller, final MethodHandle methodHandle) {
try {
final MethodType func = methodHandle.type();
final CallSite site = LambdaMetafactory.metafactory(caller, "accept", MethodType.methodType(BiConsumer.class),
MethodType.methodType(Void.TYPE, Object.class, Object.class), methodHandle, MethodType.methodType(Void.TYPE, func.parameterArray()));
final MethodHandle factory = site.getTarget();
consumer = (BiConsumer<BEAN, R>) factory.invoke();
} catch (final Throwable e) {
throw new RuntimeException(e);
}
}
示例8: accessStaticMethod
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void accessStaticMethod() throws Throwable {
MethodHandles.Lookup caller = MethodHandles.lookup();
Method reflected = SimpleBean.class.getDeclaredMethod("getStaticObj");
MethodHandle methodHandle = caller.unreflect(reflected);
CallSite site = LambdaMetafactory.metafactory(caller, "get", MethodType.methodType(Supplier.class), MethodType.methodType(Object.class), methodHandle,
MethodType.methodType(Object.class));
MethodHandle factory = site.getTarget();
Supplier r = (Supplier) factory.invoke();
assertEquals(SimpleBean.getStaticObj(), r.get());
}
示例9: accessStringInstanceSetterMethodAttemptTwo
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void accessStringInstanceSetterMethodAttemptTwo() throws Throwable {
SimpleBean simpleBeanInstance = new SimpleBean();
MethodHandles.Lookup caller = MethodHandles.lookup();
MethodType setter = MethodType.methodType(Void.TYPE, Object.class);
MethodHandle target = caller.findVirtual(SimpleBean.class, "setObj", setter);
// target.invoke(simpleBeanInstance, "newStringValue");
// assertEquals( "newStringValue" , simpleBeanInstance.getObj() );
MethodType func = target.type(); // MethodType.methodType(Void.TYPE,
// SimpleBean.class, String.class),
System.out.println(func);
System.out.println(func.generic());
System.out.println(MethodType.methodType(Void.TYPE, SimpleBean.class, Object.class));
CallSite site = LambdaMetafactory.metafactory(caller, "accept", MethodType.methodType(BiConsumer.class),
MethodType.methodType(Void.TYPE, Object.class, Object.class), target, func);
MethodHandle factory = site.getTarget();
BiConsumer r = (BiConsumer) factory.invoke();
r.accept(simpleBeanInstance, "newCustomObject");
assertEquals("newCustomObject", simpleBeanInstance.getObj());
}
示例10: accessPrimitiveInstanceMethod
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void accessPrimitiveInstanceMethod() throws Throwable {
SimpleBean simpleBeanInstance = new SimpleBean();
MethodHandles.Lookup caller = MethodHandles.lookup();
Method reflected = SimpleBean.class.getDeclaredMethod("getIntPrimitive");
MethodHandle methodHandle = caller.unreflect(reflected);
CallSite site = LambdaMetafactory.metafactory(caller, "get", MethodType.methodType(Supplier.class, SimpleBean.class),
MethodType.methodType(Object.class), methodHandle, MethodType.methodType(int.class));
MethodHandle factory = site.getTarget();
factory = factory.bindTo(simpleBeanInstance);
Supplier r = (Supplier) factory.invoke();
assertEquals(simpleBeanInstance.getIntPrimitive(), r.get());
}
示例11: accessStringInstanceMethod
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
@Test
public void accessStringInstanceMethod() throws Throwable {
SimpleBean simpleBeanInstance = new SimpleBean();
MethodHandles.Lookup caller = MethodHandles.lookup();
Method reflected = SimpleBean.class.getDeclaredMethod("getValue");
MethodHandle methodHandle = caller.unreflect(reflected);
CallSite site = LambdaMetafactory.metafactory(caller, "get", MethodType.methodType(Supplier.class, SimpleBean.class),
MethodType.methodType(Object.class), methodHandle, MethodType.methodType(String.class));
MethodHandle factory = site.getTarget();
factory = factory.bindTo(simpleBeanInstance);
Supplier r = (Supplier) factory.invoke();
assertEquals(simpleBeanInstance.getValue(), r.get());
}
示例12: buildFactory
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
private void buildFactory(String name, MethodHandles.Lookup caller, MethodType fact, MethodType getter) throws LambdaConversionException, IllegalAccessException {
CallSite site = LambdaMetafactory.metafactory(caller, name, fact, getter, target, getter);
factory = site.getTarget();
}
示例13: privateCreateLambda
import java.lang.invoke.CallSite; //導入方法依賴的package包/類
/**
* This method uses {@link LambdaMetafactory} to create a lambda.
* <br>
* The lambda will implement the argument provided interface.
* <br>
* This interface is expected to contain a signature that matches the method, for which we are creating the lambda.
* In the context of the lambda-factory project, this interface will always be the same, namely an auto-generate interface
* with abstract methods for all combinations of primitives + Object (up until some max number of arguments.)
* <p>
*
* @param method The {@link Method} we are trying to create "direct invocation fast" access to.
* @param setAccessible a boolean flag indicating whether or not the returned lambda shall force access private methods.
* This corresponds to {@link Method#setAccessible(boolean)}.
* @param interfaceClass The interface, which the created lambda will implement.
* In the context of the lambda-factory project this will always be the same, namely a compile time auto-generated interface. See {@link GenerateLambdaProcessor}.
* @param the name of the method from the interface, which shall be implemented. This argument exists for the sake of jUnit testing.
* @return An instance of the argument provided interface, which implements only 1 of the interface's methods, namely the one whose signature matches the methods, we are create fast access to.
* @throws Throwable
*/
private static <T> T privateCreateLambda(Method method, MethodHandles.Lookup lookup, Class<T> interfaceClass, String signatureName, boolean createSpecial) throws Throwable {
MethodHandle methodHandle = createSpecial? lookup.unreflectSpecial(method, method.getDeclaringClass()) : lookup.unreflect(method);
MethodType instantiatedMethodType = methodHandle.type();
MethodType signature = createLambdaMethodType(method, instantiatedMethodType);
CallSite site = createCallSite(signatureName, lookup, methodHandle, instantiatedMethodType, signature,interfaceClass);
MethodHandle factory = site.getTarget();
return (T) factory.invoke();
}