Skip to content

Commit

Permalink
Generator Improvements
Browse files Browse the repository at this point in the history
- No longer generated incorrect contracts on delegates and reifications
- Also add deprecations to reified versions
 Remove some Suppression of INVISIBLE_REFERENCE
  • Loading branch information
DRSchlaubi committed Nov 18, 2023
1 parent 225d359 commit 42f4fc6
Show file tree
Hide file tree
Showing 29 changed files with 357 additions and 190 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/code-generator.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tasks {
val kspGenerationSourceKotlin by getting(KspTask::class) {
dependsOn(downloadSources)
// We don't need to reify twice, actual reification occurs in main source
commandLineArgumentProviders.add(CommandLineArgumentProvider{ listOf("disable-reification=true") })
commandLineArgumentProviders.add(CommandLineArgumentProvider{ listOf("only-reify=com.squareup.kotlinpoet") })
}
compileKotlin {
dependsOn(kspGenerationSourceKotlin)
Expand Down
104 changes: 40 additions & 64 deletions kotlinpoet/api/kotlinpoet.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.Annotatable
import com.squareup.kotlinpoet.asClassName

public inline fun <T : Annotatable.Builder<T>, reified A> Annotatable.Builder<T>.addAnnotation(): T
= addAnnotation(A::class.asClassName())
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.asClassName
import kotlin.Int

public inline fun <reified O> ClassName.compareTo(): Int = compareTo(O::class.asClassName())
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.asClassName
import kotlin.String
import kotlin.collections.Iterable

public inline fun <reified C> FileSpec.Builder.addImport(vararg names: String): FileSpec.Builder =
addImport(C::class.asClassName(), names = names)

public inline fun <reified C> FileSpec.Builder.addImport(names: Iterable<String>): FileSpec.Builder
= addImport(C::class.asClassName(), names)

public inline fun <reified C> FileSpec.Builder.addAliasedImport(`as`: String): FileSpec.Builder =
addAliasedImport(C::class.asClassName(), `as`)

public inline fun <reified C> FileSpec.Builder.addAliasedImport(memberName: String, `as`: String):
FileSpec.Builder = addAliasedImport(C::class.asClassName(), memberName, `as`)

public inline fun <reified A> FileSpec.Builder.addAnnotation(): FileSpec.Builder =
addAnnotation(A::class.asClassName())
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.typeNameOf

public inline fun <reified R> FunSpec.Builder.`receiver`(kdoc: CodeBlock = emptyCodeBlock()):
FunSpec.Builder = `receiver`(typeNameOf<R>(), kdoc)

public inline fun <reified R> FunSpec.Builder.returns(kdoc: CodeBlock = emptyCodeBlock()):
FunSpec.Builder = returns(typeNameOf<R>(), kdoc)

public inline fun <reified A> FunSpec.Builder.addAnnotation(): FunSpec.Builder =
addAnnotation(A::class.asClassName())
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.typeNameOf
import kotlin.collections.List

@ExperimentalKotlinPoetApi
public inline fun <reified R, reified R_>
LambdaTypeName.Companion.`get`(parameters: List<ParameterSpec> = emptyList(),
contextReceivers: List<TypeName> = emptyList()): LambdaTypeName = `get`(typeNameOf<R>(),
parameters, typeNameOf<R_>(), contextReceivers)

public inline fun <reified R, reified R_>
LambdaTypeName.Companion.`get`(parameters: List<ParameterSpec> = emptyList()): LambdaTypeName =
`get`(typeNameOf<R>(), parameters, typeNameOf<R_>())

public inline fun <reified R, reified R_> LambdaTypeName.Companion.`get`(vararg parameters: TypeName
= emptyArray()): LambdaTypeName = `get`(typeNameOf<R>(), parameters = parameters,
typeNameOf<R_>())

public inline fun <reified R, reified R_> LambdaTypeName.Companion.`get`(vararg
parameters: ParameterSpec = emptyArray()): LambdaTypeName = `get`(typeNameOf<R>(),
parameters = parameters, typeNameOf<R_>())
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.typeNameOf
import kotlin.String

public inline fun <reified T> ParameterSpec.toBuilder(name: String = this.name):
ParameterSpec.Builder = toBuilder(name, typeNameOf<T>())

public inline fun <reified A> ParameterSpec.Builder.addAnnotation(): ParameterSpec.Builder =
addAnnotation(A::class.asClassName())

public inline fun <reified T> ParameterSpec.Companion.unnamed(): ParameterSpec =
unnamed(typeNameOf<T>())
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.typeNameOf

public inline fun <reified T> ParameterizedTypeName.plusParameter(): ParameterizedTypeName =
plusParameter(typeNameOf<T>())
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.typeNameOf
import kotlin.String

public inline fun <reified T> PropertySpec.toBuilder(name: String = this.name): PropertySpec.Builder
= toBuilder(name, typeNameOf<T>())

public inline fun <reified R> PropertySpec.Builder.`receiver`(): PropertySpec.Builder =
`receiver`(typeNameOf<R>())

public inline fun <reified A> PropertySpec.Builder.addAnnotation(): PropertySpec.Builder =
addAnnotation(A::class.asClassName())
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.TypeAliasSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.typeNameOf
import kotlin.String

public inline fun <reified T> TypeAliasSpec.toBuilder(name: String = this.name):
TypeAliasSpec.Builder = toBuilder(name, typeNameOf<T>())

public inline fun <reified A> TypeAliasSpec.Builder.addAnnotation(): TypeAliasSpec.Builder =
addAnnotation(A::class.asClassName())
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,39 @@ public inline fun TypeSpec.Companion.`class`(className: ClassName, block: TypeSp
return TypeSpec.classBuilder(className).apply(block).build()
}

@Deprecated(
message = "Use classBuilder() instead. This function will be removed in KotlinPoet 2.0.",
replaceWith = ReplaceWith(imports = arrayOf(), expression =
"TypeSpec.classBuilder(name).addModifiers(KModifier.EXPECT)"),
level = DeprecationLevel.WARNING,
)
@Suppress(names = arrayOf("DEPRECATION"))
public inline fun TypeSpec.Companion.expectClass(name: String, block: TypeSpecBuilderScope = {}):
TypeSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return TypeSpec.expectClassBuilder(name).apply(block).build()
}

@Deprecated(
message = "Use classBuilder() instead. This function will be removed in KotlinPoet 2.0.",
replaceWith = ReplaceWith(imports = arrayOf(), expression =
"TypeSpec.classBuilder(className).addModifiers(KModifier.EXPECT)"),
level = DeprecationLevel.WARNING,
)
@Suppress(names = arrayOf("DEPRECATION"))
public inline fun TypeSpec.Companion.expectClass(className: ClassName, block: TypeSpecBuilderScope =
{}): TypeSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return TypeSpec.expectClassBuilder(className).apply(block).build()
}

