I want to make sure all getters of the classes in a certain package follow a given template.
For example, all getters must be of the form:
XXX getYYY(){
classLock.lock();
return YYY;
finally{
classLock.unlock();
}
}
Basically, I want that my project will not compile/run unless all getters are of that form.
What is the best way to do that? I would prefer a solution that can be used as an Eclipse plugin.
There is no way to force the compiler to do that check. You might want to look into static code analysis tools such as PMD or CheckStyle and define your own rule for this.
Then call that tool during your build cycle and make it a fatal error if that check fails on any class.
But I'm not sure that that kind of getter would provide any level of thread safety to your code. You would still be able to read inconsistent values when you call two getters in a row.