Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom annotations like BeanProperty

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.

like image 630
Vinicius Seufitele Avatar asked Apr 07 '12 18:04

Vinicius Seufitele


People also ask

Is it possible to create custom annotations?

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.


1 Answers

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.

like image 89
leedm777 Avatar answered Oct 05 '22 02:10

leedm777