I have got a class with approximately 40 member-variables. Now I want to create getters and setters for them (they need to fire a kind of PropertyChangeEvent and getters aren't the default ones too; but the code is practically the same for each variable)
Instead of just creating the 80 Functions and (sort of) copy-paste everything, I'm asking if it is possible to do that via (static) metaprogramming (or something different that I do not know about).
I've done a lot with ruby before where I'd roughly just write sthg like:
instance_variables.each do |var|
define_method "get_#{var}" do
return var if var.cached?
return var = super
end
# ...
end
I know that Java and Kotlin are completely different since they are compiled and statically typed, but since it is really useful and similar things are possible in C (Preprocessor), C++ and even VHDL (for generate), I can't believe that this isn't possible easily.
I've read that Annotations are the point to start in java, but I also read, that one can (normally) not change existing classes with them.
I've read about Lombok, where you can annotate fields to generate normal Getters & Setters for them, hashCode & equals functions and lots of other things, but the devs point out, that this is a lot of hackery with private compiler APIs which can break at any time.
That's not really what I want in the first place (as my IDE is intelligent enough to just create them, without needing such tricks).
The boilerplate code is nasty and waters the code, but copy pasting real code is something I hate, thus the question.
I have to admit, that I don't know how that looks for Kotlin, but have heared that Extensions should be the entry point there. I'm open for that too, if that is easier (or at least possible).