Questions tagged [kotlinpoet]

A library from Square providing a Kotlin API for generating .kt source files.

A library from Square providing a Kotlin API for generating .kt source files. https://square.github.io/kotlinpoet/

72 questions
8
votes
2 answers

How to use KotlinPoet to get correct TypeName for PropertySpec

Using KotlinPoet, in order to generate a PropertySpec for adding properties to classes and constructors, you need a TypeName object. The TypeMirror.asTypeName() KotlinPoet function is deprecated, because it won't always work correctly for Kotlin…
M Dapp
  • 1,453
  • 16
  • 31
7
votes
1 answer

Generate data class with KotlinPoet

I want to generate simple data class with KotlinPoet: data class User(val id: Int) I do it like that: val builder = KotlinFile.builder("", "Foo") val classBuilder = TypeSpec.classBuilder("User").addModifiers(KModifier.DATA) val ctor =…
xap4o
  • 2,776
  • 2
  • 19
  • 13
7
votes
4 answers

KClass reference for nullable values

In Kotlin, when declaring getting a KClass for a type, such as String::class (which represents values whose type will be String), is there a syntax to indicate that the value is nullable (ie represente String? values instead of String). The context…
Antoine
  • 1,068
  • 10
  • 17
6
votes
2 answers

Kotlinpoet: Ommitting redundant `public` modifier from generated types and properties

Is there any way to omit the redundant public modifier from types and properties generated via KotlinPoet's TypeSpec.Builder and PropertySpec.Builder respectively?
m_katsifarakis
  • 1,777
  • 1
  • 21
  • 27
6
votes
1 answer

How to generate code with KotlinPoet when I am building my application? (Gradle)

I'm new using kotlinpoet and I've been reading the documentation and it seems like a great library, but I could not find an example to solve my problem. I have a dependency lib-domain-0.1.jar in which I have business objects for example: package…
TioCoding
  • 61
  • 3
4
votes
1 answer

Use file templates to generate Kotlin code

I'm building a tool to help my dev team to be faster when coding. For that, I thought on creating some kind on command line interface to generate code that's somewhat boilerplate. I found kotlinpoet, but I'm not sure if it's possible and how to use…
4
votes
1 answer

Android Annotation Code Generation - Android Classes

When starting to build my first code generation annotation, I've found I can't generate Android classes, such as SharedPreferences, since I start with a Java Library module in order to extend AbstractProcessor. I'm using kotlinpoet to generate my…
James B
  • 447
  • 3
  • 15
3
votes
0 answers

how to programatically mark generated source folder as Generated Sources Root

I am investigating generating Kotlin code with KotlinPoet i would like to be able to programatically mark the target directory as "Generated Sources Root" however i cannot see a KotlinPoet option for this. is it possible to mark my target directory…
Hector
  • 4,016
  • 21
  • 112
  • 211
3
votes
0 answers

Create a TypeSpec from an existing file with kotlinpoet

I need to make a copy of the class marked with my annotation, and then add my own code to it. Class marked with my annotation: @MyAnnotation class Test { private name = "Ann" fun method1() { println("Test") } } Generated…
DJ. XYZ
  • 99
  • 1
  • 9
3
votes
0 answers

How to get TypeName with correct nullable

I try to call this.asType().asTypeName().isNullable isNullable - does not work correct. I found a solution, but it does not look well: fun Element.getTypeNameWithRealNullable(): TypeName { val annotation =…
Vova
  • 956
  • 8
  • 22
3
votes
1 answer

With KotlinPoet or similar; generating a non-raw Kotlin string literal

Straightforward; is there a way to generate a valid Kotlin string literal (non-raw, as in non-triple-quote) from a string; I'm currently trying to accomplish this with KotlinPoet. For clarity sake, example input: Hello, how are you? I'm doing…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
3
votes
1 answer

How do I generate a class which extends a class which implements a generic interface using kotlinpoet

Given an interface like interface Builder { fun build() : R } How do I generate a class BooBuilder which implements this interface using kotlinpoet. I couldn't find an example on creating a generic interface (or class) in the…
Lukasz
  • 2,257
  • 3
  • 26
  • 44
3
votes
2 answers

How to get the type of an instance of javax.lang.model.element.Element

I'm following Hello World of Annotation Processing in Kotlin and KotlinPoet's documentation and am trying to implement a generic builder for Kotlin. I'd like to generically create a method for every field in an annotated data class and give it's…
uzilan
  • 2,554
  • 2
  • 31
  • 46
3
votes
1 answer

How to dynamically define a code block with parameters

I am stuck on this feature, below is the expected code to be generated, and the total number of parameters is not a fix number, there might be 2, or 3 or more. val instance: InstanceType = Instance(parameter1, parameter2) this is within one…
Jiachuan Li
  • 195
  • 9
3
votes
1 answer

How to generate a class with a custom interface via kotlinpoet

I want to generate a kotlin class definition, this class implements an customized interface, the target class definition as below: data class TemplateState(val data: String) : ContractState { } I used below poet code to generate it except the…
Jiachuan Li
  • 195
  • 9
1
2 3 4 5