将 Guava 10 的谓词和函数接口与 GWT 2.4.0 结合使用
GWT 2.4.0 和 Guava GWT 10.0.1 是否支持谓词
和函数
?两个接口都标记为@GwtCompatible
。
在调试托管模式下运行项目时,我在使用 Predicate
时收到运行时验证错误:
[错误] [MyProject] - XXY 行:导入 javax.annotation.Nullable 无法解决
[错误] [MyProject] - YYY 行:无法将 Nullable 解析为类型
从其他 StackOverflow 帖子中,我相信这些错误不应该要求在 Guava 版本 09 的路径中包含 JSR 305 (包括路径中的 JSR 305)无论如何,没有解决问题)。
我似乎还收到了一些接口不匹配错误:
[错误] [MyProject] - 第 XXX 行:新类型 Function(){} 必须实现继承的抽象 方法 Function.apply(Object)
[错误] [MyProject] - YYY 行:new 类型的方法 apply(MyType) Function(){} 必须重写或实现超类型 方法
,以及使用 Predicate
时出现的类似错误,我将其作为错误提交:http://code.google.com/p/guava-libraries/issues/detail?id=765
对于我的设置可能有什么问题,您有什么想法吗?
我的 Project.gwt.xml 文件包含以下行:
<inherits name="com.google.common.collect.Collect" />
<inherits name="com.google.common.base.Base" />
我的 java 文件包含以下导入:
import com.google.common.base.Function;
import com.google.common.base.Predicate;
我正在使用 Eclipse 3.7.1 和 JavaSE-1.6
Are Predicate
s and Function
s supported in GWT 2.4.0 and Guava GWT 10.0.1? Both interfaces are marked as @GwtCompatible
.
When running the project in debug hosted mode, I receive run-time validation errors at the uses of Predicate
:
[ERROR] [MyProject] - Line XXY: The import javax.annotation.Nullable
cannot be resolved[ERROR] [MyProject] - Line YYY: Nullable cannot be resolved to a type
From other StackOverflow posts, I believe these errors should not require including JSR 305 in the path as of Guava version 09 (including JSR 305 in the path didn't fix the problem, anyway).
I also appear to receive a couple interface mismatch errors:
[ERROR] [MyProject] - Line XXX: The type new
Function(){} must implement the inherited abstract
method Function.apply(Object)[ERROR] [MyProject] - Line YYY: The method apply(MyType) of type new
Function(){} must override or implement a supertype
method
, and similar errors at uses of Predicate
, which I submitted as a bug: http://code.google.com/p/guava-libraries/issues/detail?id=765
Any ideas as to what might be wrong with my setup?
My Project.gwt.xml file 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了同样的问题并找到了解决方案。问题在于 JSR 305 源代码不是 GWT 模块的一部分,因此被 GWT 忽略。要修复此问题,请执行以下操作:
javax/annotation
中至少应包含 java 源的 jar 内,添加一个包含以下内容的文件Annotation.gwt.xml
:将修改后的 jsr305-2.0.0.jar 添加到 Eclipse 中的 GWT 项目的类路径中。尽管在其他地方有所说明,但不需要将此 jar 添加到
WEB-INF/lib
通过添加以下行让项目的模块继承自新创建的 GWT 模块添加到模块的
.gwt.xml
文件:就是这样!现在您的 Eclipse 项目将成功编译,并且开发/托管模式也将工作。
I just had the same problem and found a solution to this. The problem is that the JSR 305 sourcecode is not part of a GWT module and therefore ignored by GWT. To fix it do the following:
javax/annotation
add a fileAnnotation.gwt.xml
with the following content:Add the modified jsr305-2.0.0.jar to your GWT project's classpath in eclipse. Though stated elsewhere it is not requiered to add this jar to
WEB-INF/lib
Let your project's modules inherit from the newly created GWT module by adding the following line to you modules's
.gwt.xml
files:That's it! Now your eclipse project will compile successfully and development/hosted mode will work too.
这是一种已知错误,您需要了解jsr305.jar 位于 WEB-INF/lib 中
就是这样。
另一件需要注意的事情是,番石榴的部分内容捆绑在 GWT*.jar 中,但它们被放在单独的包 com.google.gwt.thirdparty.guava.common 中......
正如 eclipse 经常发生的那样,在解析导入时,它会获取错误的导入,这会在运行时导致混乱,因为打包在 com.google.gwt.thirdparty.guava 中的类来自另一个版本,并且不得引用。
我的建议:
在所有 *.java 文件中搜索“thirdparty”子字符串,并将其替换为正确的导入 - 这对我有用。 更深入
一点的解释是,在 Guava 的早期版本中,jsr305 包含在 guava jar 中,但这个版本中他们将其取出,并且对它的依赖确实出现在 lib 的 Maven pom 中,但在网站上没有详细记录。这给用户造成了很多困惑。
This is a kind of known bug you need to have jsr305.jar in your WEB-INF/lib
that is it.
Another thing to pay attention to is the fact that parts of guava is bundled in side GWT*.jar but there they are put in the separate package com.google.gwt.thirdparty.guava.common.....
And as it often happens with eclipse is that when resolving imports it grabs the wrong import and this causes mess at runtime as classes that packaged in com.google.gwt.thirdparty.guava are from another version and MUST NOT be referenced.
My advice:
Do a text search on all your *.java files for "thirdparty" substring and replace it with correct imports - it worked for me.
A little bit deeper explanation is that in previous versions of Guava jsr305 was included in the guava jar but this release they took it out and dependency on it does appear in the Maven pom of the lib but was not well documented on the web site. This caused a lot of confusion among users.