5

I have implemented an AnnotationProcessor that picks up class annotations that take a string argument. The string argument is an expression in a domain-specific language, which the annotation processor will use to compile a class file.

I create a small test project to try this out. The behaviour I see is this:

  1. I can successfully build the project using maven
  2. I can successfully run the project from within intellij
  3. Despite the project RUNNING in intellij, the generated class is not recognised in the editor ("Cannot resolve class '...'"), and intelli-sense does not work, either.

I've tried to find the issue and found:

  • the class file that is being generated is being created in target/classes/package/name/KlassName.class (this is the location that the Filer::createClassFile method picks, I'd have expected this to go to some separate directory though).

  • if I'd create a java source file during annotation processing (using Filer::createSourceFile), intellij would have no problem. However, I can't do that, since the compiler is a library that really must create classes directly.

I have two guesses about what a solution might look like:

  1. This problem might stem from intellij not looking inside target/classes when type checking in the editor window.
  2. The class files should be generated in a separate directory instead. If so, what is the setting to fix that?

I have reproduced this issue using intellij IDEA 2016.2.1 and intellij IDEA 2017.2 EAP.

  • 1
    Can't you simply tag the directory where your generated .class files are as a `Source` directory in your project structure ? – DamCx Sep 01 '17 at 12:59
  • 1
    Does the project run from maven using only maven from the command line? – Freiheit Sep 01 '17 at 12:59
  • 1
    Can you build your .class files to a JAR or otherwise declare them as a dependency in maven? If Maven picks it up, so can InteilliJ – Freiheit Sep 01 '17 at 13:00
  • @DamCx: intellij marks, by default, the target directory as "Excluded". When I mark target/classes as Sources, the behaviour does not seem to change. – Stephan Brandauer Sep 01 '17 at 13:21
  • 1
    @Freiheit: I actually built them to a JAR before, and I could do that again. However, the annotation processing framework API seems to not have support for creating JAR files, only sources and class files (https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Filer.html) – Stephan Brandauer Sep 01 '17 at 13:22
  • 1
    @Freiheit: Also, the project *does* run using maven from the command line, using mvn clean compile exec:java -Dexec.mainClass=... – Stephan Brandauer Sep 01 '17 at 13:23
  • @StephanBrandauer please describe the use case when the generated classes should be recognized in the editor. How exactly do you use the generated classes in your project sources? What's the workflow? – CrazyCoder Sep 05 '17 at 13:27

0 Answers0