Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice 3.0 - ArrayIndexOutOfBoundsException on startup?

Tags:

guice-3

Why Guice 3.0 throws this exception instead of a formatted message for wrong configurated components (@Inject is missing for example)?

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

My initating code is:

Injector injector = Guice.createInjector(new SwingUIModule(useCaseFactory));
injector.getInstance(MainFrameUI.class).show();
like image 308
Daniel Hári Avatar asked Apr 02 '16 17:04

Daniel Hári


2 Answers

We have faced this issue and it turned out that it is not Guice 3 issue at all (in our case). Fact is, though, that due to poor Exception handling in Guice 3 we got the same error message as the author.

TL/DR

The source of our problem was NoClassDefFoundError: Could not initialize class ... exception that was thrown in a static block in one of our classes.

It turned out, that we had excluded too many classes during fat-jar building, and simply there were some classes missing. Unfortunately, with Guice 3, we received only $ComputationException: java.lang.ArrayIndexOutOfBoundsException: ... message, to help us out.

My point is, that it might be, that Guice 3 is not the source of your problem.

Full version

  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.

like image 123
Atais Avatar answered Oct 09 '22 14:10

Atais


I think the issue is with how Guice 3 and below handles lambda expressions. You might have to upgrade to Guice 4 to fix this as mentioned here.

like image 34
mindreader Avatar answered Oct 09 '22 15:10

mindreader