Sonntag, 18. September 2022

Java + Vim: Annotation Processing for Lombok, QueryDsl and such

Getting Java working with Vim is quickly done with vim-lsp and vim-lsp-settings.

But using generated classes, like those from Lombok or QueryDsl or MapStruct, needs a little more work.

Especially the Query-Entities from QueryDsl made problems and weren't recognized in the projects I am working on. 

Eclipse has an extra option for "Annotation Processing", which needs to be turned on and configured correctly.


The gradle plugins Eclipse and com.diffplug.eclipse.apt helps with setting this up. The second one generates entries in the factory path, which gets used by the annotation processor. And the first one generally generates project files for eclipse, and triggers the apt-plugin.

After adding those two plugins into the build.gradle under plugins

plugins {
    id 'eclipse'
    id "com.diffplug.eclipse.apt" version "3.38.0"
}

run ./gradlew eclipse to generate the neccessary files.

Check the ".settings/org.eclipse.jdt.apt.core.prefs" settings file. It should have aptEnabled set to true, and look similar to this:

org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genTestSrcDir=.apt_generated_tests
org.eclipse.jdt.apt.reconcileEnabled=true
org.eclipse.jdt.apt.genSrcDir=.apt_generated
eclipse.preferences.version=1

Now there is a problem with lombok, because this already gets injected into the eclipse-jdt-ls via

-javaagent:\$DIR/lombok.jar -Xbootclasspath/a:\$DIR/lombok.jar

To fix this, change the .factorypath by locating the lombok entry and setting enabled="false".

Identification, auto-completion, usage of generated classes should now work nicely.

You can visually check annotation processing by loading the project in eclipse, opening the Project Properties → Java Compiler → Annotation Processing → Factory Path. Annotation processing should be enabled, and the Factory Path should contain multiple entries, with lombok being turned off.

If it's not working, a check of the lsp-log might give some inside. Enable it by setting the correct options inside the .vimrc, e.g. in vim9script:

g:lsp_log_verbose = 1
g:lsp_log_file = expand('~/vim-lsp.log')

Then re-open vim and the Java-files. Check :LspStatus for eclipse-jdt-ls to be running and working. Try using a generated class, or using auto-completion on such object. And then check the content of ~/vim-lsp.log

Keine Kommentare:

Kommentar veröffentlichen

[Review/Critic] UDock X - 13,3" LapDock

The UDock X - 13.3" LapDock is a combination of touch display, keyboard, touch-pad and battery. It can be used as an extension of vari...