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;
}
}
With AspectJ you can modify methods and fields with advises.
My example is written with
@AspectJsyntax 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@AspectJsyntax.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!