Lombok

Lombok:简洁代码、提高编程效率利器

£可爱£侵袭症+ 提交于 2019-12-24 16:18:27
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Lombok是一个可以让编程人员不必硬编set、get、构造以及hash和toString方法的插件,在编译时会自动根据定义的属性生成包含这些相关方法的class文件。 常用的注解如下: @AllArgsConstructor 作用于类,生成所有属性的代参构造函数 @NoArgsConstructor 作用于类,生成无参构造函数 @toString 作用于类,生成toString方法 @EqualsAndHashCode 作用于类,生成equals和hashcode方法 @Setter,@Getter 作用于类,为所有属性生成set和get方法,作用于属性,为该属性生成set和get方法; 下面这三个注解比较厉害了,终极大boss: @Data 作用于类,可以生成上面除了@AllArgsConstructor以外所有的方法。 若想生成所有参数构造,需配合@AllArgsConstructor使用,但是无参构造就会失效,所以需要自定义或搭配@NoArgsConstructor一起使用。 需要说明的是: 若自定义了set或get方法,@Setter,@Getter和@Data三个注解使用时是不会覆盖生成set或get方法的 @Builder 用于类,生成一个当前类的builder构建器

开发中的小技巧

旧巷老猫 提交于 2019-12-24 15:55:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1、lombok 简洁代码 lombok 其实就是一个注解包,他里面有一些可以简化代码的注解方法给我们使用,比较常用的是 @Data //生成所有非final的属性的get/set方法,以及toString等方法 @Getter/@Setter //生成所有非final的属性的get/set方法 @Builder //这个就是现在比较流行的操作方式,比如:Person.builder().job("he").job("job").build(); @Log //相当于创建了一个可操作的log4j对象,而且是final类型的 其他还有一些但是用到的不多,在idea中有对应的插件可以直接搜索到,因为是在代码编译的时候作用的所有基本不会影响到运行效率,不过好像还是有争议,总的来说还是不错的 详细:http://blog.csdn.net/ghsau/article/details/52334762 来源: oschina 链接: https://my.oschina.net/u/2607780/blog/795677

Use Lombok @RequiredArgsConstructor without @NonNull

坚强是说给别人听的谎言 提交于 2019-12-24 14:19:31
问题 I have something like this: @RequiredArgsConstructor public class Person { private String name; private LocalDate date = null; } I need a constructor for the name attribute only, but it is not working. If I use @NonNull it will build the constructor I need, but for my case the name should be able to be null, can not be final either. Any ideas? Thanks! 回答1: Lombok @RequiredArgsConstructor has no such option currently. You have at least two options: Just code the constructor public Person

Android Studio not playing well with Lombok

≯℡__Kan透↙ 提交于 2019-12-24 06:13:53
问题 I just upgraded to the latest Android Studio (2.1.1) and now it’s acting strange concerning Lombok. I have an arrangement such that class Dog implements interface IDog. @Data class Dog implements IDog{ private final List<String> toys; } interface IDog{ public List<String> getToys(); } Before the upgrade to 2.1.1 I never had a complaint, but now here is what’s happening In the IDE everything looks normal: no red squiggly underline asking Dog to implement getToys. When I run the code, Gradle

Android Studio not playing well with Lombok

て烟熏妆下的殇ゞ 提交于 2019-12-24 06:13:08
问题 I just upgraded to the latest Android Studio (2.1.1) and now it’s acting strange concerning Lombok. I have an arrangement such that class Dog implements interface IDog. @Data class Dog implements IDog{ private final List<String> toys; } interface IDog{ public List<String> getToys(); } Before the upgrade to 2.1.1 I never had a complaint, but now here is what’s happening In the IDE everything looks normal: no red squiggly underline asking Dog to implement getToys. When I run the code, Gradle

How to make Gradle add Lombok to its Project and external dependencies libraries?

大憨熊 提交于 2019-12-24 00:38:42
问题 I created a Java project in Eclipse Neon. I used Buildship 1.0.21 to import the project as a Gradle project and ran the wrapper and init commands to generate the build, settings and wrapper files. The project has an empty source folder because I am trying to solve a similar problem on a more complicated project and taking the divide and conquer approach - just add Lombok dependency. Here is my build.gradle as instructed on the lombok website: apply plugin: 'java' dependencies { compileOnly

Configuring lombok for builder

断了今生、忘了曾经 提交于 2019-12-23 22:13:09
问题 I want to avoid multiple constructors, so I want to use a builder design pattern, by using lombok library, it can be more easier, so I want to annotate class of ContractDTO with this library annotation: @Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder(toBuilder = true) class ContractDTO { private Integer id; private String name; private Integer acquirerId; private Integer terminalId; private String merchantId; } then your code can be : ... .map(g -> new ContractDTO().toBuilder(

Is there any “PostConstruct” feature of lombok?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 21:43:51
问题 Is there a way that I can define a "PostConstruct" initialization method with lombok? @RequiredArgsConstructor(staticName = "of") class MyObj { private final int x; private final int y; private int z; // not work @PostConstruct private void someInitLogic { z = x + y; } public void start() { // code use "z" } } So that I can init an object like: MyObj obj = MyObj.of(1, 2); obj.start(); 回答1: Not yet. There's an open issue named just like your question. Unfortunately, Lombok development is

Any way to do Android binding with Lombok accessors?

青春壹個敷衍的年華 提交于 2019-12-23 20:31:17
问题 I'm starting to play with Android binding. The standard (1-way) binding is to the point of being good enough for the people I hang out with. However, I find that I can't use Lombok accessors without a Could not find accessor error. Have you found a way around this, shy of manually writing getters and setters like some kind of Lombok-ignorant cavebeast? @Bindable @Getter @Setter private String stringField; //Must uncomment hand-coded accessors to compile! //public String getStringField() {