Guava r07, GWT and javax.annotation.Nullable

2020-02-08 12:00发布

问题:

I'm trying to use Guava in a GWT project without success (a HashMultimap, to be precise). I get a never-ending list of stacktraces for classes:

  • com.google.common.collect.ComparisonChain
  • com.google.common.collect.ForwardingSortedSetMultimap
  • com.google.common.collect.Ordering
  • ...

Each stack trace is along the lines of:

  • line xx: the import javax.annotation cannot be resolved
  • line xx: Nullable cannot be resolved to a type
  • line xx: Nullable cannot be resolved to a type
  • line xx: Nullable cannot be resolved to a type
  • ...

Looking at the code, each file that throws an error includes:

import javax.annotation.Nullable;

and, looking at the guava-src-r07.jar, each mentioned classes uses a @Nullable annotation.

I'm using JDK6 and looking at the JDK6 javadoc and...well, I can't find any such annotation. Can I get these libraries to work with a GWT project and JDK6?

P.S. - What version of Java are you using over there?

回答1:

Hum... I think it's the jsr305 you're looking for. Take a look at

http://www.findjar.com/jar/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar.html

It must be better here: http://code.google.com/p/guava-libraries/source/browse/#svn/trunk/lib where I see the @Nullable annotation



回答2:

As written above, the problem appears to be solved when using guava 10.0.1, which has an additional dependency on the JSR305 library.

Alternatively, the ID of the missing library to add to Maven is com.google.code.findbugs:jsr305:1.3.9, so the build configuration should be fixed by adding the following dependency to pom.xml in the appropriate place (warning - I didn't test this):

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

Update: User @ips suggested adding <scope>provided</scope> since the jsr305 jar isn't needed at runtime, and updating to version 2.0.1. I know that the first change makes sense, and I guess that the version update also does. In my experience, using <scope>provided</scope> created problems for Scala, but that's a separate issue.



回答3:

You need to obtain the JSR 305 JAR, but in addition, you need to include the @Nullable annotation source code as food for the GWT compiler.

Assuming your project is under com/example/myproject/ Create a file: com/example/myproject/annotation/javax/annotation/Nullable.java With the following content:

package com.example.myproject.annotation.javax.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierNickname;
import javax.annotation.meta.When;

@Documented
@TypeQualifierNickname
@Nonnull(when = When.UNKNOWN)
@Retention(RetentionPolicy.RUNTIME)
public @interface Nullable {

}

Add the line to MyProject.gwt.xml:

<super-source path="annotation"/>

And you're good to go.



标签: gwt guava