免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1888 | 回复: 0
打印 上一主题 下一主题

使用 Lombok 简化项目中无谓的Java代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-10 13:30 |只看该作者 |倒序浏览
在写使用Java时,难免会有一些模板代码要写,不然get/set,toString, hashCode, close 资源,定义构造函数等等。代码会显得很冗余,很长。Lombok项目可以是我们摆脱这些东西,通过一系列的注解,Lombok可以帮我们自动生成这些函数。

Lombok 官网地址:https://projectlombok.org/

参考文档:https://projectlombok.org/features/index.html

1. 安装

到官网下载 lombok.jar,直接双击,按照提示进行操作,就可以在eclipse中安装成功。

如果使用maven时,则需要引入依赖:
  1. <dependency>
  2.         <groupId>org.projectlombok</groupId>
  3.         <artifactId>lombok</artifactId>
  4.         <version>1.16.4</version>
  5.         <scope>provided</scope>
  6.     </dependency>
复制代码
如果需要用javac或者其他命令工具编译java类,则需要将 lombok.jar放入classpath.

2. 使用方法 (文档:https://projectlombok.org/features/index.html)

1> @Getter/@Setter, 注解在一个pojo类上,会在编译时,帮我们自动生成get/set函数。

2> @ToString 注解在类上,编译时,帮我们生成包括所有field的toString函数;

3> @EqualsAndHashCode,  编译时,帮我们生成equlas 和hashCode函数;

4> @Cleanup, 注解在一些资源对象的定义上,可以帮我们自动调用它们的close()函数;这个很有帮助;

5> @NoArgsContructor,@RequireArgsContructor, @AllArgsContructor,分别帮我们生成无参数构造函数,每一个非Null的field的构造函数,所有field参数的构造函数;

6> @Data,All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor! (等价于:@ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor)

更多的注解,参见https://projectlombok.org/features/index.html

3. 例子
  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Test {
  5.     private int id;
  6.     private String name;
  7.     private String password;

  8.     public static void main(String[] args) {
  9.         Test test = new Test(1, "test", "password");
  10.         System.out.println(test);
  11.         System.out.println(test.getName());
  12.     }
  13. }
复制代码
结果:
  1. Test(id=1, name=test, password=password)
  2. test
复制代码
通过@Data, @AllArgsConstructor,@NoArgsConstructor 三个注解自动 生成了 Test 的全field参数的构造函数,自动生成了 toString(), get/set函数等等。

再看一例:
  1. public static void main(String[] args) throws IOException{
  2.         @Cleanup
  3.         InputStream in = new FileInputStream("/home/a.txt");
  4.         @Cleanup
  5.         OutputStream out = new FileOutputStream("/home/b.txt");
  6.         byte[] b = new byte[10000];
  7.         while (true) {
  8.             int r = in.read(b);
  9.             if (r == -1)
  10.                 break;
  11.             out.write(b, 0, r);
  12.         }
  13.     }
复制代码
@Cleanup自动帮我们调用 close() 方法进行关闭资源。

You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation like so:
@Cleanup InputStream in = new FileInputStream("some/file");
As a result, at the end of the scope you're in, in.close() is called. This call is guaranteed to run by way of a try/finally construct.

If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method, you can specify the name of this method like so:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
By default, the cleanup method is presumed to be close(). A cleanup method that takes 1 or more arguments cannot be called via @Cleanup.

@Cleanup是通过 try/finally 实现的,如果资源的关闭方法不是默认的close(),那么也可以指定关闭方法的名称@Cleanup("closeMethod"), 但是关闭方法不能有参数,不然就无法使用 @Cleanup了。

更多的 参考 https://projectlombok.org/features/index.html

通过使用 Lombok,可以减少很多的 Java 代码的,减轻了心理负担。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP