我已经Lombok在我的代码中使用自动生成Getter和Setter编码,现在我想添加其他个人Annotations并使用它.
例如,我想添加@Exist验证现有密钥列表:
@Getter @Setter
public class User {
private String name;
private List<Integer> keys;
public boolean existKeys(Integer key) {
boolean exist = keys.contains(key);
return exist;
}
}
Run Code Online (Sandbox Code Playgroud)
在创建Annotation之后,我将只需执行以下操作:
@Getter @Setter
public class User {
private String name;
@Exist
private List<Integer> keys;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码.我想改变hello类的say方法.我用javassist.我有以下错误.
public class TestJavasisit {
/**
* @param args the command line arguments
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception {
ClassPool pool = ClassPool.getDefault();
// version original
Hello h1 = new Hello();
h1.say();
CtClass cc = pool.get("testjavasisit.Hello");
cc.defrost();
CtMethod m = cc.getDeclaredMethod("say");
m.insertBefore("{ System.out.println(\"Hello.say():\"); }");
cc.writeFile(".");
cc.toClass();
// version modifie
Hello h2 = new Hello();
h2.say();
}
}
Run Code Online (Sandbox Code Playgroud)
你好类:
public class Hello {
public void say() {
System.out.println("Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息:
run:
Hello
Exception in …Run Code Online (Sandbox Code Playgroud)