Add annotation in JVM module on a class imported from common module


#1

I want to define data classes in a common module so that I can reuse them in many multiplatform modules. I need to apply few annotations on some of those classes in the JVM backend module (Spring backend, @entity annotation an so on) but of course not in the other modules. Is there a practical way to do so? The only solution found is this guide that I’m not able to translate in Kotlin. Any idea how?


#2

Whatever you doing I think added platform-specific annotation to cross-platform code is not a good idea.

First come to mind: did you think of using delegation?


#3

Indeed it is not and that’s not what I am trying to accomplish. Let’s make an example to more easily understand what i need.

I have a class User that is needed by client and back-end, now the back-end module needs to put the annotation @Entity to it while the client module does’t (and shouldn’t I’d say). I was asking if there is a way to accomplish just that, but I assume you just can’t. If there is another approach that can solve this issue while reusing some code it is welcome.

I’ve read the delegation part and I’m not sure how it could help me. Can you elaborate a little? I’m not really familiar with delegation and multi-platform-projects together :sweat:


#4

I thought something like:

  1. have a cross-platform class without annotation
  2. on JVM side have the class delegating to common class with annotation

This may not work as annotation may not work correctly with delegation. At least I wouldn’t be surprised if it wouldn’t.

Alternative:

  1. make a cross-platform class open
  2. on JVM side have the child class to common class with annotation

It may require additional flag/annotation to make sure the annotation applied to the parent classes.

Sorry, I don’t have the project to test this. Just try to offer solutions I would try facing this problem.

Good luck.


#5

This approach would mean more boilerplate code when eliminating it is the key point of using a common module. I think i will use external XML JPA definitions those class entities. Still a little boilerplate but at least i won’t have to modify those classes in the clients.

Thank you tho :slight_smile:


#6

You can redeclare JPA annotations in a common module as expect annotations and implement them as actual typealias'es to the real JPA annotations in a JVM module.
Then in order not to write dummy annotations in other platforms, you can mark these expect declarations as @OptionalExpectation.


#7

Thank you for the answer :slight_smile: It was exactly what i was looking for!
I’ve managed to implement it as you recommended but it feels kinda clunky. The fact that I am declaring platform specific annotations in a common module feels wrong.

I think the right way to do this would consist in adding the platform specific annotation from the specific module to the common class, without modifying the common class. I assume right now it’s not possible.