In Scala it's possible to use the annotation @BeanProperty to automatically generate getters and setters to a field.
How can I create my own annotation that behaves like that?
I'm interested in creating annotations that changes the source code, like @BeanProperty does.
As an example, how could I create an annotation that only generated get methods?
Thanks.
When creating custom annotations with methods, we should be aware that these methods must have no parameters, and cannot throw an exception. Also, the return types are restricted to primitives, String, Class, enums, annotations, and arrays of these types, and the default value cannot be null.
Two words: compiler plugin. It is not easy, and the documentation is sparse, but that is one way to do it.
Future versions of Scala will likely have macro support, which gives you much of the same flexability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With