Why does the Guava ArrayListMultimap method clear the IllegalAccessError method when called by reference?

Sample code to demonstrate failure:

package ia.quant.nextgen.entry; import com.google.common.collect.ArrayListMultimap; import java.util.function.Consumer; /** * Created by arpeke on 2015-12-18. */ public final class SampleMain { public static void main(String[] argArr) { final ArrayListMultimap<Void, Void> arrayListMultimap = ArrayListMultimap.create(); arrayListMultimap.clear(); final Consumer<ArrayListMultimap> arrayListMultimapConsumer = ArrayListMultimap::clear; arrayListMultimapConsumer.accept(arrayListMultimap); } } 

Here is the Error (not Exception ) that I see:

 Exception in thread "main" java.lang.IllegalAccessError: tried to access class com.google.common.collect.AbstractMapBasedMultimap from class ia.quant.nextgen.entry.SampleMain at ia.quant.nextgen.entry.SampleMain.lambda$main$1(SampleMain.java:17) at ia.quant.nextgen.entry.SampleMain.main(SampleMain.java:18) 

I am using 32-bit Java 8 (v1.8.0_60) with Google Guava v19.0. My Google-Fu says that Google Guice is the main reason, but has no reliable explanation. (This is part of a much larger project that also includes Google Guice v3.0 through Apache Maven.)

Link example: https://github.com/hierynomus/scannit/issues/7

Somebody knows...?

  • The main reason
  • Workaround

(Finally, I also tried Google Guava v18.0, but no luck.)

Update

This code works, but I don’t understand why. (Do I not understand references to non-static methods?)

 final Consumer<ArrayListMultimap> arrayListMultimapConsumer = x -> x.clear(); arrayListMultimapConsumer.accept(arrayListMultimap); 
+6
source share
1 answer

Lambdas needs to find the latest method definition. Therefore, when you write ArrayListMultimap::clear , the interpreter is really trying to find which hierarchy class the last clear method was defined. It happens that the last method is defined in AbstractMapBasedMultimap , which is a private class of packages.

The problem in the JRE is more than likely, but there are two workarounds.

The first is the one you described. Secondly, the Guava command either makes AbstractMapBasedMultimap publicly available or overrides the clear() method in ArrayListMultimap (and this method should only call super.clear() ).

The easiest of the two workarounds at this point is the one you tried. If I were you, I would still ask the Guava team to make their workaround so that it does not affect more users.

As for your workaround, this works because you are using this method and not referencing it.

+4
source

All Articles