Get IntelliJ to recognize classes generated by AnnotationProcessor - java

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).

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.

You can go on pom/gradle file and Reload it again, it will work.
example :
pom.xml(right-click)-> Maven-> Reload

Related

How to get lombok generated source to be visible in eclipse/maven?

I am attempting to use lombok (https://projectlombok.org/) to help with "boilerplate" code generation. I have added the maven dependency and have also added the lombok maven plugin to the <build> element in the POM.
One of the things I was also told to do was to place the lombok-annotated code in src/main/lombok instead of src/main/java. I assume that I keep the package directory structure the same as in src/main/java.
This seems a little odd, but I did this and lombok did generate source files with all the getter/setter/equals/hashCode etc methods. They are placed in e.g. target/generated-sources/lombok/x/y/z/MyClass.java. This is kind of what I would expect having used other code generation tools like XJC.
My question is: how do I get this generated source to be "visible" to eclipse and the compile environment? I'm getting compile errors all over the place because it can't "see" the generated code. This occurs in eclipse as well as when I attempt to do a mvn clean install.
UPDATE:
I have already tried installing lombok into eclipse. It generates the getters/setters/etc, but there is a bug introduced somewhere that breaks the auto-complete feature of eclipse. I removed it from eclipse and am just trying to get maven to do the generation (or delombok-ing).
I've just installed Lombok (after a while) in my new Eclipse (STS) installation, and i was a bit "annoyed" by the lack of generated sources.
I found a hint however on this page that the generated structure is visible in the "Outline" view. (Look for the screenshots)
If you don't have Outline view open yet, enable it in the menu: Window / Show view / Outline.
Download Lombok jar (https://projectlombok.org/download) and execute it java -jar lombok.jar and select your eclipse installed folder.Restart eclipse and rebuild project.
To verify Lombok installation, please check Help -> About eclipse. Lombok entry show show at bottom.
You have to enable annotation processing for Eclipse to be able to pick up generated code.
Right-click on the project and select Properties.
Open Java Compiler and then Annotation Processing. Check Enable
annotation processing.
Open Java Compiler -> Annotation Processing -> Factory Path. Check
Enable project specific settings. Add your Lombok JAR file to the list.
Clean and build the project.
My problem was using full namespace annotation: #lombok.Getter, and I resolved it by doing import lombok.Getter and do #Getter

Received "Variable information not available, source compiled without -g option" error

After I created a new class in netbeans, I received "Variable information not available, source compiled without -g option" error trying to debug the new class. I deleted that class, and decided to use a different class, which was not anyway related to the previously deleted class, that worked perfectly with the debugger yesterday. I also receive this error, and it appears that this issue is permanent. I have gone through some related posts, but they do not appear to have the same issue. I have checked to make sure "generate Debugging info" was checked, and it was.
What causes this error, and why is it now permanent regardless of the class I try to debug?
Take a look at the javac -g options, especially -g and -g:none.
If you use a build infrastructure like maven or gradle then there might be something wrong.
But for NetBeans itsef look at the compiler options.
Go to the Project Properties (not global options), where the checkbox "Generate Debugging info" should be checked.
There also is a text field "Additional Compiler options" that should normally be empty.
Do a clean build.
I must admit, that the behaviour there almost looks like project garbage. But more to be expected from eclipse than NetBeans.
As the mention is of "Variable information not available," it seems
the option -g:vars is wanted for some reason (some class manipulating library,
documentation tool, annotation processing). Check whether you installed some NetBeans plugin in that area. By default debug information on variables is not added, as for exceptions the other two suffice.
Additional compiler options: -g:source,lines,vars
I had the same problem. For me it was because I'd created a new (completely empty) Java project under the default NetBeansProject folder and then added a pre-existing source folder to the project, which was located somewhere else.
Right-click project --> Properties --> Sources --> Source Package Folders
Remove any extraneous/empty folders.
Click OK and try debugging the project again.
I'd created a separate project folder to house all of NetBeans' meta junk (so that it wouldn't contaminate my real project folder).

Problems compiling AutoValue library example project

I just discovered google's AutoValue library, which seems great.
I'm trying to compile the example project which I downloaded from here
But the project doesn't compile.
The compiler complains about the AutoValue_ constructor symbol not being recognized.
Can someone explain what I am doing wrong?
I am using auto-value-1.3.jar
Thanks
IntelliJ does not enable annotation processors by default.
You can follow the official guide. You need to go to the Annotation Processors page and click + to create a new profile. Then you have to associate your module with this profile. In the end you only need to enable annotation processing for this profile and everything should work.
The IntelliJ editor might not find the generated class files though and still display the name red. But it should not affect the build.

IntelliJ IDEA code inspection profile to show dependency and resolving errors

I have a multi-module project which is amidst migration process. I cannot compile some dependent module and thus it's hard to track dependency/resolving errors without explicitly opening each and every file to be checked by the editor.
I created code inspection profile which can show me syntactic errors and but I'm not able to configure it to show everything that should be red-underlined (like e.g. Eclipse problems window).

Class file not found in IntelliJ

In IntelliJ I get this weird error when I try to compile.
Cannot find annotation method 'cascade()' in type 'javax.persistence.ManyToOne': class file for javax.persistence.ManyToOne not found
I know, it seems pretty obvious what the problem is, but having spent too much time on this problem I now turn my trust to you!
I use ideaCommunity-9, where I have created a global library for JBoss, which contains all JBoss jars including ejb3-persistence.jar. My module includes this library, and I have moved it to the top of dependencies.
No otherwhere in classpath I have a javax.persistence.ManyToOne class, and it also opens up in IntelliJ without problems.
If I open the file where I get the compile error, IntelliJ shows no problems and also navigates to the class file on pressing Ctrl+B.
The code is also build with ant and here it works fine.
Besides the above error I get six others along the same path (name in Table, etc.) - all related to JPA. I also get a message saying "Please file a bug to JDC yada yada yada."
Try to tick a checkbox "export" in your dependencies.
I mean: "File" menu -> Project Structure -> Modules -> Dependencies, and there you can see libs included to the module. You should also see a checkbox near each lib in column 'Export'.
Just got this myself but non of those things were my issue. I downloaded a project from github, then opened in intelli J with the problem after trying to run a junit test.
Solution: Project Structure -> Modules -> 'Project_Name' -> Sources (tab) -> click src folder -> Mark as: Sources
This should highlight src to be blue and all the class files should be the circles with C instead of looking like normal files. Try running a test again and see if that does it.
I'm sure, it is too late to answer, but I had to follow a different approach for this error to resolve this issue.
I had to create this as global library and add that library to the module, then only the error is resolved.
Isn't that similar to this SO question?
The solution there was to add all JBoss jar (which you did), but also all persistent-related libraries (like Hibernate)
See this JBoss project configuration for example:
alt text http://jdick.co.uk/blog/wp-content/uploads/2008/12/projectstructure1.jpg
In short, ejb3-persistence.jar might not be the problem here, just a symptom of other elements missing.
Or, if it is the problem, it may be related to the fact that it is not deployed correctly (for the WebApp configuration).
Here's an idea:
Maybe you have multiple versions of your jar file.
Search your folder hierarchy for the file ejb3-persistence.jar,
using a file management tool such as Total Commander.
Multiple appearances of the file in your classpath may confuse IntelliJ Idea
into not recognizing the proper class in the proper jar.
had a similar problem ("cannot find class spring...DefaultResourceLoader even though it was in my maven dependencies) and no amount of tweaking of revalidating caches seemed to help. Blowing away my $HOME/.Idea* paths and (basically) starting from scratch fixed it for me. At least for now...
I was getting the class not found error message when trying to execute a .groovy test class, which referenced a .groovy file (located in the root of the source directory of a submodule - i.e. no packages).
Moving the source file .groovy into a package, fixed the issue for me.
Move:
src/main/groovy/RunMe.groovy
to
src/main/groovy/somePackage/RunMe.groovy
In intelliJ I got this problem when I built (ctrl+shift+F9) at the file level.
The reason is, I had changes in other files in the same maven project, that were not getting picked.
I solved it by building at the project level (Select the Maven project in intellij and press ctrl+shift+F9)

Categories

Resources