还在写恶心的trim代码吗?用这个注解让你舒舒服服

  金牌会员 | 2023-3-16 03:14:13 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 507|帖子 507|积分 1521

背景

业务系统开发时,你是否踩过这样的坑:

  • 业务说列表筛选姓名精准查询查不到人?
  • 导入数据时,明明看起来一样的ID却匹配不到DB里的数据?
  • 看起来一样的内容,SQL Group By 时出现好几行?
  • ……
DEBUG后发现,原来要么时用户传入或者导入的数据里有个空格,要么是数据库里不知道什么时候已经存了一个前后有空格的值。
总之,我们不知道它什么时候暴雷,开放的接口你也无法确定调用方(前端、服务间RPC调用、本应用内调用等)是否都帮你去除了两端空格,为了保证代码的健壮性,你只好写了如下般的代码:
  1. @PostMapping(value = "/login")
  2. public ResponseEntity<UserDetails> login(String username, String password, String captcha) {
  3.     Assert.hasText(username, "用户名不能为空");
  4.     Assert.hasText(password, "密码不能为空");
  5.     Assert.hasText(captcha, "验证码不能为空");
  6.     // 判断具有有效值后,还需要去除两端空白符
  7.     username = username.trim();
  8.     password = password.trim();
  9.     captcha = captcha.trim();
  10.     // Security Authorization...
  11.     return new ResponseEntity(null, HttpStatus.OK);
  12. }
复制代码
又或者,对象实体里更恶心的:
  1. @Data
  2. static class Student {
  3.     private String name;
  4.     private String phone;
  5.     private String idCard;
  6.     private String city;
  7.     private String specializedSubject;
  8. }
  9. @PostMapping(value = "/save")
  10. public ResponseEntity<UserDetails> save(Student student) {
  11.     Assert.notNull(student, "参数不能为空");
  12.     Assert.hasText(student.getName(), "学生姓名不能为空");
  13.     Assert.hasText(student.getIdCard(), "学生身份证号不能为空");
  14.     // 判断具有有效值后,还需要去除两端空白符
  15.     student.setName(student.getName().trim());
  16.     student.setIdCard(student.getIdCard().trim());
  17.     // 可选值如果有长度,还需要去除两端空白符;似乎感觉绝大多数的String类型参数都要去除两端空白符
  18.     if (StringUtils.hasLength(student.getCity())) {
  19.         student.setCity(student.getCity().trim());
  20.     }
  21.     if (StringUtils.hasLength(student.getPhone())) {
  22.         student.setPhone(student.getPhone().trim());
  23.     }
  24.     if (StringUtils.hasLength(student.getSpecializedSubject())) {
  25.         student.setSpecializedSubject(student.getSpecializedSubject().trim());
  26.     }
  27.     // Service call...
  28.     return new ResponseEntity(null, HttpStatus.OK);
  29. }
复制代码
然后你发现,似乎整个业务系统里,绝大多数的字符串类型参数,都应该去除两端空白符,可是每个字段你都这么写一遍,不仅篇幅过长,还极其恶心人。
那怎么办?又想代码健壮一点,避免暴雷,又想代码简洁增强可读性。
那你可以试一下AutoTrim这个小巧的工具,它可以帮你优雅地解决这个情况。
AutoTrim

概述

**AutoTrim**是一个小巧且nice的APT工具,它能够帮你减少String类型参数的trim()代码书写。
什么APT工具?就是类似于Lombok,APT(Annotation Processing Tool),也就是大家常说的编译期间注解处理器。
注解


  • @AutoTrim对注释的对象进行AutoTrim操作,即添加 string == null ? null : string.trim() 代码.
  • @AutoTrim.Ignored进行AutoTrim操作时,忽略对该注释的对象。
快速使用


  • 添加依赖
  1. <dependency>
  2.     <groupId>com.supalle</groupId>
  3.     <artifactId>auto-trim</artifactId>
  4.     <version>1.0.0</version>
  5.     <scope>provided</scope>
  6. </dependency>
复制代码

  • 使用注解改写上面的伪代码

    • 伪代码 1 改写:

  1. // 只需要在方法上用@AutoTrim注解修饰即可
  2. @AutoTrim
  3. @PostMapping(value = "/login")
  4. public ResponseEntity<UserDetails> login(String username, String password, String captcha) {
  5.     Assert.hasText(username, "用户名不能为空");
  6.     Assert.hasText(password, "密码不能为空");
  7.     Assert.hasText(captcha, "验证码不能为空");
  8.     // Security Authorization
  9.     return new ResponseEntity(null, HttpStatus.OK);
  10. }
  11. // 编译后,用IDEA查看反编译结果
  12. @PostMapping(value = "/login")
  13. public ResponseEntity<UserDetails> login(String username, String password, String captcha) {
  14.     username = username == null ? null : username.trim(); // 已自动添加
  15.     password = password == null ? null : password.trim(); // 已自动添加
  16.     captcha = captcha == null ? null : captcha.trim();    // 已自动添加
  17.     Assert.hasText(username, "用户名不能为空");
  18.     Assert.hasText(password, "密码不能为空");
  19.     Assert.hasText(captcha, "验证码不能为空");
  20.     // Security Authorization...
  21.     return new ResponseEntity(null, HttpStatus.OK);
  22. }
复制代码

  • 伪代码 2 改写:
  1. // 直接给实体加上@AutoTrim注解
  2. @Data
  3. @AutoTrim
  4. static class Student {
  5.     private String name;
  6.     private String phone;
  7.     private String idCard;
  8.     private String city;
  9.     private String specializedSubject;
  10. }
  11. // 那么方法里的代码就可以简化为:
  12. @PostMapping(value = "/save")
  13. public ResponseEntity<UserDetails> save(Student student) {
  14.     Assert.notNull(student, "参数不能为空");
  15.     Assert.hasText(student.getName(), "学生姓名不能为空");
  16.     Assert.hasText(student.getIdCard(), "学生身份证号不能为空");
  17.     // Service call...
  18.     return new ResponseEntity(null, HttpStatus.OK);
  19. }
  20. // 编译后,用IDEA查看反编译结果
  21. static class Student {
  22.     private String name;
  23.     private String phone;
  24.     private String idCard;
  25.     private String city;
  26.     private String specializedSubject;
  27.     public void setName(String name) {
  28.         this.name = name == null ? null : name.trim();
  29.     }
  30.     public void setPhone(String phone) {
  31.         this.phone = phone == null ? null : phone.trim();
  32.     }
  33.     public void setIdCard(String idCard) {
  34.         this.idCard = idCard == null ? null : idCard.trim();
  35.     }
  36.     public void setCity(String city) {
  37.         this.city = city == null ? null : city.trim();
  38.     }
  39.     public void setSpecializedSubject(String specializedSubject) {
  40.         this.specializedSubject = specializedSubject == null ? null : specializedSubject.trim();
  41.     }
  42. }
复制代码
代码一下子就干净起来了。
@AutoTrim.Ignored

如果你想忽略个别参数、字段、方法被@AutoTrim影响呢,可以使用@AutoTrim.Ignored注解对目标进行标记,比如:

  • 方法内多个String类型的形参,忽略某一个不需要 trim()操作:
  1. @AutoTrim
  2. @PostMapping(value = "/login")
  3. public ResponseEntity<UserDetails> login(String username, String password
  4.                                          , @AutoTrim.Ignored String captcha) {
  5.     Assert.hasText(username, "用户名不能为空");
  6.     Assert.hasText(password, "密码不能为空");
  7.     Assert.hasText(captcha, "验证码不能为空");
  8.     // Security Authorization
  9.     return new ResponseEntity(null, HttpStatus.OK);
  10. }
  11. // 编译后,用IDEA查看反编译结果
  12. @PostMapping(value = "/login")
  13. public ResponseEntity<UserDetails> login(String username, String password
  14.                                          , String captcha) {
  15.     username = username == null ? null : username.trim(); // 已自动添加
  16.     password = password == null ? null : password.trim(); // 已自动添加
  17.     // 编译时就不会为captcha参数添加 auto-trim 的代码
  18.     Assert.hasText(username, "用户名不能为空");
  19.     Assert.hasText(password, "密码不能为空");
  20.     Assert.hasText(captcha, "验证码不能为空");
  21.     // Security Authorization...
  22.     return new ResponseEntity(null, HttpStatus.OK);
  23. }
复制代码

  • JavaBean里,某个String类型的属性不需要 trim()操作:
  1. @Data
  2. @AutoTrim
  3. static class Student {
  4.     private String name;
  5.     @AutoTrim.Ignored
  6.     private String phone;
  7.     private String idCard;
  8.     private String city;
  9.     private String specializedSubject;
  10. }
  11. // 编译后,用IDEA查看反编译结果
  12. static class Student {
  13.     private String name;
  14.     private String phone;
  15.     private String idCard;
  16.     private String city;
  17.     private String specializedSubject;
  18.     public void setName(String name) {
  19.         this.name = name == null ? null : name.trim();
  20.     }
  21.     public void setPhone(String phone) {
  22.         this.phone = phone;// 编译时就不会为phone属性的Setter方法添加 auto-trim 的代码
  23.     }
  24.     public void setIdCard(String idCard) {
  25.         this.idCard = idCard == null ? null : idCard.trim();
  26.     }
  27.     public void setCity(String city) {
  28.         this.city = city == null ? null : city.trim();
  29.     }
  30.     public void setSpecializedSubject(String specializedSubject) {
  31.         this.specializedSubject = specializedSubject == null ? null : specializedSubject.trim();
  32.     }
  33. }
复制代码
怎么样,是不是非常简单咧。
特性


  • 小巧,单一目标功能,简单易用;
  • 支持_JDK8~JDK21_;
  • 支持class类、接口、子类、匿名内部类的类级别;
  • 支持属性上使用;
  • 支持方法上使用;
  • 支持方法形参上使用;
  • 支持final修饰的形参;
源码仓库

开源协议:Apache License 2.0
GitHub:https://github.com/supalle/auto-trim
Gitee:https://gitee.com/Supalle/auto-trim
如果使用中有什么问题欢迎各位小伙伴提issues反馈,我会长期维护这个亲儿子项目。
更新周期

AutoTrim定位为单一功能,轻包袱的工具,因此暂时不会加太多其他与之无关的功能,如有新的APT点子,会另开项目推出,因此更新的频率不会太频繁。

  • 修复已知bug后,发布小版本;
  • JDK发布新版本后,会第一时间支持新的JDK版本,然后发布大版本。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表