[英]How to create proxy object at runtime using LambdaMetaFactory?
How can I create proxy objects for SAM/functional interfaces using LambdaMetaFactory如何使用 LambdaMetaFactory 为 SAM/功能接口创建代理对象
ie. IE。 equivalent of public static Object java.lang.reflect.Proxy.newProxyInstance(ClassLoader, Class<?>[], InvocationHandler)
相当于public static Object java.lang.reflect.Proxy.newProxyInstance(ClassLoader, Class<?>[], InvocationHandler)
Eg.例如。 I have multiple factory interfaces我有多个工厂接口
interface X{
// members
// methods
}
interface Y{
// members
// methods
}
public interface Factory1{
X get(String name);
}
public interface Factory2{
Y get(String name);
}
.
.
.
public interface FactoryN{
someclassOrInterface get(String name);
}
and I want to generate the factory proxies at runtime tied to some prebuilt bean container eg我想在运行时生成工厂代理绑定到一些预先构建的 bean 容器,例如
public Object getFactoryBean(String name){
return beanContainer.get(name);
}
something similar to类似于
org.springframework.beans.factory.config.ServiceLocatorFactoryBean
but built on LambdaMetaFactory.但建立在 LambdaMetaFactory 之上。 I tried following code but gets an exception我尝试了以下代码,但出现异常
static <T> T getFactory(Class<T> factoryClass) {
T factory =null;
try {
final MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> beanType = factoryClass.getMethod("get", String.class).getReturnType();
final CallSite site = LambdaMetafactory.metafactory(lookup,
"get",
MethodType.methodType(factoryClass, String.class),
MethodType.methodType(beanType),
lookup.findStatic(ReflectionUtil.class, "getFactoryBean", MethodType.methodType(Object.class, String.class)),
MethodType.methodType(beanType));
factory = (T) site.getTarget().invoke();
} catch(Throwable e) {
e.printStackTrace();
}
return factory;
}
public static Object getFactoryBean(String beanName) {
return beanMap.get(beanName);
}
java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle(String)Factory1 to ()Object
at java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:775)
at java.lang.invoke.MethodHandle.asType(MethodHandle.java:761)
at java.lang.invoke.Invokers.checkGenericType(Invokers.java:321)
Thanks in advance.提前致谢。
Your code specifies a String
argument for the invokedType
argument instead of the samMethodType
and instantiatedMethodType
arguments.您的代码为invokedType
参数指定了一个String
参数,而不是samMethodType
和instantiatedMethodType
参数。 This would be the right thing if you want to capture a String
value and implement a functional signature without arguments.如果您想捕获String
值并实现不带参数的功能签名,这将是正确的做法。
But you consistently show interfaces with a String
argument and call invoke()
without arguments.但是您始终使用String
参数显示接口并invoke()
不带参数的invoke()
。 Therefore, the code should look like因此,代码应该看起来像
static <T> T getFactory(Class<T> factoryClass) {
T factory =null;
try {
final MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> beanType = factoryClass.getMethod("get", String.class).getReturnType();
final CallSite site = LambdaMetafactory.metafactory(lookup,
"get",
MethodType.methodType(factoryClass),
MethodType.methodType(beanType, String.class),
lookup.findStatic(ReflectionUtil.class, "getFactoryBean",
MethodType.methodType(Object.class, String.class)),
MethodType.methodType(beanType, String.class));
factory = (T)site.getTarget().invoke();
} catch(Throwable e) {
e.printStackTrace();
}
return factory;
}
which can be simplified to可以简化为
static <T> T getFactory(Class<T> factoryClass) {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> beanType = factoryClass.getMethod("get", String.class).getReturnType();
MethodHandle target = lookup.findStatic(ReflectionUtil.class, "getFactoryBean",
MethodType.methodType(Object.class, String.class));
MethodType funcType = target.type().changeReturnType(beanType);
return (T)LambdaMetafactory.metafactory(
lookup, "get", MethodType.methodType(factoryClass), funcType, target, funcType)
.getTarget().invoke();
} catch(Throwable e) {
e.printStackTrace();
return null; // rethink this error handling
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.