Using KotlinPoet, how would I create the Parameter...
# squarelibraries
d
Using KotlinPoet, how would I create the ParameterSpec of class B in the following example?
Copy code
abstract class A(
    open var field1: String
)

abstract class B(
    override var field1 : String
) : A(field1)
I tried to also create a corresponding PropertySpec for my class, but as a result I get the following:
Copy code
public abstract class B(
  override field1: String
) : A() {
  public override var field1: String
}
e
Copy code
TypeSpec.classBuilder("B")
    .addModifiers(KModifier.ABSTRACT)
    .primaryConstructor(FunSpec.constructorBuilder()
        .addParameter("field1", STRING)
        .build())
    .superclass(ClassName("example", "A"))
    .addSuperclassConstructorParameter("field1")
    .addProperty(PropertySpec.builder("field1", STRING, KModifier.OVERRIDE)
        .mutable()
        .initializer("field1")
        .build())
    .build()
but you don't need the property override; take it out and it generates
Copy code
public abstract class B(
  field1: String
) : A(field1)
which still has a
var field1: String
d
Thank you @ephemient I had something very similar, but it's missing the
override var
part, which is needed since I want to add the Room annotation "`ColumnInfo`" Following this issue: https://github.com/square/kotlinpoet/issues/113 tells me that I need to add a property to my class, but even then, I don't get a
var
or
val
I'm currently generating a test class with:
Copy code
classBuilder.superclass(classDeclaration.buildClassName())
classBuilder.primaryConstructor(FunSpec.constructorBuilder()
    .addParameter(ParameterSpec.builder("cheese", String::class)
        .addModifiers(KModifier.OVERRIDE)
        .build())
    .build())
classBuilder.addSuperclassConstructorParameter("cheese")

classBuilder.addProperty(PropertySpec.builder("cheese", String::class)
    .addModifiers(KModifier.OVERRIDE)
    .mutable()
    .build())
And tried it with and without the property
e
you're adding the property "cheese" twice, once with mutable and once without
d
Yes, once as a normal property and one as the constructor parameter. Even when I comment that part out (PropertySpec), the generated source is still missing the
var
part. The PropertySpec is only there because in the issue it is stated that that one is neccesary for kotlinpoet to create the
var
or
val
at ParameterSpec level
e
oh I misread, the override there is confusing (it doesn't make sense on a parameter)
d
Sry, i quite new to kotlin, so any help is appreciated
e
with your code there (removing the override on the parameter and leaving it on the property only), it should generate
Copy code
public class A(
  cheese: String
) : A(cheese) {
  public override var cheese: String
}
which is valid
🎉 1
d
Thought that was the solution, but it still does not create a
var
. So now I'm back at my first problem... Since it doesn't has a `var`or is overriden I cannot add my annotion
Ah, after examaning the KotlinPoet code, it appears to be relativly easy. The ProperySpec simply needs the
initializer
to be set to the
ParameterSpec
name
Copy code
classBuilder.superclass(classDeclaration.buildClassName())
classBuilder
    .primaryConstructor(FunSpec.constructorBuilder()
        .addParameter(ParameterSpec.builder("userName2", String::class)
            .addModifiers(KModifier.OVERRIDE)
            .addAnnotation(AnnotationSpec.builder(ColumnInfo::class).build())
            .build())
        .build())
classBuilder.addSuperclassConstructorParameter("userName2")

classBuilder.addProperty(PropertySpec.builder("userName2", String::class)
    .addModifiers(KModifier.OVERRIDE)
    .initializer("userName2")
    .mutable()
    .build())
119 Views