@Deprecated(
message = "Use classBuilder() instead. This function will be removed in KotlinPoet 2.0.",
replaceWith = ReplaceWith(imports = arrayOf(), expression =
"TypeSpec.classBuilder(name).addModifiers(KModifier.VALUE)"),
level = DeprecationLevel.WARNING,
)
@Suppress(names = arrayOf("DEPRECATION"))
public inline fun TypeSpec.Companion.valueClass(name: String, block: TypeSpecBuilderScope = {}):
TypeSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.typeNameOf
import kotlin.Deprecated
import kotlin.DeprecationLevel
import kotlin.ReplaceWith
import kotlin.String
import kotlin.Suppress

public inline fun <reified S> TypeSpec.Builder.superclass(): TypeSpec.Builder =
superclass(typeNameOf<S>())

public inline fun <reified S> TypeSpec.Builder.addSuperinterface(`delegate`: CodeBlock =
emptyCodeBlock()): TypeSpec.Builder = addSuperinterface(typeNameOf<S>(), `delegate`)

public inline fun <reified S> TypeSpec.Builder.addSuperinterface(constructorParameter: String):
TypeSpec.Builder = addSuperinterface(typeNameOf<S>(), constructorParameter)

public inline fun <reified A> TypeSpec.Builder.addAnnotation(): TypeSpec.Builder =
addAnnotation(A::class.asClassName())

public inline fun <reified C> TypeSpec.Companion.classBuilder(): TypeSpec.Builder =
classBuilder(C::class.asClassName())

@Deprecated(
message = "Use classBuilder() instead. This function will be removed in KotlinPoet 2.0.",
replaceWith = ReplaceWith(imports = arrayOf(), expression =
"TypeSpec.classBuilder(className).addModifiers(KModifier.EXPECT)"),
level = DeprecationLevel.WARNING,
)
@Suppress(names = arrayOf("DEPRECATION"))
public inline fun <reified C> TypeSpec.Companion.expectClassBuilder(): TypeSpec.Builder =
expectClassBuilder(C::class.asClassName())

public inline fun <reified C> TypeSpec.Companion.objectBuilder(): TypeSpec.Builder =
objectBuilder(C::class.asClassName())

public inline fun <reified C> TypeSpec.Companion.interfaceBuilder(): TypeSpec.Builder =
interfaceBuilder(C::class.asClassName())

public inline fun <reified C> TypeSpec.Companion.funInterfaceBuilder(): TypeSpec.Builder =
funInterfaceBuilder(C::class.asClassName())

public inline fun <reified C> TypeSpec.Companion.enumBuilder(): TypeSpec.Builder =
enumBuilder(C::class.asClassName())

