Groovy #Builder not visible in Java (and eclipse) - java

I am trying to simplify my Java code by the use of groovy. At the moment I am struggling with the use of the #Builder annotation. I have a groovy like the following one….
import groovy.transform.builder.Builder
#Builder
class TheFancyOne {
String message
}
From Java I want to use the builder like…
TheFancyOne.builder().message("Hello World!").build();
The problem is, that only the getter and setter of the “TheFancyOne” is visible but no builder.
As a side note I am unsing groovy 2.4.5 and Eclipse (Neo with Groovy Eclipse)
--- edit ---
The builder is visible within groovy and it is also in the resulting jar (checked by decompiling). So it seems to be an eclipse plugin issue
Any idea?
Regards,
Thomas

Related

Java Records and Lombok annotation - IntelliJ

Just trying a hands-on the java.lang.Record. I have gone through the documentation and the JEP-359 for some understanding. So upon reading about the implicit declaration of the constructor I thought of mixing it up with an existing code generation library - Lombok!
Now what I've ended up creating as a minimal reproducible example is this record
import lombok.AllArgsConstructor;
#AllArgsConstructor
public record Java(String version) {
}
which when compiled using IntelliJ successfully produces the class file which looks like
public final class Java extends java.lang.Record {
private final java.lang.String version;
public Java(java.lang.String version) { /* compiled code */ }
... rest of the compiled code
}
Note that the constructor for the .class file is just what I would have expected in the two worlds independently as well. But, further trying to create an instance of this record fails during compilation in IntelliJ :
public class MixOfWorlds {
public static void main(String[] args) {
System.out.println(new Java("14").version()); // cannot resolve constructor
}
}
I would create a further simpler example to perform the compilation with javac and execution with java tools. I am still looking for an answer if this is a possible expected behavior that could occur because of something I might have overlooked?
IntelliJ IDEA 2020.1 EAP (Community Edition)
Build #IC-201.6487.11, built on March 18, 2020
Runtime version: 11.0.6+8-b765.15 x86_64
macOS 10.14.6
This is how it reflects in IntelliJ for both the cases - with and without the #AllArgsConstructor.
Following up on this and with some help online from the IntelliJ developers, I had tried the following steps to resolve this --
I was assured that it looks more about javac-lombok interaction and was not connected to IDE.
I could actually gain the confidence to try running the code despite the highlighted error(which looks like a compile-time error) and it successfully executed to print "14" as expected.
I can confirm that after disabling the "Lombok Plugin" installed in the IDE, the highlighted error disappears.
Note: The second step was with the plugin installed. So in short, it just happens that the plugin gets to highlight the code as if it wouldn't compile, but the actual execution is handled by IntelliJ properly. (Kudos!)
Edit: With the 1.8.20 release, Lombok prevents a record to be annotated with an AllArgsConstructor any more. You can access the official changelog here.

Difficulty extending Lombok

I want to create an annotation handler to extend the Java language. However I can't do this easily, because of the SCL files. I wanted to know if the Lombok developers made it this hard on purpose.
If I try extending JavacAnnotationHandler, it can't find it in lombok.jar because the name is JavacAnnotationHandler.SCL.Lombok. I cloned the GitHub repository but I keep getting errors with duplicate classes in the resources.after and resources.before package and Java 12 syntax. I am using lombok 1.18.8.
This is what I have.
import lombok.javac.JavacAnnotationHandler;
public class SingletonJavacHandler extends JavacAnnotationHandler<Singleton> {
It says "Cannot resolve symbol 'JavacAnnotationHandler'"
The SCL files are to hide lombok implementation classes from autocomplete dialogs in IDEs.
The resources-before and resources-after files should not be compiled. They are in the test resources, because our testing framework processes them.

Entities with #RequiredArgsConstructor error in IntelliJ IDE

I'm trying to run sample Spring boot application and I'm facing problem with entities that are marked as #RequiredArgsConstructor on my IDE. I'm using latest intelliJ IDEA (14.1) over java 1.8. There's an error marked on IDE when I tried to initialize the entity with constructor arguments.
E.g. It would be showing "cannot resolve symbol" for following line.
itemRepository.save(new Item("MacBook Pro"));
My entity would be as follows.
#Entity
#Data
#RequiredArgsConstructor
public class Item {
private #Id #GeneratedValue Long id;
private final String description;
Item() {
this.description = null;
}
}
Apart from IDE error project builds and runs properly.
The sample project you are running uses Lombok, a library which can generate a lot of boilerplate code for you (such as getters and setters) based on annotations (e.g. #RequiredArgsConstructor). This is useful, but because the code is generated during compilation, the IDE doesn't see it and therefore shows errors.
You must install Lombok plugin to make IntelliJ aware that the constructor actually does exist, but is generated during compilation. Then the errors will go away.
You can also take a look at this post for more details about how Lombok works under the hood.

Netbeans groovy code completion not working

I have created a new Gradle project in Netbeans where I want to use java and groovy classes together. To make it work I have configured my build.gradle with the following code:
apply plugin: 'groovy'
sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDir 'src/main/java'
The build process works like a charm: groovy and java classes interoperate transparently. The only annoying thing is the Netbeans code completion. Code completion of java classes does not work from groovy. Let's say I have the following classes:
*my.package1.JavaClass.java
my.package2.GroovyClass.groovy*
From GroovyClass, if I type 'JavaC' and hit CTRL+space the IDE does not present me any help to import and use JavaClass. I have to manually create the import statement. Moreover when I type '.' after the JavaClass instance Netbeans does not show the methods I could call. This happens also for classes included as dependencies.

How to actually make lombok annotations work for getters and setter

I am trying to use lombok getters and setters annotations.
As far as I know the annotated code is generated at runtime and not compile time so how can take the help of autogenerated getters and setters for writing code?
for example I have a class like this
#lombok.Getters
#lombok.Setters
public class MyLombokTesting {
private String userName;
}
but what is the use of such annotations if these are not generated while writing code...
Now I want to do something like this
MyLombokTesting myLombokTesting = new MyLombokTesting();
String username = myLombokTesting.getUserName();
or myLombokTesting.setUserName("some username");
but I can't do any of these since there are no setters and getters generated for me in eclipse while writing code..
Obviously I can have a one argument constructor to set the username but what about getter?
First of all, Lombok does run compile time to change the generated class file on the fly.
Possibly, lombok is not correctly installed in your Eclipse. See this answer on lombok installation problems in Eclipse.
Furthermore, the runtime processing of annotations is not the only use for them. Java 5 already shipped with apt, the Annotation Processing Tool and since java 6 annotations can be processed by the standard compiler (javac). Annotations can generate class files, source files or other resource files.
Disclosure: I am one of the Project Lombok developers
Make sure you Restart Eclipse/Intellij or Any IDE that you are working in after adding Lombok jars and plugins.
I use Eclipse IDE. I have to install lombok in my computer before adding lombok library in pom.xml
Step 1:
- Download lombok at https://projectlombok.org/download
- Run command java -jar
- Restart your IDE
- Finish
Step 2:
- Add lombok to pom.xml file
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
call getter / setter in your code

Categories

Resources