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
1
vote
3 answers

Kotlin Reflection: How to know if a Kotlin class is marked with "internal" visibility modifier

I'm autogenerating code with KotlinPoet and Auto Service. I didn't find any way to know if an annotated class has "internal" modifier so I can create another class with same modifier. For example: @MyAnnotation internal class Car So I thought using…
Juan Saravia
  • 7,661
  • 6
  • 29
  • 41
1
vote
1 answer

How to return function in kotlin poet

I'm using KotlinPoet for annotation processing, but I've got an issue. I need return function in generated class, but I'm not sure that KotlinPoet can do it? If you have some experience, please help me. That I can do: fun test(): String =…
Vanya Sakharovskiy
  • 295
  • 1
  • 3
  • 16
1
vote
0 answers

How to use FunSpec.overriding? in KotlinPoet

Submitted by Fleshgrinder on GitHub. The FunSpec class has the very handy overriding method, however, it is unclear how this can be used while generating code. Minimal example: FileSpec.builder("com.fleshgrinder", "KotlinPoet").apply { val…
Egor
  • 39,695
  • 10
  • 113
  • 130
1
vote
1 answer

How should I define these complex initializer for a property

Although I checked all tests in the kotlinpoet code, but I didn't find a proper way to implement below target codes, or I am not sure whether I used the best approach to do that. If anyone can provide some comments about this, that would be highly…
Jiachuan Li
  • 195
  • 9
1
vote
2 answers

How do I generate a constructor parameter of the Kotlin "Unit" type with a single type parameter with kotlinpoet?

This might be a bit too specific for posting here, but I'm trying to generate a class like this with kotlinpoet: class Query(val onSuccess: (E) -> Unit, val onError: (Int, String) -> Unit = { i, m -> }) How would I create that…
Preston Garno
  • 1,175
  • 10
  • 33
1
vote
2 answers

Kotlin Poet empty constructor generation - inheritance

I'm trying to print a call to superclass' constructor using KotlinPoet. My current (not compiling) output is: open class NameOfASubclass : NameOfSuperclass { } In order to make my code compile I need to (somehow) either print open class…
hhg
  • 649
  • 1
  • 11
  • 20
0
votes
0 answers

kotlinpoet generate code to call builder class continuously

Now i can generate code like this val builder = MyBuilder() builder.setA() builder.setB() builder.setC() val target = builder.build() I want to generate code like under val target = MyBuilder() .setA() .setB() .setC() .build()
Jade
  • 416
  • 5
  • 7
0
votes
1 answer

IntelliJ IDEA Plugin - How to modify the contents of an existing file using Kotlin poet

I'm writing an Intellij Plugin. I want to retrieve a file from the project and add a new function to the class using Kotlin poet .
Anup Ammanavar
  • 432
  • 1
  • 3
  • 16
0
votes
0 answers

Android Studio redeclaration error when compiling release while having Kotlin Poet generated classes

My app uses Kotlin Poet to generate some classes at compile time. These classes are generated in app/build/generated/source/kaptKotlin [main] and there in two subfolders debug."packagename" and release [main]/"packagename", depending on if I compile…
Michael Konz
  • 503
  • 1
  • 3
  • 20
0
votes
0 answers

How to create .kt file in generated directory and reference file within project/app?

I am generating a class using KotlinPoet and i can write to the generated directory, however I cannot use the class within the project. Is there something I need to add to build.gradle.kts file?
babybadger
  • 101
  • 3
0
votes
0 answers

How to add a function definition available in a string to a class generated from kotlinpoet TypeSpec?

I am generating a class using kotlinpoet and one of the method to be generated is available to me as string (full function definition), how will I add that to a TypeSpec? // if I have function definition like this val funDefinition = "fun…
Renjith
  • 397
  • 5
  • 14
0
votes
1 answer

How to get KClass> type from List in kotlin

I'm having problem with Kotlin poet PropertySpec object. Because i can't provide List to the PropertySpec builder. Help please. PropertySpec.builder("services", List::class.asTypeName()).build()
0
votes
1 answer

adding star projection in kotlin poet

I am using KOTLIN Poet to generated some code. I am stuck with adding star projection for a parameterized type. Could not find anything of adding star projection in KOTLIN Poet docs. Is there a way to add star projection in KOTLIN Poet.
0
votes
1 answer

Kotlin Annotation processor doesn't add import for generated files

I have an Annotation-processor, which should generate a class MyGeneratedClass containing a variable of another class MyEntity. My code inside the processfunction: val elementsWithAnnotation =…
0
votes
1 answer

KotlinPoet: how to generate a DSL with parameters

I'm trying to generate this Kotlin code that contains a DSL with a parameter: listOf( navArgument(QUERY_PARAM) { type = NavType.StringType nullable = true defaultValue = null }, ) Is there a better way to provide the…
Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69