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

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"

Related

How to include Javadoc in a library JAR?

I'm currently trying to write my first own library. It's just for testing, I want to find out how libraries are written, compiled, distributed and used in order to prepare for some upcoming personal projects.
Yet, what really causes me to wonder, is why exactly my Javadoc isn't compiled with the Library. I know that comments and annotations are not compiled, but for example the JDK (which is basically a huge library) comes with a working doc as well.
I've tried to compile a JAR (libraries aree normally JARs, right?) from not the compile output, but the sources (so I had a JAR of .java files), but this failed to be included in IntelliJ. I could add it as an external library, but neither did it show up in my explorer, not could I import or use any of my classes.
I know there must be a mistake somewhere here, all libraries I've ver used, whether it was Java, C# or whatever else always came with a working documentation (IntelliJ shold show that on mouse hover), and I'd like to know how to correctly build a library that I can share with a team partner, so he just needs to download it, add it as a library in IntelliJ and has all the functionality, but also the documentation.
Thanks to everyone in advance!
Because it isn't needed, and would bloat the file size of the executable. If you have a library in C or C++, the documentation may be bundled in a zip file, but you won't find it in the compiled .so or .dll. One just holds the binary and resources needed for the project. The .jar is equivalent of that- it's the compiled output. Documentation is hosted/downloaded separately.
When you download the JDK, you're not just downloading a giant .jar. It includes other things, like documentation in the download.
I'd like to know how to correctly build a library that I can share with a team partner, so he just needs to download it, add it as a library in IntelliJ and has all the functionality, but also the documentation.
The short answer is that you provide your team partners with your project source code as well as the binaries. They then can configure their IDE (Intellij, NetBeans, Eclipse, whatever) with the location of the source code and the IDE will be able to extract the javadoc comments on the fly and render them as requested.
Sharing the source code also has the additional benefit that your partners can debug their (and your) code better. By themselves, javadocs are rarely sufficient for debugging complicated problems.
Alright, if everyone ever has this probelm again, here's a complete tutorial:
As #Gabe Sechan already said, the Doc is not compiled into the JAR for some valid reasons. Instead, I recommend you to add the following to your JAR:
module compilation output
content of "src" directory / alternatively: module sources
Build --> Artifacts --> All Artifacts.
Now, if you add your library JAR into a project, it will show "Classes" and "Sources" in the right tab, as IntelliJ automatically recognizes you've bundled both into the JAR.
Now, IntelliJ should show your documentation, as it lives within the source files.
If, for some reason, IntelliJ switches from its "fancy" documentation popup to unformatted plain text, proceed as follows:
Navigate to File -> Settings -> Advanced Settings, and in the 5th block, where it says "Documentation Components", just tick everything you find. That's gonna fix it.
Thanks to Gabe Sechan again, your answer helped me to understand what won't work, and finally I've found a way to make it work myself.

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.

Android with AspectJ and building it with Ant on Eclipse

I'm new to Android and wanted to use AspectJ with it.
I had searched couple articles online and follow the instruction to have it working:
http://blog.punegtug.org/2010/11/adding-aspect-to-android.html
But I wanted to know whether if it's possible to separate the aspects away from the Android project. In the tutorial link above, it has both the Android App and the aspects inside the same project, but in many cases, we wanted to leave the Android Project untouched in its isolating spaces.
Let said I have AndroidProject in my Eclipse workspace, I would like to create a separate projects for my aspects called something like "AndroidAspectProject" which only contains the aspects for it.
I'm not sure whether this would work because it seems we need to let AspectJ compiler inject point cuts and advices to the .class files before creating the .dex files. In this sense, I may not able to do it in a separate project.
Does anyone try with this?
Another related question would be:
Is it possible to have Ant build the AndroidProject with AND without aspects on it? Can this be done outside of Eclipse?
I'm looking for a way to build different flavours as I'm only injecting pointcuts into the AndroidProject on dev/debug build, but will leave it untouched on release build.
Whether or not to do the compile-time aspects is a matter of whether or not you run the aspectj ant tasks. Have separate targets or properties for the AOP- and non-AOP-builds and either build one based on a target name or property, or build them both and change the artifact name.
IIRC Eclipse allows you to specify an Ant target to run on a build.
Inside of Eclipse, this is simple. Just add AndroidAspectProject to the aspect path of AndroidProject.
Inside of ant, there are several ways of doing this. But, the simplest is to define 2 targets. One that uses iajc and the other that uses javac to compile your sources. You then need to use a little ant magic switch between targets depending on whether you are compiling for dev or for production.

netbeans: how to determine unused JARs?

Over the course of developing my Spring MVC project, I've accumulated an overabundance of JARs, many of which I suspect are no longer necessary.
I see that Eclipse has a plugin that can help find unused JARs. Is there an equivalent for Netbeans or something that works on the command line?
I don't think this can be done in a reliable way.
What if you have classes that are loaded via reflection?
What if you have classes that are only referenced from within a Spring XML file?
They will never show up in the Java source code
A good command line tool would be Tattletale
You can check other recommendations here:
How to Determine which JARs are Used in an Application
I believe Netbeans has an inbuilt function for that? "Remove unused dependencies" it is called iirc.
Well, that works for a single file. If you want more than one, 6.8 should add that according to Organising Imports in NetBeans

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