简体   繁体   中英

Strange Guice injection error

I have a very strange error when trying to inject with constructor with Guice. There is a particular line in the constructor as the following:

@Inject
public RoundRobinAssigner(
        ... arguments
        ) {
            ...stuff

    assignments = Sets.synchronizedNavigableSet(Sets.<CountingEntry<String>>newTreeSet());
}

This fails upon injection with the following.

1) Error injecting constructor, java.lang.NoSuchMethodError: com.google.common.collect.Sets.synchronizedNavigableSet(Ljava/util/NavigableSet;)Ljava/util/NavigableSet;
  at edu.harvard.econcs.turkserver.util.RoundRobinAssigner.<init>(RoundRobinAssigner.java:46)
  at edu.harvard.econcs.turkserver.util.RoundRobinAssigner.class(RoundRobinAssigner.java:40)
  while locating edu.harvard.econcs.turkserver.util.RoundRobinAssigner

But if I remove the Sets.synchronizedNavigableSet() wrapping, things inject just fine.

@Inject
public RoundRobinAssigner(
        ... arguments
        ) {     
            ...stuff

    assignments = Sets.<CountingEntry<String>>newTreeSet();

}

Clearly, this is suboptimal as I want to use the synchronized set. Is there any reason why a Guice-called instructor would behave any differently than a normal one? Neither of these code has any compile problems and the Sets class from guava has too have been loaded, so I have no idea what is causing this.

I suspect you're just seeing a problem which you'd otherwise see elsewhere - basically because Guice is involved when loading up the class via reflection, the "link time" error of Sets.synchronizedNavigableSet being unavailable is shown within the context of Guice instead of in a "normal" constructor call.

synchronizedNavigableSet was only introduced in 13.0 - is it possible that you're compiling against that, but running against an older version of Guava?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM