语言: 中文 | English
A small and nice APT tool. 一个小巧且nice的APT工具。
Auto-Trim使用Java编译时注解处理器( Annotation Processing Tool)帮助你自动生成String参数的String.trim()的操作。
@AutoTrim
对注释的对象进行AutoTrim操作,即添加string == null ? null : string.trim()
代码.@AutoTrim.Ignored
进行AutoTrim操作时,忽略对该注释的对象。
- 小巧,单一目标功能,简单易用;
- 支持JDK8~JDK21;
- 支持class类、接口、子类、匿名内部类的类级别;
- 支持属性上使用;
- 支持方法上使用;
- 支持方法形参上使用;
- 支持final修饰的形参;
- 仅支持
java.lang.String
类型的变量进行AutoTrim操作; - 不支持
record
类型的类构造器;
<dependency>
<groupId>com.supalle</groupId>
<artifactId>auto-trim</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
可以在 maven.org 查看最新可用的版本。
- 编译前
package org.example;
import com.supalle.autotrim.AutoTrim;
public class Example {
@AutoTrim
public boolean login(String username, String password, Integer type) {
System.out.println(username);
System.out.println(password);
System.out.println(type);
return true;
}
}
- 编译后,对方法所有
String
类型对形参进行了AutoTrim操作:
package org.example;
public class Example {
public Example() {
}
public boolean login(String username, String password, Integer type) {
System.out.println(username == null ? null : username.trim());
System.out.println(password == null ? null : password.trim());
System.out.println(type);
return true;
}
}
- 编译前
package org.example;
import com.supalle.autotrim.AutoTrim;
public class Example {
@AutoTrim
public boolean login(String username, @AutoTrim.Ignored String password, Integer type) {
System.out.println(username);
System.out.println(password);
System.out.println(type);
return true;
}
}
- 编译后,对方法所有
String
类型对形参进行了AutoTrim操作,但忽略了@AutoTrim.Ignored
修饰对形参:
package org.example;
public class Example {
public Example() {
}
public boolean login(String username, String password, Integer type) {
System.out.println(username == null ? null : username.trim());
System.out.println(password);
System.out.println(type);
return true;
}
}
- 编译前,可以配合Lombok一起使用
package org.example;
import com.supalle.autotrim.AutoTrim;
import lombok.Setter;
@Setter
public class Example {
@AutoTrim
private String username;
private String password;
private Integer type;
}
- 编译后,对
@AutoTrim
修饰对String
类型属性的Setter方法进行了AutoTrim操作:
package org.example;
public class Example {
private String username;
private String password;
private Integer type;
public Example() {
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public void setPassword(String password) {
this.password = password;
}
public void setType(Integer type) {
this.type = type;
}
}
- 编译前,可以配合Lombok一起使用
package org.example;
import com.supalle.autotrim.AutoTrim;
import lombok.Setter;
@Setter
@AutoTrim
public class Example {
private String username;
private String password;
private Integer type;
}
- 编译后,对类所有的有
String
类型形参的方法进行了AutoTrim操作,注意,包括非Setter的方法:
package org.example;
public class Example {
private String username;
private String password;
private Integer type;
public Example() {
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public void setType(Integer type) {
this.type = type;
}
}
- 编译前
package com.supalle;
import com.supalle.autotrim.AutoTrim;
import java.util.Objects;
@AutoTrim
public interface UserService {
default boolean login(String username, @AutoTrim.Ignored String password, Integer type) {
System.out.println(username);
System.out.println(password);
return Objects.equals(username, password);
}
}
- 编译后,对类所有的有
String
类型形参的方法进行了AutoTrim操作,@AutoTrim.Ignored
同样好使:
package com.supalle;
import java.util.Objects;
public interface UserService {
default boolean login(String username, String password, Integer type) {
username = username == null ? null : username.trim();
System.out.println(username);
System.out.println(password);
return Objects.equals(username, password);
}
}
- 原则1:越接近形参的注解,优先级越高;
- 原则2:同一注解对象上,
@AutoTrim.Ignored
优先级比@AutoTrim
高;
顺序:形参@AutoTrim.Ignored
> 形参@AutoTrim
> 方法@AutoTrim.Ignored
> 方法@AutoTrim
> 属性@AutoTrim.Ignored
>
属性@AutoTrim
> 类@AutoTrim.Ignored
> 类@AutoTrim
@AutoTrim
的本质是给方法所有的String
形参,在使用前合理的添加一段string == null ? null : string.trim()
代码。
- 感谢 Lombok 的代码开源,给予我参考。
AutoTrim定位为单一功能,轻包袱的工具,因此暂时不会加太多其他与之无关的功能,如有新的APT点子,会另开项目推出,因此更新的频率不会太频繁。
- 修复已知bug后,发布小版本;
- JDK发布新版本后,会第一时间支持新的JDK版本,然后发布大版本。