编程语言
首页 > 编程语言> > java-如何使用Guice使Multibinder与FactoryModuleBuilder和泛型一起使用?

java-如何使用Guice使Multibinder与FactoryModuleBuilder和泛型一起使用?

作者:互联网

我读过Google guice – multibinding + generics + assistedinject,但它似乎无法解决我的特定情况.这是我所拥有的:

与FactoryModuleBuilder一起使用的接口

public interface IFilterFactory<T extends IFilter> {
    T create(FilterConfig config);
}
public interface IFilterOneFactory extends IFilterFactory<FilterOne> {}
public interface IFilterTwoFactory extends IFilterFactory<FilterTwo> {}

我的Guice模块:

@Override
protected void configure() {
    install(new FactoryModuleBuilder().build(IFilterOneFactory.class));
    install(new FactoryModuleBuilder().build(IFilterTwoFactory.class));

    Multibinder<IFilterFactory<?>> filterBinder = Multibinder.newSetBinder(binder(), new TypeLiteral<IFilterFactory<?>>() {});
    filterBinder.addBinding().to(IFilterOneFactory.class);
    filterBinder.addBinding().to(IFilterTwoFactory.class);
}

我的主要过滤器应该组合所有其他过滤器:

public class MainFilterFactory {
    @Inject
    MainFilterFactory(Set<IFilterFactory<?>> factories) {
        this.factories = factories;
    }

    public MainFilter get(String groupKey) {
        FilterConfig fc = new FilterConfig(groupKey);
        IFilter[] filters = this.factories.stream().map(f -> f.create(fc)).toArray(IFilter[]::new);
        return new MainFilter(Arrays.asList(filters));
    }
}

那实际上是行不通的.我得到以下堆栈跟踪:

[WARNING]
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
        at java.lang.Thread.run(Thread.java:745)
[WARNING]
com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException: 19640
        at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553)
        at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419)
        at com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)
        at com.google.inject.internal.util.$StackTraceElements.forType(StackTraceElements.java:70)
        at com.google.inject.internal.Errors.formatSource(Errors.java:687)
        at com.google.inject.internal.Errors.format(Errors.java:555)
        at com.google.inject.CreationException.getMessage(CreationException.java:48)
        at java.lang.Throwable.getLocalizedMessage(Throwable.java:391)
        at java.lang.Throwable.toString(Throwable.java:480)
        at java.lang.String.valueOf(String.java:2982)
        at java.lang.StringBuilder.append(StringBuilder.java:131)
        at java.lang.Throwable.printEnclosedStackTrace(Throwable.java:695)
        at java.lang.Throwable.printStackTrace(Throwable.java:667)
        at java.lang.Throwable.printStackTrace(Throwable.java:643)
        at org.apache.maven.cli.PrintStreamLogger.warn(PrintStreamLogger.java:131)
        at org.apache.maven.monitor.logging.DefaultLog.warn(DefaultLog.java:93)
        at org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup.uncaughtException(ExecJavaMojo.java:386)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:309)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 19640
        at com.google.inject.internal.asm.$ClassReader.<init>(Unknown Source)
        at com.google.inject.internal.asm.$ClassReader.<init>(Unknown Source)
        at com.google.inject.internal.asm.$ClassReader.<init>(Unknown Source)
        at com.google.inject.internal.util.$LineNumbers.<init>(LineNumbers.java:62)
        at com.google.inject.internal.util.$StackTraceElements$1.apply(StackTraceElements.java:36)
        at com.google.inject.internal.util.$StackTraceElements$1.apply(StackTraceElements.java:33)
        at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549)
        ... 18 more

我在这里做错什么了吗?

解决方法:

这实际上是Guice 3.0与Java 8之间的Guice兼容性问题.
查看:https://github.com/google/guice/issues/757

我在构造函数中缺少一些@Assisted和@Inject,并且Guice无法报告错误,因为在这些类中使用了一些lambda.

评论lambda可以使我看到错误并进行修复.然后我取消了注释,一切似乎都按预期进行.

标签:generics,guice,java,assisted-inject
来源: https://codeday.me/bug/20191119/2039612.html