Is there a way to add an annotation to a class def...
# announcements
s
Is there a way to add an annotation to a class defined in a separate library? Some kind of extension annotation? E.g. I have a class called
data class Foo()
in my network library, but I would like to add an annotation to it in my Android
app
module for use with my database.
g
I dont think this is within kotlin's power, this would need changes in the jvm-spec. You can probably get away with classpath games though, compile with the annotation and omit the jar containing it as a runtime dep.
b
If you are about room - there are few things you can do: 1. Use room-common dep in your network lib (it can be pure java lib). Then just add this dataclass to your DB 2. Use
@Embedded
for entity declaration - you can just write
data class FooEntity(@Embedded val foo: Foo)
only for schema generation, then you will be able to use Foo in all adapters (one note though - you will need to add targetEntity for
@Insert
)
s
Never thought of that second option. That should work nicely. Thank you! Was really tired of declaring my data classes twice.
Will that work if the data class I am `@Embedded`ing also has nested data classes?
I don't think it will, alas.. I will try the first option I suppose
b
It depends on how are you planning to store these nested data classes - if in a single column, you can use type adapter for these classes