Eclipse 3.5+ - 注释处理器:无法导入生成的类 [英] Eclipse 3.5+ - Annotation processor: Generated classes cannot be imported

查看:336
本文介绍了Eclipse 3.5+ - 注释处理器:无法导入生成的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第三方注解处理器从我项目中的注释类生成元数据代码(.java文件)。
我已经通过Eclipse(属性 - > Java编译器 - >注释处理)成功配置了处理器,代码生成工作正常(代码自动创建和生成)。此外,Eclipse成功自动完成生成的类及其字段,没有任何错误。假设我有一个类some.package.Foo,生成的元数据类是some.package.Foo_。在自动完成的帮助下,我可以在Eclipse编辑器中获取以下代码,没有任何错误:

I am using a 3rd party annotation processor for generating meta-data code (.java files) from the annotated classes in my project. I have successfully configured the processor through Eclipse (Properties -> Java Compiler -> Annotation Processing) and the code generation works fine (code is automatically created and generated). Also, Eclipse successfully auto-completes the generated classes and their fields, without any errors. Let's say that I have a class "some.package.Foo" and that the generated meta-data class is "some.package.Foo_". By the help of auto-completion, I can get the following code in the Eclipse editor, without any errors:

import some.package.Foo_;
...
public class Test {
  void test() {
    Foo_.someField = null; // try to access a field from the generated class Foo_
  }
}

但是,一旦我实际构建项目(或只是保存文件,因为Build自动启用),我得到的错误,告诉some.package.Foo_无法解决。
似乎Eclipse正在生成并编译some.package.Foo_,或者更有可能。

However, as soon as I actually build the project (or just save the file since Build automatically is enabled), I get the error which tells that "some.package.Foo_" cannot be resolved. It seems like Eclipse is generating and compiling the some.package.Foo_ at the same time, or more likely.

我发现了两个临时解决方案实际上阻​​碍了注解处理器的使用):

I found two temporary solutions (which are practically hindering the use of the annotation processor in the first place):


  1. 在生成类的每个构建之前,右键单击每个生成的文件转到属性,并取消选中派生滴答。之后,我做了项目的清理,进口是罚款 - 没有更多的错误。但是,如果我再次进行清理,则再次显示错误,因为文件的生成会导致再次检查(自动)派生刻度。所以这真的很麻烦和耗时。

  2. 我还从所有这些文件中取消选中派生
    ,这次
    我取消选中派生从
    的源文件夹和
    包含这些文件的软件包。然后我禁用
    注释处理器,然后
    执行清理。没有更多的
    导入错误,即使我做另一个
    清理,但没有利用
    使用注释处理器
    ,因为如果我要更改
    将更新模型,我需要
    将注释处理器
    重新打开,并重复这个乏味的
    程序将其关闭,之后
    生成新的
    这些文件的版本。

这是Eclipse中的一个错误?如果是,有没有比上面提到的两个更好的解决方法或快速修复?如果没有,我该怎么尝试解决问题?

Is this a bug in Eclipse? If yes, is there a better workaround or quick-fix than the two I have stated above? If not, what should I try to solve the problem?

我还尝试重新排列构建路径上的库的顺序,并没有帮助。 >

I also tried rearranging the order of the libraries on the build path and it doesn't help.

推荐答案

我有一个类似的问题,我唯一发现的是,这是导入不工作,但是类中的引用也可以工作。我使用的解决方法是在需要生成的类的所有情况下使用FQCN(除了生成的类在同一个包中,因为显然不需要导入)。

I have a similar problem, and the only thing I've found is that it's the imports specifically that don't work, but the references in the class itself do work. The workaround I've used is to use the FQCN in all cases where the generated class is needed (except when the generated class is in the same package, since then the import is obviously not needed).

所以要使用你的例子,我会做:

So to use your example, I'd do:

public class Test {
  void test() {
    some.package.Foo_.someField = null; // try to access a field from the generated class Foo_
  }
}

我唯一的猜测是,eclipse编译器在进行注解处理之前正在处理导入,这在eclipse中必须是一个错误。

My only guess then is that the eclipse compiler is processing the imports before doing the annotation processing, which imho must be a bug in eclipse.

我知道这个问题已经结束了一年以前,所以我有兴趣知道你是否找到其他方法来解决它。

I know this question is over a year old, so I'd be interested to know if you've found any other way to fix it.

这篇关于Eclipse 3.5+ - 注释处理器:无法导入生成的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