将代码插入方法 - Java
有没有办法自动将代码插入到方法中?
我有以下带有 getter 和 setter 的典型字段,我想将指示的代码插入到记录该字段是否被修改的 setter 方法中,以及插入指示的“isFirstNameModified”字段来跟踪该字段是否被修改或不是。
public class Person {
Set<String> updatedFields = new LinkedHashSet<String>();
String firstName;
public String getFirstName(){
return firstName;
}
boolean isFirstNameChanged = false; // This code is inserted later
public void setFirstName(String firstName){
if( !isFirstNameChanged ){ // This code is inserted later
isFirstNameChanged = true; // This code is inserted later
updatedFields.add("firstName"); // This code is inserted later
} // This code is inserted later
this.firstName = firstName;
}
}
我也不确定是否可以将方法名称的子集作为方法本身内部的字符串,如我将 fieldName 作为字符串添加到更新字段集中的行所示: updatedFields.add( “名字”);
。而且我不确定如何将字段插入到类中,在该类中添加布尔字段,该字段跟踪该字段之前是否已被修改(为了提高效率,防止必须操作 Set):boolean isFirstNameChanged = false;
似乎最明显的答案是在 eclipse 中使用代码模板,但我担心稍后必须返回并更改代码。
编辑:::::::::
我应该使用这个更简单的代码而不是上面的示例。它所做的只是将字段名称作为字符串添加到集合中。
public class Person {
Set<String> updatedFields = new LinkedHashSet<String>();
String firstName;
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
updatedFields.add("firstName"); // This code is inserted later
this.firstName = firstName;
}
}
Is there a way to automatically insert code into a method?
I have the following typical field with a getter and setter and I would like to insert the indicated code into the setter method that records if the field was modified as well to insert the indicated "isFirstNameModified" field to also track if the field was modified or not.
public class Person {
Set<String> updatedFields = new LinkedHashSet<String>();
String firstName;
public String getFirstName(){
return firstName;
}
boolean isFirstNameChanged = false; // This code is inserted later
public void setFirstName(String firstName){
if( !isFirstNameChanged ){ // This code is inserted later
isFirstNameChanged = true; // This code is inserted later
updatedFields.add("firstName"); // This code is inserted later
} // This code is inserted later
this.firstName = firstName;
}
}
I'm also not sure if I can the subset of the method name as a string from inside the method itself as indicated on the line where I add the fieldName as a string into the set of updated fields: updatedFields.add("firstName");
. And I'm not sure how to insert fields into a class where I add the boolean field that tracks if the field has been modified or not before (for efficiency to prevent having to manipulate the Set): boolean isFirstNameChanged = false;
It seems to most obvious answer to this would be to use code templates inside eclipse, but I'm concerned about having to go back and change the code later.
Edit:::::::::
I Should have used this simpler code instead of the example above. All it does is add the name of the field as a string to a set.
public class Person {
Set<String> updatedFields = new LinkedHashSet<String>();
String firstName;
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
updatedFields.add("firstName"); // This code is inserted later
this.firstName = firstName;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,可以,一种方法是使用某种形式的字节码操作(例如 javassist、ASM、BCEL)或更高级别的 AOP 库位于以下之一之上这些工具,例如AspectJ、JBoss AOP。
注意:大多数 JDO 库都这样做是为了处理持久性。
这是使用 javassist 的示例:
Yes you can, one approach is to use some form of byte code manipulation (e.g. javassist, ASM, BCEL) or a higher level AOP library sit on top of one of these tools, e.g. AspectJ, JBoss AOP.
Note: most JDO libraries do this for handling persistence.
Here is an example using javassist:
使用 AspectJ,您可以根据建议修改方法和字段。
我的示例是使用
@AspectJ
语法编写的,它会在编译时或加载时修改代码。如果你想在运行时修改,你可以使用Spring AOP,它也支持@AspectJ语法。具有简单 Person 类和存根存储库的示例。有关更新哪些字段的所有信息均由名为 SetterAspect 的方面处理。它监视写入字段时更新哪些字段。
此示例中的其他建议是围绕存储库中的更新方法。这是检索从第一方面收集的数据。
Person 类:
存根存储库:
使用 AspectJ 在 Person 类中执行
main()
方法的输出:注意的是,main() 方法调用的是
DtoRepository.update(T t)
,但是DtoRepository.update(T t, SetUpdatedFields)
code> 由于存储库方面的周围建议而被执行。监视演示包中对私有字段的所有写入的方面:
存储库方面:
最后,方面使用的两个帮助器类:
和
我的示例使用方面执行所有更新逻辑。如果所有 DTO 都实现了返回
Set
的接口,则可以避免最后一个方面。我希望这能回答您的问题!
With AspectJ you can modify methods and fields with advises.
My example is written with
@AspectJ
syntax which modifies the code at compile-time or load-time. If you want the modification at runtime, you can use Spring AOP which also supports this@AspectJ
syntax.An example with a simple Person class and a stub repository. All information about which fields are updated is handled by an aspect called SetterAspect. It monitors which fields that are updated when the fields is written to.
The other advice in this example is around the update method in the repository. This is to retrieve the data collected from the first aspect.
The Person class:
The stub repository:
The output for executing the
main()
method in the Person class with AspectJ:Important to note here is that the main() method calls on the
DtoRepository.update(T t)
but theDtoRepository.update(T t, Set<String> updatedFields)
gets executed because of the around advice in the repository aspect.The aspect that monitors all writing to private fields in the demo package:
The repository aspect:
Finally, the two helper classes used by the aspects:
and
My example does all the update logic with aspects. If all the DTOs implemented an interface that returns a
Set<String>
, you could have avoided the last aspect.I hope this answered your question!
您可以使用动态代理类并获取事件在调用
setFirstName
和其他方法set...
之前,通过method.substring(3)
=>确定字段名称; “FirstName”,并将其放入setFirstName
中。You can use Dynamic Proxy Classes and get the event before the call of
setFirstName
and other methodsset...
, determine field name withmethod.substring(3)
=> "FirstName", And put it insetFirstName
.