public inline fun <reified C> TypeSpec.Companion.annotationBuilder(): TypeSpec.Builder =
annotationBuilder(C::class.asClassName())
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.typeNameOf

public inline fun <reified O> WildcardTypeName.Companion.producerOf(): WildcardTypeName =
producerOf(typeNameOf<O>())

public inline fun <reified I> WildcardTypeName.Companion.consumerOf(): WildcardTypeName =
consumerOf(typeNameOf<I>())
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ public fun FunSpec.Builder.addParameter(
type: TypeName,
vararg modifiers: KModifier,
block: ParameterSpecBuilderScope = {},
): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return produceByName { name ->
ParameterSpec(name, type, modifiers = modifiers, block).also(::addParameter)
}
): SubSpecDelegateProvider<ParameterSpec> = produceByName { name ->
ParameterSpec(name, type, modifiers = modifiers, block).also(::addParameter)
}

public inline fun FunSpec.Builder.addParameter(
Expand All @@ -48,11 +45,8 @@ public fun FunSpec.Builder.addParameter(
type: TypeName,
modifiers: Iterable<KModifier>,
block: ParameterSpecBuilderScope = {},
): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return produceByName { name ->
ParameterSpec(name, type, modifiers, block).also(::addParameter)
}
): SubSpecDelegateProvider<ParameterSpec> = produceByName { name ->
ParameterSpec(name, type, modifiers, block).also(::addParameter)
}

public inline fun FunSpec.Builder.addParameter(
Expand All @@ -69,11 +63,8 @@ public fun FunSpec.Builder.addParameter(
type: Type,
vararg modifiers: KModifier,
block: ParameterSpecBuilderScope = {},
): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return produceByName { name ->
ParameterSpec(name, type, modifiers = modifiers, block).also(::addParameter)
}
): SubSpecDelegateProvider<ParameterSpec> = produceByName { name ->
ParameterSpec(name, type, modifiers = modifiers, block).also(::addParameter)
}

public inline fun FunSpec.Builder.addParameter(
Expand All @@ -90,11 +81,8 @@ public fun FunSpec.Builder.addParameter(
type: Type,
modifiers: Iterable<KModifier>,
block: ParameterSpecBuilderScope = {},
): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return produceByName { name ->
ParameterSpec(name, type, modifiers, block).also(::addParameter)
}
): SubSpecDelegateProvider<ParameterSpec> = produceByName { name ->
ParameterSpec(name, type, modifiers, block).also(::addParameter)
}

public inline fun FunSpec.Builder.addParameter(
Expand All @@ -111,11 +99,8 @@ public fun FunSpec.Builder.addParameter(
type: KClass<*>,
vararg modifiers: KModifier,
block: ParameterSpecBuilderScope = {},
): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return produceByName { name ->
ParameterSpec(name, type, modifiers = modifiers, block).also(::addParameter)
}
): SubSpecDelegateProvider<ParameterSpec> = produceByName { name ->
ParameterSpec(name, type, modifiers = modifiers, block).also(::addParameter)
}

public inline fun FunSpec.Builder.addParameter(
Expand All @@ -132,11 +117,8 @@ public fun FunSpec.Builder.addParameter(
type: KClass<*>,
modifiers: Iterable<KModifier>,
block: ParameterSpecBuilderScope = {},
): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return produceByName { name ->
ParameterSpec(name, type, modifiers, block).also(::addParameter)
}
): SubSpecDelegateProvider<ParameterSpec> = produceByName { name ->
ParameterSpec(name, type, modifiers, block).also(::addParameter)
}

@DelicateKotlinPoetApi(message =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ public inline fun <reified T> FunSpec.Builder.addParameter(
}

public inline fun <reified T> FunSpec.Builder.addParameter(vararg modifiers: KModifier, noinline
block: ParameterSpecBuilderScope = {}): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addParameter(typeNameOf<T>(), modifiers = modifiers, block)
}
block: ParameterSpecBuilderScope = {}): SubSpecDelegateProvider<ParameterSpec> =
addParameter(typeNameOf<T>(), modifiers = modifiers, block)

public inline fun <reified T> FunSpec.Builder.addParameter(
name: String,
Expand All @@ -35,7 +33,5 @@ public inline fun <reified T> FunSpec.Builder.addParameter(
}

public inline fun <reified T> FunSpec.Builder.addParameter(modifiers: Iterable<KModifier>, noinline
block: ParameterSpecBuilderScope = {}): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addParameter(typeNameOf<T>(), modifiers, block)
}
block: ParameterSpecBuilderScope = {}): SubSpecDelegateProvider<ParameterSpec> =
addParameter(typeNameOf<T>(), modifiers, block)
Loading

0 comments on commit 42f4fc6

Please sign in to comment.