Skip to content
Advertisement

How to wrap @Column annotation with my own annotation in Java or Kotlin

I simply want to have my own annotation to clean up the annotation mass and to be able to change them easily when I want;

JavaScript

Wish I could

JavaScript

nullable = false, length = 32 are the default params.

Java or Kotlin solutions are welcome.

Advertisement

Answer

Since you’re using 3-rd party annotations imported from javax the best option is to introduce a composite annotation. (Kotlin doesn’t support annotation inheritance.

JavaScript

Spring boot is doing a pretty good job combining tons of config annotations all together – check it out.

There is a problem with composite annotation Anno, tho. You have to supply annotation parameters with constant values.

If you’re sure, you need a parametrised annotation like

JavaScript

have a look at Kapt or Kotlin Compiler plugins, you will need a piece of code generation.

With Kapt or Kotlin compiler plugin you will need just to override a newField method of your custom ClassBuilder:

JavaScript

And then register it with

JavaScript

It should be relatively easy to integrate this processing into an existing gradle or maven project, or just pass to kotlinc.

Advertisement