What do these annotations mean in this Kotlin code in android?
#SuppressLint("SimpleDateFormat")
fun convertLongToDateString(systemTime: Long): String {
return SimpleDateFormat("EEEE MMM-dd-yyyy' Time: 'HH:mm")
.format(systemTime).toString()
}
#Entity(tablename = "daily_sleep_quality_table")
data class SleepNight(...)
....
....
check this
#
- introduces an annotation
- introduces or references a loop label
- introduces or references a lambda label
- references a 'this' expression from an outer scope
- references an outer superclass
# is a Java annotation, which also supported in Kotlin.
#SuppressLint("SimpleDateFormat")
#SuppressLint is an annotation used by the Android Lint tool. Lint will tell you whenever something in your code isn't optimal or may crash. By passing "SimpleDateFormat" there, you're suppressing all warnings that would tell you if you're using SimpleDateFormat in a wrong way.
#Entity(tablename = "daily_sleep_quality_table")
#Entity is annotation used by SQLite to marks a class as an Entity. If your using that in your class, SQLite will identify your class as an Entity with the specified table name.
Those are Java annotations which kotlin supports
Java annotations are 100% compatible with Kotlin
In your example #Entity
Specifies that the class is an entity. This annotation is applied to the entity class
Related
I'm trying to use a room entity with a value class:
#JvmInline
value class UserToken(val token: String)
and the entity:
#Entity(tableName = TABLE_AUTH_TOKEN)
data class TokenEntity(
#PrimaryKey val id: Int = 0,
val token: UserToken
)
I get the following error:
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class TokenEntity {
^
is it even possible to use room with value class? I couldn't find anything about this. thanks
See the comment from #CommonsWare. Android does not yet support value classes for Room.
The same holds true for the value classes introduced in kotlin 1.5. The type is not supported.
— Support Inline class in Room entity
Here is a possible explanation according to Kotlin Inline Classes in an Android World.
Looking to solve this you could try and add a TypeConverter for your Inline class, but since your Inline class is just the value it wraps when it’s compiled, this doesn’t make much sense and it doesn’t work as you’d expect even if you tried...
I’m just guessing it’s because this is a TypeConverter converting UserId to Int which is basically the same as Int to Int 😭. Someone will probably solve this problem, but if you have to create a TypeConverter for your Inline class then you are still plus one class for the count (multidex). 👎
I think yes if you can provide a type converter for it to change it to some sort of primitive data type (int , string, long ...etc) when it needs to be stored, and to change it back to its class type when it's fetched from database.
You can read about Type Converters from here
Referencing complex data using Room
other than that, your other class should be an entity and bind both your entities together using a Relation.
at least that's what I know about how to use Room.
UserToken always will have only one attribute? In this case, you don't need two classes, just use token: String directly on your entity class;
If you really need keep this class, you have two options:
TypeConverter, where you basically will convert the object into a json, and save as string in the database;
Relation, where you will transform the UserToken in a entity, and on TokenEntity save the tokenId.
I am currently creating a library for private use which provides an Annotation (lets call it #Something) which can be used on Properties of a data class. I created the annotation like so:
#Retention(AnnotationRetention.RUNTIME)
#Target(AnnotationTarget.PROPERTY)
annotation class Something
It will be used like this
data class SomeDataClass (val param1: String, #Something val param2: String, val param3: String)
I then want to be able to access the following things on the class that contains properties with this annotation:
memberFunctions (to get the copy constructor)
parameters of the copy constructor
memberProperties
property annotations
property name
List item
Now everything works properly until I minify my library with ProGuard. As I understand it, I need to describe my ProGuard rules in consumer-rules.pro. But what exactly do I need to add here? I want every data class in the application that uses my library that has a property annotated with #Something to be kept so I can read the things above from it.
I am currently integrating Firestore documents within a Kotlin application using an annotated POJO to pull the data in. As part of this I also need the ID of the document.
I'm trying to do this using the new #DocumentId notation described here...
However the annotation isn't being recognised in my POJO class (unresolved reference: DocumentId)
In my gradle file I have...
implementation 'com.google.firebase:firebase-firestore:21.2.1'
In my model class I have...
data class DocumentModel(
#DocumentId var id: String
#PropertyName var field1: String
...etc...
)
The other annotations work fine, it's just the DocumentId one which isn't working. Anyone have any ideas on what I'm doing wrong?
According to official document (https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/DocumentId.html),
This annotation is applied to a property that is not writable (for example, a Java Bean getter without a backing field).
So, you have to use val instead of var.
data class DocumentModel(
#DocumentId val id: String
...etc...
)
Had anyone success with using Room over Android multi-module Kotlin setup.
#Entity
data class School(#Embedded val student: Student)
data class Student(val age: Int = 0)
Whenever I have both above classes in main module everything compiles properly.
But if I move the Student class to another android library module and School in main module. It throws compile time error as:
error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
Tried the following constructors but they failed to match:
Student(int) : [arg0 : null]
Note: On debug found this might be name mangling issue. If I change the Student class to data class Student(val arg0: Int = 0) it compiles fine.
Looks like at compile time age exposed as arg0
Any idea how to resolve this issue?
I had the same issue. Entities and Room DAO placed in same module everything ok, putting entities in separate module break compilation. In my case the problem was putting enum declaration in same file as entity. Declaring enum in separate file solved the problem.
I asked:
when geting objects from a Json with restlet how can I get different objects in case the Json chnges its structure
So I am implementing an abstract class with 2 sub classes as suggested in:
Answer
I Followed the documentation in jackson annotations: jackson-annotations and Annotation Type JsonTypeInfo
But I don't know how to: // Include Java class name ("com.myempl.ImplClass") as JSON property "class"
on the abstract class:
#JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="class")
I get errors:
Id cannot be resolved to a variable
As cannot be resolved to a variable
And in the subclasses:
#JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
#JsonSubTypes({com.smartenit.client.business.rest.CapabilityWhenReadingAllDevices.class,
com.smartenit.client.business.rest.CapabilityWhenRequestingAttribute.class})
I get errors:
Id cannot be resolved to a variable
As cannot be resolved to a variable
The value for annotation attribute JsonSubTypes.value must be some #com.fasterxml.jackson.annotation.JsonSubTypes.Type annotation
I think the Jackson API has evolved.
I've tried with the sample annotations (located on the abstract class) with restlet 2.2 (leveraging jackson 2.2):
#JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = As.PROPERTY, property = "#class")
#JsonSubTypes({ #Type(value = Impl1.class, name = "impl1"),
#Type(value = Impl2.class, name = "impl2") })
Could you give it a try?