For example, I have a class with a public field and corresponding getter/setters:
public class User {
public String name;
public String getName() { return name; }
public void setName(String name) { this.name = name;}
}
Now invoke the field name:
User user = new User();
user.name = "test";
System.out.println(user.name);
Is it able to use aspectj to enhance the class to let the bytecode be:
User user = new User();
user.setName("test");
System.out.println(user.getName());
I know javassist can do this, but can AspectJ do the same?
Check out the pointcut docs and the
get(FieldPattern)andset(FieldPattern)pointcuts.There is some additional writeup in the same doc.