When I imported the source for a project, I got a lot of build errors. There are a lot of errors that make XXXBuilder not exist. If you look at build.gradle, you can see that it uses lombok's library, but in fact, you need to install the Lombok plugin in the IDE as well.
Lombok https://projectlombok.org/
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
Spring Tool Suite
https://projectlombok.org/download
Installation is complete.
Idea
If it already exists in Installed, you do not need to install it.
Press the Restart IDE button to complete.
VSCode You can avoid build errors with an extension called Lombok Annotations Support for VS Code. https://marketplace.visualstudio.com/items?itemName=GabrielBB.vscode-lombok
After installing, restart VS Code just in case
Lombok features https://projectlombok.org/features/all
build.gradle
// https://mvnrepository.com/artifact/org.projectlombok/lombok
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.12'
@Getter
/@Setter
It's easy to understand, but getter and setter methods are automatically generated.
@ToString
The toString method is defined and controlled as @ToString (exclude =" age ", callSuper = false)
Only the excluded age field is unused.
@EqualsAndHashCode
Three methods, hashCode, equals and canEqual, are generated.
@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor
@Value
@Builder
@Data
As shown in the above example, it is a summary of the following annotations.
@ToString
@EqualsAndHashCode
--@Getter
for all non-final fields
--@Setter
for all non-final fields@RequiredArgsConstructor
@Log
@Cleanup
Annotation that automatically releases resources. This is convenient because you can omit the resource close process yourself.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import lombok.Cleanup;
public class TestMain {
public static void main(String[] args) throws Exception {
@Cleanup
InputStream in = new FileInputStream(args[0]);
@Cleanup
OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[1024];
while (true) {
int r = in.read(b);
if (r == -1) {
break;
}
out.write(b, 0, r);
}
}
}
Hakamo val
, var
, @ NonNull
,@ Getter (lazy = true)
, @Helper
, @ Slf4j
and so on.
Using these annotations can reduce the amount of bean classes and improve maintenance.
that's all
Recommended Posts