7

I'm using a java annotation processor to generates additional classes at compile time. It works fine when building with gradle, but I can't get IntelliJ to recognize the generated classes. Whenever I try to build the project in IntelliJ, it errors saying that it can't find the symbols that refer to the generated class. In the same vein, since it doesn't know about the classes, it's not giving me any help in writing code that uses the classes, and just highlights it all as an error.

I have two sibling modules: the "processor" module implements the annotation processor and defines the annotations. The "demo" module is just some JUnit tests to try out the annotation processor. I can build the "processor" module in IntelliJ fine, but the "demo" one gives me the errors as described above. I've got "processor" as a dependency of the "demo" module, at the "Test" scope (I've also tried "Compile" scope).

How can I get IntelliJ to recognize the classes automatically?

Update

I've been able to get it to build by creating a new profile under "Annotation Processors" in the settings dialog, moving the "demo" module under that profile, enabling annotation processing for that profile, and specifying the FQCN of the annotation processor under the "Annotation Processors" list box.

However, the live code help still isn't working, the editor just tells me it can't find the class, which is really more important (because I could always build from gradle).

brianmearns
  • 9,581
  • 10
  • 52
  • 79

2 Answers2

21

In IntelliJ, find the folder that the generated code goes in. This is controlled by settings on the same dialog as where you set up the annotation processing. Right click on that folder, find "Mark Directory As" in the context menu, and choose "Generated Sources Root".

After doing this, IntelliJ will recognize and handle the generated classes normally. It will also give a compile warning that an output path intersects with a source root, but that's reasonably ignorable. I haven't been able to find a way to get rid of that warning without also leaving the generated classes unrecognized.

Douglas
  • 5,017
  • 1
  • 14
  • 28
  • 2
    I'm encountering the same issue, but my generated folders for annotation processing do not have the Right Click "Mark Directory As" option because they are Excluded by default in Project Structure -> Modules. It is not possible for me to unmark them as Excluded or to mark them as anything else... – lingz Apr 05 '17 at 06:31
  • 3
    @lingz I would guess your generated code folder is actually a subfolder of what the exclusion marking is on. You can either change where your generated code goes through the annotation processing settings, or unmark the parent folder (you will then want to exclude each of its subfolders other than the one for generated code). – Douglas Apr 05 '17 at 21:14
  • Yup that was it. Thank you! – lingz Apr 06 '17 at 01:08
  • I don't have the option "Generated Sources Root" or am I searching in the wrong place? (version is 2020.2) – Mayra Delgado Sep 28 '20 at 15:03
8

You can go on pom/gradle file and Reload it again, it will work. example :

pom.xml(right-click)-> Maven-> Reload

ashish gupta
  • 91
  • 1
  • 1