Java annotation not usable from Kotlin project jar - java

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

Related

Spring Data not working for Gradle projects under Eclipse without #Param annotations for method parameters

So I'm using Gradle, Eclipse, Java 11, Spring Boot with some Spring Data JPA repositories.
In Eclipse, I've tested with 3 different JDK libraries configured in the build path and in the execution: OpenJDK, OracleJDK and GraalVM.
I get the same error every time:
Caused by: java.lang.IllegalArgumentException: Either use #Param on all parameters except Pageable and Sort typed once, or none at all!
Because of a line like:
#Query("select e from Entity e where e.value = :value")
Optional<Entity> findByValue(String value);
Guaranteed to work would be to add #Param("value") for the parameter, but I don't want to do that because this project has a lot of repositories.
If I run this under the command line ./gradlew bootRun where I set one of the JDKs, this error does not occur.
If I try to reproduce this under a new project, but under Maven where I set the properties for java and maven for Java 11, then it works just fine under Eclipse.
The Gradle project works fine under a IntelliJ IDEA.
Can someone please help in explaining why this is happening? Does Eclipse bode well with Maven but not with Gradle? I can't fine an explanation.
It's a compiler argument.
Seems that when the code is compiled, depending on the project, it will run with or without the -parameters argument to the javac command. When you run javac --help, the following line explains:
...
-parameters
Generate metadata for reflection on method parameters
...
This parameter is vital if you don't want to use #Param("value") in the repository method parameter, because this helps Spring Data determine the name of the parameter and assign it to :value.
In Eclipse(as of 2022-06), for your specific project, go to project Properties > Java Compiler and check "Store information about method parameters(usable via reflection)" and the project will work without "#Param" annotations in the repository method parameters:
I still find it strange why on Maven projects this is activated but not on Gradle projects.
These answers helped me:
https://stackoverflow.com/a/38198699/4632181
https://stackoverflow.com/a/44075684/4632181
For IntelliJ IDEA, this might be a useful answer:
https://stackoverflow.com/a/24652186/4632181

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

Custom Annotation Processor with Netbeans Module

I'm doing work with a (non Maven, I believe) Netbeans module, which is being used as a Plugin for a program built on top of the Netbeans platform.
What I wanted to do was use custom annotations to add a sort of documentation of how to use the GUI elements (of the plugin I'm working on), when a user goes to the help menu and selects "how to use".
My thought was create a custom annotation, where a user supplies name, and help information for a class. Then the annotation processor goes through the annotations, and outputs a class with the annotation information stored in a class field. This class can then be extended from, and used to display the information in a useful way.
Following this example https://netbeans.org/kb/docs/java/annotations-custom.html
I'm using Java 7, but both the Module and the Annotation Processor Jar are compiled as Java 6. My Netbeans Version is 7.3.1, however I believe the module is being developed for Netbeans Platform 7.0.1.
I have the annotation, and annotation processor compiled into a Jar, which if I use with a regular java project, it works as expected.
However, I cannot get the processor to run during compiling with the Netbeans module project.
I tried adding the Annotation Processor class to the META-INF.services, javax.annotation.processing.Processor file, in the netbeans module.
I tried adding the Annotation Processor class to the META-INF.services, javax.annotation.processing.Processor file, in the Annotation Processor project. (This made it work with my test Project)
I tried adding the -processor compiler flag to the netbeans module project to use the specific annotation processor.
After removing the extends relationship between my class and the expected auto generated class, output from the annotation processor, the code compiled. But at that point I noticed that the annotation processor was in fact being run.
I added the extends relationship back in, and cleaned and compiled again, and it failed to compile but the annotation processor was run, and successfully generated the output file as hoped. I hit build, without cleaning, and it built a final time, and worked as expected.
I don't know what is causing this, it appears to be, at least for a Netbeans module project, that the build order is different than a regular Java Project. Or perhaps there are several intermediate build steps which don't always get cleaned up.

Error compiling Java/Scala mixed project and Lombok

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.

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