我提供了一个注释@validateName
,任何人都可以在他们的代码中包含它。假设有人编码
class Person {
@validateName
private String name;
....
}
然后调用NameValidator.validate(personObject)
或其他类似的方法来验证字段。
我想确保名称字段始终处于有效状态,即我想在任何有注释的变量变化时自动调用validate()
方法(无论它在类内部还是外部发生变化)。
我愿意编写一个挂钩到Eclipse并在编译阶段被调用的插件。请提供一些指示,我可以开始寻找解决方案。
(我想我必须实现某种AOP,或者应该使用BCEL或其他东西修改字节码。我不确定,因为我没有尝试过。
使用AspectJ您可以这样做:
注释
package de.scrum_master.aop.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateName {}
package de.scrum_master.aop.app;
public class Application {
private int id;
@ValidateName
private String firstName;
@ValidateName
private String lastName;
private String placeOfBirth;
public Application(int id, String firstName, String lastName, String placeOfBirth) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.placeOfBirth = placeOfBirth;
}
@Override
public String toString() {
return "Application [id=" + id + ", firstName=" + firstName
+ ", lastName=" + lastName + ", placeOfBirth=" + placeOfBirth
+ "]";
}
public static void main(String[] args) {
System.out.println(new Application(1, "Galileo", "Galilei", "Pisa, Italy"));
System.out.println(new Application(2, "Isaac", "Newton", "Woolsthorpe-by-Colsterworth, United Kingdom"));
System.out.println(new Application(3, "Albert", "Einstein", "Ulm, Germany"));
System.out.println(new Application(4, "Werner", "Heisenberg", "Würzburg, Germany"));
}
}
<<p> 验证器方面/strong> package de.scrum_master.aop.aspect;
import java.util.Random;
import de.scrum_master.aop.app.ValidateName;
public aspect NameValidator {
void validate(String name) {
if (new Random().nextBoolean())
throw new RuntimeException("Invalid name " + name);
}
void around(String name) : set(@ValidateName * *.*) && args(name) {
//System.out.println(thisJoinPointStaticPart);
System.out.print("Validating name " + name);
try {
validate(name);
System.out.println(" -> OK");
proceed(name);
}
catch (Exception e) {
name = name.toUpperCase();
System.out.println(" -> " + e.getMessage() + " -> replaced by " + name);
proceed(name);
}
}
}
正如您所看到的,我的验证器基于伪随机值在大约50%的情况下随机失败。当它这样做时,它只是用大写版本替换"无效"名称。输出看起来像下面的一个变体:
Validating name Galileo -> OK
Validating name Galilei -> Invalid name Galilei -> replaced by GALILEI
Application [id=1, firstName=Galileo, lastName=GALILEI, placeOfBirth=Pisa, Italy]
Validating name Isaac -> Invalid name Isaac -> replaced by ISAAC
Validating name Newton -> Invalid name Newton -> replaced by NEWTON
Application [id=2, firstName=ISAAC, lastName=NEWTON, placeOfBirth=Woolsthorpe-by-Colsterworth, United Kingdom]
Validating name Albert -> OK
Validating name Einstein -> Invalid name Einstein -> replaced by EINSTEIN
Application [id=3, firstName=Albert, lastName=EINSTEIN, placeOfBirth=Ulm, Germany]
Validating name Werner -> OK
Validating name Heisenberg -> Invalid name Heisenberg -> replaced by HEISENBERG
Application [id=4, firstName=Werner, lastName=HEISENBERG, placeOfBirth=Würzburg, Germany]