Error compiling Java/Scala mixed project and Lombok - java

I am trying to compile a Maven Java/Scala mixed project that has a Scala class that depends on a Java bean with lombok annotations. I tried adding the lombok jar file to the boot classpath of the Scala compiler as well as the lombok agent, but the compiler still failed to find the generated getters. Is there a way for the Scala compiler to recognize the lombok annotations? If not, what would be a good workaround?
Pease note that I am trying to avoid introducing another maven project just for compiling this bean first as the bean logically belongs to the same project. Also I cannot rewrite the bean in Scala as it is later used in a GWT project.
Thank you,

I think you'll not be able to avoid it. Normal Scala/Java integration works like this:
Scala goes first, since Java doesn't know anything about Scala.
Scalac parsers Java files and learns about visible elements.
Scalac reads Scala files and generate class files.
Java goes last, and reads Java files plus the class files generated by Scala.
The obvious problem is that Scala doesn't know anything about Lombok annotations, so it can't figure out the elements generated by it.
If you don't have any dependency from Java to Scala, you can simply invert the order: let Java go first, and have Scala include the output classfiles of javac on its classpath.
Otherwise, I suppose you'll need to break it up.

Related

How to (quickly) replace all Lombok annotations with generated methods?

I'm working on a Spring Boot project for university. The deadline is in a week and I'm 99% done with everything. Now, my instructor has reached out to me and asked me to remove Lombok from my project because (from how I understand it) he has a personal issue with it.
While I can't fathom why anyone would give such a requirement and really don't want to remove Lombok, I have to obey.
So now my question is: Is there a way to quickly replace all my Lombok annotations with its generated methods? If there is no way, I guess I'll have to resort to refactoring everything manually...
Thanks for any help.
The Lombok .jar file provides a 'delombok' tool, which will process all of the Java files in a given directory and output the generated code to another directory.
The usage looks like this:
java -jar lombok.jar delombok src -d src-delomboked
There is also a Maven plugin for this.
Alternatively, if you're using IntelliJ, the Lombok plugin also provides this functionality, under 'Refactor' -> 'Delombok'.
I have just tested this out on one of my own projects, and the resulting code does not have any references to Lombok, so hopefully it will be enough to make your instructor happy :)
What is the IDE you're using? Most IDEs have shotcuts for this. For instance, in Eclipse, you could generate Getters and Setters with "Right click" --> "Source" --> "Generate Getters and Setters"

Set custom compiler in eclipse (omp4j)

So I am trying to use omp4j with the eclipse IDE. The problem is, that omp4j needs to replace the javac command to work (see http://www.omp4j.org/download). And I don't know how I can accomplish that in eclipse other than renaming the omp4j.jar to javac.jar and replacing my JDKs javac.jar and that seems like a wrong solution.
omp4j is a preprocessor. If omp4j is called without --no-compile, the preprocessed Java source code will be automatically compiled via javac, so omp4j can be used as a replacement for javac.
Eclipse has its own incremental Java compiler which can not be replaced. This means, in Eclipse omp4j has to be used with the argument --no-compile as preprocessor only. The processor can be executed
in an Ant, Maven, Gradle, etc. build script or
via an Ant build script as project builder on save.
To have the full Java support for the sources before preprocessing, the OMP4J_THREAD_NUM and OMP4J_NUM_THREADS constants can be faked via a static import statement with the * wildcard and a JAR that exists in two different versions, one with these face constants for the sources to edit and one with other constants for the generated sources which are not intended to be edited.
Probably it will be best to start with a Java project for each, before and after preprocessing.

Java annotation not usable from Kotlin project jar

I am working on library that I am transferring from Java to Kotlin. Since the projects is still mixed with both languages, I use kotlin-maven-plugin and maven-compiler-plugin as described in https://kotlinlang.org/docs/reference/using-maven.html . Kotlin version is 1.2.10.
I need to create an annotation in the library, that is going to be used in other projects. The annotation is a standard Java one, and I am able to build my library.
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
public #interface EnableSomeBehavior {
}
When I import the library in another project (a Java project), I can see the annotation. The auto-completion in my IDE (IntelliJ) actually proposes me annotation.. but for some reason, is not able to import it : when I validate the auto-completion choice, then I am getting a compilation error "cannot resolve symbol 'EnableSomeBehavior' ".
This is quite puzzling.. I've rebuilt several times (in case the jar was corrupted), but it didn't change anything.
I suspect it's something that has to do with the way the Kotlin/Java library is compiled and/or packaged.. The Java annotation is there in the jar, I can see it, and to some extent, the IDE can also see it, since it's proposing it. But somehow, it's not able to use it. It's not an IDE issue, because even compile with Maven from command line fails with the same error.
Any idea of what the problem could be ?
Thanks
Initially, I found that it was working better if the annotation was a Kotlin one instead of a Java one :
#Target(AnnotationTarget.CLASS, AnnotationTarget.FILE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
annotation class EnableSomeBehavior
After rebuilding my library, IntelliJ wouldn't show any error in the project using the annotation. However, compilation would still fail !
I ended up finding that it's not a Kotlin issue at all but a Spring Boot packaging issue : Using Kotlin classes from jar built with kotlin-maven-plugin

Getting warning "The following references to java symbols could not be resolved." when running Fortify maven plugin

I am using sca-maven-plugin to scan a multi module maven project. Unfortunately I ran into the the following warning while executing translate
[warning]: The following references to java functions could not be
resolved. These functions may be part of classes that could not be
found, or there may be a type error at the call site of the given
function relative to the function declaration. Please ensure the java
source code can be compiled by a java compiler.
The code can be compiled by a java compiler, which leads me to believe that something is wrong with the classpath. Despite this I remain unconvinced due to the fact that as I understand it Maven handles the classpath and passes it to sourceanalyzer. How do I solve the issue?
Check the maven compiler plugin you are using . May be you are using some code which compiles with the higher version of java.
Also makes sure all the dependencies are added properly and the jars are apart of classpath.
Please share your pom.xml

Maven Plugin to automatically generate setters/getters?

Is there a Maven Plugin that will automatically generate setters and getters with the corresponding JavaDocs?
I am aware that Eclipse/Netbeans will do this when you tell it to; however, it would be nice for the source to simply contain the skeleton and have Maven or another tool generate the repetitive stuff.
I would want to modify the source code so that a source jar can be compiled and used when debugging.
Thanks,
Walter
This isn't necessarily something that you want maven to do for you. It will make working with the code in the IDE harder, as the IDE won't necessarily know about the generated code unless it has a plugin that understands Lombok's notation. IntelliJ has such a plugin available.
That said, project lombok aims to do this properly through the use of an #Data annotation. It looks like it works well, but I haven't tried it. It supports a number of environments and IDEs through plugins, including Maven, Eclipse, IntelliJ and Netbeans. There are a few caveats with Netbeans currently, see the project documentation.
You could try Modello, it allows you to specify a model and let the java be generated during the build by the modello-maven-plugin.
I know you asked for a Maven plugin but there is annotation based project that takes care of many boilerplate code issues in Java:
http://projectlombok.org
There is a use case that doesn't work with Eclipse and the mouse.
Sometimes you need accessors in generated Java files, say, from an IDL specification.
One example would be if the classes are further processed by an ORM framework.
Now, how can this easily be done with Maven?
Of course, it's possible to script it or use the replacer plugin; but is there any off-the-shelf solution?

Categories