Eclipse 3.5+ - Annotation processor: Generated classes cannot be imported

可紊 提交于 2019-12-06 12:13:51

I assume that you are generating sources in the last processor round. This is not recommended way and leads exactly to the problem that you had. Explanation is here: http://code.google.com/p/acris/wiki/CodeGenerationPlatform_Pitfall_Rounds

So the my advise is to generate sources in regular processing rounds and final round should be used just for notification that processing is over or something like that.

Hopefully this helps you.

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_
  }
}

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.

We were experiencing a similar problem and apparently just solved it, so thought of sharing it at SO, in case it helps someone.

We are using:

  • Eclipse Indigo (Build id: 20120216-1857)
  • m2e Connector for maven
  • openJPA for static metamodel class generation

Our problem:

Say, we have a package named com.abc.xyz and an entity class in there named OurEntity. When we build the projects (JPA, EJB, EAR etc. all together with an mvn clean at the beginning) the metamodel classes get generated. And also get appropriately packaged within the PU jar. But when we try to import the generated metamodel class com.abc.xyz.OurEntity_, Eclipse cannot resolve it. OP apparently got past this point:-). Maven build failed, saying it could not resolve that class. Not much help from google except for a few bug reports such as this one: https://bugs.eclipse.org/bugs/show_bug.cgi?id=350378
That bug report said importing the whole package as opposed to the single class helped. So, tried that, but with no benefit. It also said (and so did David Heitzman) that using the fully qualified class name worked for them. That did not work either.

The solution:

Added the PU jar to Eclipse build path for the project that needed to use the metamodel classes. All of a sudden all the red underlines went away (not a surprise). But the fear was there might be two PUs in the same ear. But maven automagically took care of that.

As this rather old question got some attention without pointing to the very probable eclipse bug the OP was specifically asking for, I'd like to complement the above answers with a pointer to the eclipse bug tracker:

Cannot resolve import for generated class IF processing annotations with parameters referencing constants

The workarounds include

  1. doing a wildcard import of the package defining the generated classes (i.e. import some.package.*;)
  2. using the fully qualified name of your generated class, i.e. referring to some.package.Foo in your code and not using an import
  3. switch to a newer Eclipse. This specific eclipse bug is resolved with Eclipse version 4.4 (aka Luna).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!