Compiler warning when using @Value annotation in Kotlin project

Is there some type of Kotlin language I could use on late initialization instead of java.lang.Integer

so I don't get a compiler warning?

Let's say I have a class like this:

class SomeClass {
 @Value(#{config['poolCapacity']?:'5000'})
 lateinit var somePool: Integer
}

      

I can't use a type Int

from Kotlin because it's a primitive type and lazeint

don't accept it.

If I stick java.lang.Integer

with it, it works fine, but I get a compiler warning like this:

SomeClass.kt: (20, 24): This class should not be used in Kotlin. using kotlin.Int instead.

Obviously I could create the required type myself, but I'm just wondering if there is something out of the box and recommended to use in such a situation and I just can't find it? (Annotated constructor is not a solution in this particular case.)

+3


source to share


1 answer


The simplest solution is not to use the late-initialized property, since the last Kotlin initialization property no longer supports primitive types and you can initialize it with the default value of the spring expression, e.g .:

@Value(#{config['poolCapacity']?:'5000'})
var somePool: Int = 5000

      

In a complex example, you can write delegated properties, but you must be annotated in setter

at @set

site-target, not field

/ property

, for example:



@set:Value(#{config['poolCapacity']?:'5000'})
var value by required<Int>()

      


inline fun <reified T> required(): ReadWriteProperty<Any, T> {
    return object : ReadWriteProperty<Any, T> {
        var value: T? = null;
        override fun getValue(thisRef: Any, property: KProperty<*>): T = value as T

        override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
            this.value = value;
        }

    }
}

      

+4


source







All Articles