Guice 3.0 - 启动时出现ArrayIndexOutOfBoundsException?

7
为什么Guice 3.0抛出这个异常而不是为配置错误的组件(例如缺少@Inject)提供格式化消息?
Exception in thread "main" com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException: 16640
    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.forMember(StackTraceElements.java:53)
    at com.google.inject.internal.Errors.formatInjectionPoint(Errors.java:716)
    at com.google.inject.internal.Errors.formatSource(Errors.java:678)
    at com.google.inject.internal.Errors.format(Errors.java:555)
    at com.google.inject.ConfigurationException.getMessage(ConfigurationException.java:70)
    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.io.PrintStream.println(PrintStream.java:821)
    at java.lang.Throwable$WrappedPrintStream.println(Throwable.java:748)
    at java.lang.Throwable.printStackTrace(Throwable.java:655)
    at java.lang.Throwable.printStackTrace(Throwable.java:643)
    at java.lang.Throwable.printStackTrace(Throwable.java:634)
    at hu.daniel.hari.exercises.cleanarchitecture.payrollcasestudy.adapters.primary.ui.impl.swing._2.SwingUIMain2.<init>(SwingUIMain2.java:40)
    at hu.daniel.hari.exercises.cleanarchitecture.payrollcasestudy.adapters.primary.ui.impl.swing._2.SwingUIMain2.main(SwingUIMain2.java:17)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 16640
    at com.google.inject.internal.asm.$ClassReader.readClass(Unknown Source)
    at com.google.inject.internal.asm.$ClassReader.accept(Unknown Source)
    at com.google.inject.internal.asm.$ClassReader.accept(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)
    ... 17 more

我的初始代码是:

Injector injector = Guice.createInjector(new SwingUIModule(useCaseFactory));
injector.getInstance(MainFrameUI.class).show();

你找到解决方案了吗?对于这个问题有什么线索吗? - angelcervera
2个回答

9
我们曾经遇到过这个问题,但事实证明,这根本不是Guice 3的问题(至少在我们的情况下)。但由于Guice 3中异常处理不佳,我们收到了与作者相同的错误信息。
简而言之,我们的问题源于一个类的静态块中抛出的“NoClassDefFoundError: Could not initialize class ...”异常。后来发现,在构建fat-jar时,我们排除了太多的类,导致一些类缺失。不幸的是,使用Guice 3时,我们只收到了“$ComputationException: java.lang.ArrayIndexOutOfBoundsException: ...”消息,无法提供更多帮助。
我的观点是,你遇到的问题可能并不是Guice 3造成的。
  1. We have a project (A) that we include as a dependency in project (B) that is running on Spark Cluster

  2. Project A, was using log4j 2, and spark-hive (used in Project B) for some reason does not like when it has extra logging frameworks in the classpath, so we excluded it in sbt-assembly:

    ExclusionRule(organization = "org.apache.logging.log4j"),
    
  3. In project A, we have a class that has, lets say some code like this (java):

    static {
        this.defaultMarker = MarkerManager.getMarker("abc")
    }
    

    And MarkerManager is from org.apache.logging.log4j, so this class is missing in fat-jar of project B.

  4. We run it on the cluster, where some class that is supposed to be @injected uses the class with static block.

  5. Boom! $ComputationException: java.lang.ArrayIndexOutOfBoundsException

  6. I decided to initialize all classes manually, without Guice only to find out that it was not Guice fault.

  7. Fix the ExclusionRule and it all works with Guice 3 again.


有没有什么技巧可以获取真实的异常信息?不幸的是,Guice 不在我的代码中,而是作为一个依赖项存在,所以我不能轻易地尝试不使用它。 - Daniel Darabos
正如我在答案中所提到的 - 我不得不摆脱Guice才能找到它。我猜测Guice 3之所以不受支持是有原因的 :) - Atais
谢谢!我和你处于同样的情况,正在构建一个fat jar并解决冲突。我会看看是否可以以某种方式使用更新的Guice。 - Daniel Darabos

5

我觉得问题出在Guice 3及以下版本对lambda表达式的处理上。你可能需要升级到Guice 4来解决这个问题,如这里所述。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接