Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need jsr305 to use guava in scala?

Tags:

scala

guava

I have the following scala file:

object SGuavaTryout {
  com.google.common.cache.CacheBuilder.newBuilder()
}

I compile with guava-11.0.2.jar in the classpath. My scala 2.9.1 compiler (both eclipse plugin and plain scalac) complains:

error while loading CacheBuilder, Missing dependency 
  'class javax.annotation.CheckReturnValue', required by 
  D:\devel\eclipse-workspace\Scala Spielwiese\guava-11.0.2.jar
  (com/google/common/cache/CacheBuilder.class)

To compile, I need to add jsr305 (jsr305-1.3.9.jar) to the build path. The java equivalent compiles just fine without jsr305:

public class JGuavaTryout {
  public void tryout() {
    com.google.common.cache.CacheBuilder.newBuilder();
  }
}

Any ideas why scala requires jsr305? Is there an official jsr305 implementation to use with guava?

Thanks! - Georg

like image 231
Georg Avatar asked Apr 04 '12 09:04

Georg


3 Answers

That's because of the way the Scala compiler is designed, it requires all the types exposed by a class to be available at compile time, whereas the Java compiler effectively doesn't care.

like image 60
Sean Parsons Avatar answered Oct 17 '22 05:10

Sean Parsons


Sean Parsons answered your first question, by explaining why Scala requires the JSR 305 dependency.

As to the "official" JSR 305 implementation to use with Guava, I'd use the one they declare in their pom.xml:

<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>1.3.9</version>
</dependency>

If you were using Maven, I think it would add the dependency to the classpath automatically.

Note: you can download the jar directly from the Maven Central repository.

like image 20
Etienne Neveu Avatar answered Oct 17 '22 04:10

Etienne Neveu


You can add this dependency:

<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>2.0.3</version>
    <scope>provided</scope>
</dependency>

thus the compilation will work and this won't come to the final release.

like image 24
igreenfield Avatar answered Oct 17 '22 03:10

igreenfield