SM:
どのように私はLambdaMetaFactoryを使用してSAM /機能インタフェースのプロキシオブジェクトを作成することができます
すなわち。の同等public static Object java.lang.reflect.Proxy.newProxyInstance(ClassLoader, Class<?>[], InvocationHandler)
例えば。私は複数の工場インタフェースを持っています
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);
}
そして、私はいくつかの構築済みBeanコンテナなどに結び付けられ、実行時に工場出荷時のプロキシを生成したいです
public Object getFactoryBean(String name){
return beanContainer.get(name);
}
のようなもの
org.springframework.beans.factory.config.ServiceLocatorFactoryBean
しかしLambdaMetaFactory上に構築されました。私は、次のコードを試みたが、例外を取得します
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)
前もって感謝します。
ホルガー:
あなたのコードでは、指定String
の引数invokedType
の代わりに、引数samMethodType
とinstantiatedMethodType
引数を。あなたがしたい場合、これは正しいことだろうキャプチャString
値をし、引数なしで機能的な署名を実装します。
しかし、あなたは一貫してインターフェースを示しString
、引数と呼び出しinvoke()
引数なし。したがって、コードは次のようになります。
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;
}
どのように簡略化することができます
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
}
}