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
2
votes
1 answer

How to implement the suspend keyword to modify higher-order functions in kotlin poet

I want to modify higher-order functions with the ”suspend“ keyword. The following is what I want: public fun login(io: suspend () -> ResLogin): Unit {} I tried to use: val lambdaTypeName = LambdaTypeName.get(returnType = responseBean) But get the…
2
votes
0 answers

"File name too long" when generating a class using Kotlinpoet

I'm trying to generate a class structure by parsing a json file which ends up in many anonymous nested classes. I then save the file in my project build folder and configure gradle to add its path as a source folder. However when I try to…
uzilan
  • 2,554
  • 2
  • 31
  • 46
2
votes
1 answer

KotlinPoet - No brackets for interface methods

I'm generating interfaces with KotlinPoet with the following code val funspec = FunSpec.builder("test").build() val interfacespec = TypeSpec.interfaceBuilder("Test").addFunction(funspec).build() This generates the following code: interface Test { …
m0skit0
  • 25,268
  • 11
  • 79
  • 127
2
votes
1 answer

AbstractProcessor not generating class

I'm building a view binder that uses KotlinPoet to generate some boilerplate codes for my views. But somehow my annotation processor is not generating the codes needed for the views hence throwing a ClassNotFoundException anytime I try running the…
devmike01
  • 1,971
  • 1
  • 20
  • 30
2
votes
1 answer

Kotlinpoet How to add inner object class definition?

I am trying to generate an object definition inside of a class. This is a distilled version: class SomeClass { // need to figure out how to generate this companion object { // and this object Constants { val…
ClayHerendeen
  • 187
  • 1
  • 4
  • 15
2
votes
1 answer

How to generate `by lazy` using KotlinPoet

I want to generate code like this: class B private constructor() : A { companion object { val instance: B by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { B() } } } Using KotlinPoet: private fun…
Ellie Zou
  • 2,021
  • 2
  • 16
  • 21
2
votes
1 answer

Kotlin/KAPT Generated Kotlin class is not recognized as class member, but it does inside of methods

I have written an annotation processor that generates a builder class for my classes annotated with @DataBuilder @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE) annotation class DataBuilder My classes annotated with this…
epool
  • 6,710
  • 7
  • 38
  • 43
2
votes
2 answers

How should I form a list property type with my own type

I am trying to form below final kotlin code val participants: List I tried to use below code in kotlinpoet but it shows error, I think it is not correct, but don't know how should I fix it. Any one can help?…
Jiachuan Li
  • 195
  • 9
1
vote
1 answer

Kotlin Poet ambigouos imports

Let's say I have two interfaces in a project: interface InterfaceA { // ... interface Listener { // ... } } interface InterfaceB { // ... interface Listener { // ... } } and I declare both of them and their…
Luja93
  • 451
  • 3
  • 8
1
vote
1 answer

Modify Kotlin compiled code output based on annotation (not generate separate code)

Is there a way to create a Kotlin compiler plugin that can modify the code being written? I don't want to create separate generated code but actually modify the code itself. For example, given this source: Original @Composable fun MyScreen() { …
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
1
vote
1 answer

How to pass arguments to super using KotlinPoet

I am using KotlinPoet to generate some code. I have an abstract class with some parameters. Let´s say: abstract class Foo (val foo: String) And I want to use Kotlin Poet so I can create an implementation of this class in the following way: class…
JJaviMS
  • 392
  • 6
  • 18
1
vote
1 answer

Is Kotlin Poet JVM target only? I want to use it for native

Did not find any reference on that - all examples seem to be using KVM / Android code. Is it possible to use Poet for a native target?
Andy Victors
  • 335
  • 2
  • 9
1
vote
2 answers

How do I get Kotlin classes instead of java classes

I'm trying to generate a subclass from an annotated class and getting methods parameters using code below, My problem is that I always get java types, not kotlin types which conflict with parent class causing override error, How do I get correct…
Islam Assem
  • 1,376
  • 13
  • 21
1
vote
1 answer

How to make method return type of generated class in KotlinPoet?

I need to generate a Builder class with the help of KotlinPoet. For this purpose, I need to make the method return the Builder type. I do it in the following way: private fun generateInitUserBehaviorClass() = TypeSpec.classBuilder("Init") …
Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43
1
vote
0 answers

How to access enclosed element's Variable annotations in annotation processor?

I am creating a string of data class for our API optimization like this data class ex: @MyAnnotation data class Cast( @Json(name = "cast_id") val castId: Int, val name: String, @Json(name = "profile_path") val…