Using the Predicate and Function Interfaces from Guava 10 with GWT 2.4.0

Are Predicate and Function supported in GWT 2.4.0 and Guava GWT 10.0.1? Both interfaces are marked as @GwtCompatible .

When starting a project in debug mode in hosting, I get runtime errors while using Predicate :

[ERROR] [MyProject] - Line XXY: javax.annotation.Nullable import cannot be allowed

[ERROR] [MyProject] - String YYY: Nullable cannot be allowed for type

From other StackOverflow posts, I believe that these errors should not require the inclusion of JSR 305 in the path of version Guava version 09 (including JSR 305 in the path did not fix the problem, anyway).

I also see that you are getting interface mismatch errors:

[ERROR] [MyProject] - Line XXX: type new Function () {} must implement the inherited abstract method Function.apply (Object)

[ERROR] [MyProject] - String YYY: method (MyType) of type new is applied Function () {} must override or implement a supertype Method

and similar errors when using Predicate , which I presented as an error: http://code.google.com/p/guava-libraries/issues/detail?id=765

Any ideas on what might be wrong in my setup?

The file My Project.gwt.xml contains the following lines:

 <inherits name="com.google.common.collect.Collect" /> <inherits name="com.google.common.base.Base" /> 

My java file includes the following imports:

 import com.google.common.base.Function; import com.google.common.base.Predicate; 

I am using Eclipse 3.7.1 and JavaSE-1.6

+7
source share
2 answers

I had the same problem and found a solution. The problem is that the JSR 305 source code is not part of the GWT module and therefore is ignored by GWT. To fix this, follow these steps:

  • Add a GWT module handle to jsr305-2.0.0.jar. Inside the jar, which should contain at least java sources in the javax/annotation , add the Annotation.gwt.xml file with the following contents:
 <?xml version="1.0" encoding="UTF-8"?> <module> <source path="" /> </module> 
  • Add the modified jsr305-2.0.0.jar to your path to the GWT object class in eclipse. Although indicated elsewhere, it is required not to add this jar to WEB-INF/lib

  • Let your project modules inherit from the newly created GWT module by adding .gwt.xml modules of the following lines to you:

 <inherits name='javax.annotation.Annotation'/> 

What is it! Now your eclipse project will be successfully compiled, and the development / deployment mode will also work.

+2
source

This is a kind of known bug , you need to have jsr305.jar in your WEB-INF / lib i.e.

Another thing that you need to pay attention to is the fact that the guava parts are connected in the GWT * .jar side, but there they are placed in a separate package com.google.gwt.thirdparty.guava.common ..... And, as is often the case with eclipse, this is that when import is resolved, it captures the wrong import, and it causes a mess at runtime, since classes that are packaged in com.google.gwt.thirdparty.guava are from a different version and MUST NOT be referenced .

My advice:

Do a text search on all of your * .java files for the "thirdparty" substring and replace them with the correct imports - this worked for me.

A slightly deeper explanation is that in previous versions of Guava, jsr305 was included in the guava jar, but in this release they pulled it out and a dependency on it appeared in the Maven pom lib, but the website was not well documented on the Internet. This caused a lot of confusion among users.

0
source

All Articles