kotlinkotlinpoet

Looking for correct kotlinpoet syntax to initialize a PropertySpec.Builder for IntArrays and Array<String> values


Don't see the correct syntax for initializing the Array types to the values from an existing types from within the project, primitive types works fine and programmer defined classes

Target(
    AnnotationTarget.FIELD,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.VALUE_PARAMETER,
    AnnotationTarget.PROPERTY)
    @Retention(AnnotationRetention.SOURCE)

annotation class ConfigurableIntArray( val key: String, val text: String, val description: String, val initialValue: IntArray )

Class BlockAnnotationTester2(@field:ConfigurableIntArray(
    key = UIComponentSettings.MAXIMUM_LENGTH_KEY,
    text = UIComponentSettings.MAXIMUM_LENGTH_TEXT,
    description = UIComponentSettings.MAXIMUM_LENGTH_DESCRIPTION,
    initialValue = [2]) 
val unusedIntArray: IntArray = intArrayOf(1, 2, 3, 4),

 for (data in memberDataIntArray)...`
addProperty( 
                  PropertySpec.builder(TestAnnotationProcessor.INITIAL_VALUE, IntArray::class) 

                        .initializer("intArrayOf(${data.annotation.initialValue.asList()})")
                        .build()
                ) 
   object Member_BlockAnnotationTester2UnusedIntArray {

    const val KEY: String = 
    "configurable_block_detail_ui_component_maximum_length"

    const val TEXT: String = "Maximum length"

    var INITIAL_VALUE: IntArray = intArrayOf([2]) <-- issue 

const val DESCRIPTION: String = "Maximum length of the corresponding UI 
component"
}

Can only get it to initialize to static data in plain text literals or the first value


Solution

  • Guessing there is a simpler way but this worked

    .initializer("%L", buildCodeBlock {
                                    var lregex = 
        data.annotation.initialValue.asList().toString()
                                    lregex = lregex.replace("["," ")
                                    lregex = lregex.replace("]"," ")
                                    add("intArrayOf($lregex)")
                                })