Skip to content

Commit

Permalink
Improve function reifier
Browse files Browse the repository at this point in the history
- Improve detection of function types
- Add contract to reified functions if possible
  • Loading branch information
DRSchlaubi committed Sep 16, 2023
1 parent 04bbadf commit da3aabb
Show file tree
Hide file tree
Showing 33 changed files with 235 additions and 123 deletions.
8 changes: 8 additions & 0 deletions kotlinpoet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ dependencies {
commonTestImplementation(kotlin("test-junit5"))
}

kotlin {
sourceSets {
all {
languageSettings.optIn("kotlin.contracts.ExperimentalContracts")
}
}
}

codeGeneration {
fromVersionCatalog(libs.kotlinpoet)
packageName = "dev.kord.codegen.kotlinpoet"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.PropertySpec
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.Annotatable
Expand All @@ -8,7 +6,6 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.ParameterizedTypeName
import javax.lang.model.element.AnnotationMirror
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import com.squareup.kotlinpoet.Annotatable
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.asClassName
import kotlin.Annotation
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

public inline fun <reified T : Annotation>
Annotatable.Builder<*>.addAnnotation(block: AnnotationSpecBuilderScope = {}): AnnotationSpec =
addAnnotation(T::class.asClassName(), block)
Annotatable.Builder<*>.addAnnotation(block: AnnotationSpecBuilderScope = {}): AnnotationSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addAnnotation(T::class.asClassName(), block)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.AnnotationSpec
Expand All @@ -8,7 +6,6 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.ParameterizedTypeName
import javax.lang.model.element.AnnotationMirror
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package dev.kord.codegen.kotlinpoet
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.asClassName
import kotlin.Annotation
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

public inline fun <reified T : Annotation> AnnotationSpec(block: AnnotationSpecBuilderScope = {}):
AnnotationSpec = AnnotationSpec(T::class.asClassName(), block)
AnnotationSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return AnnotationSpec(T::class.asClassName(), block)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.CodeBlock
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FileSpec.Companion.`get`
import com.squareup.kotlinpoet.TypeSpec
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.asClassName
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

public inline fun <reified C> FileSpec(block: FileSpecBuilderScope = {}): FileSpec =
FileSpec(C::class.asClassName(), block)
public inline fun <reified C> FileSpec(block: FileSpecBuilderScope = {}): FileSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return FileSpec(C::class.asClassName(), block)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.MemberName
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.TypeSpec
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.DelicateKotlinPoetApi
Expand All @@ -11,7 +9,6 @@ import dev.kord.codegen.kotlinpoet.`delegate`.SubSpecDelegateProvider
import dev.kord.codegen.kotlinpoet.`delegate`.produceByName
import java.lang.reflect.Type
import javax.lang.model.element.VariableElement
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,35 @@ import com.squareup.kotlinpoet.typeNameOf
import dev.kord.codegen.kotlinpoet.`delegate`.SubSpecDelegateProvider
import kotlin.String
import kotlin.collections.Iterable
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

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

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

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

public inline fun <reified T> FunSpec.Builder.addParameter(modifiers: Iterable<KModifier>, noinline
block: ParameterSpecBuilderScope = {}): SubSpecDelegateProvider<ParameterSpec> =
addParameter(typeNameOf<T>(), modifiers, block)
block: ParameterSpecBuilderScope = {}): SubSpecDelegateProvider<ParameterSpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addParameter(typeNameOf<T>(), modifiers, block)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.DelicateKotlinPoetApi
Expand All @@ -9,7 +7,6 @@ import com.squareup.kotlinpoet.ParameterSpec.Companion.`get`
import com.squareup.kotlinpoet.TypeName
import java.lang.reflect.Type
import javax.lang.model.element.VariableElement
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.typeNameOf
import kotlin.String
import kotlin.collections.Iterable
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

public inline fun <reified T> ParameterSpec(
name: String,
vararg modifiers: KModifier,
block: ParameterSpecBuilderScope = {},
): ParameterSpec = ParameterSpec(name, typeNameOf<T>(), modifiers = modifiers, block)
): ParameterSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return ParameterSpec(name, typeNameOf<T>(), modifiers = modifiers, block)
}

public inline fun <reified T> ParameterSpec(
name: String,
modifiers: Iterable<KModifier>,
block: ParameterSpecBuilderScope = {},
): ParameterSpec = ParameterSpec(name, typeNameOf<T>(), modifiers, block)
): ParameterSpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return ParameterSpec(name, typeNameOf<T>(), modifiers, block)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.DelicateKotlinPoetApi
Expand All @@ -11,7 +9,6 @@ import com.squareup.kotlinpoet.TypeSpec
import dev.kord.codegen.kotlinpoet.`delegate`.SubSpecDelegateProvider
import dev.kord.codegen.kotlinpoet.`delegate`.produceByName
import java.lang.reflect.Type
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,65 @@ import com.squareup.kotlinpoet.typeNameOf
import dev.kord.codegen.kotlinpoet.`delegate`.SubSpecDelegateProvider
import kotlin.String
import kotlin.collections.Iterable
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

public inline fun <reified T> TypeSpec.Builder.addProperty(
name: String,
vararg modifiers: KModifier,
block: PropertySpecBuilderScope = {},
): PropertySpec = addProperty(name, typeNameOf<T>(), modifiers = modifiers, block)
): PropertySpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(name, typeNameOf<T>(), modifiers = modifiers, block)
}

public inline fun <reified T> TypeSpec.Builder.addProperty(vararg modifiers: KModifier, noinline
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> =
addProperty(typeNameOf<T>(), modifiers = modifiers, block)
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(typeNameOf<T>(), modifiers = modifiers, block)
}

public inline fun <reified T> TypeSpec.Builder.addProperty(
name: String,
modifiers: Iterable<KModifier>,
block: PropertySpecBuilderScope = {},
): PropertySpec = addProperty(name, typeNameOf<T>(), modifiers, block)
): PropertySpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(name, typeNameOf<T>(), modifiers, block)
}

public inline fun <reified T> TypeSpec.Builder.addProperty(modifiers: Iterable<KModifier>, noinline
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> =
addProperty(typeNameOf<T>(), modifiers, block)
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(typeNameOf<T>(), modifiers, block)
}

public inline fun <reified T> FileSpec.Builder.addProperty(
name: String,
vararg modifiers: KModifier,
block: PropertySpecBuilderScope = {},
): PropertySpec = addProperty(name, typeNameOf<T>(), modifiers = modifiers, block)
): PropertySpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(name, typeNameOf<T>(), modifiers = modifiers, block)
}

public inline fun <reified T> FileSpec.Builder.addProperty(vararg modifiers: KModifier, noinline
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> =
addProperty(typeNameOf<T>(), modifiers = modifiers, block)
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(typeNameOf<T>(), modifiers = modifiers, block)
}

public inline fun <reified T> FileSpec.Builder.addProperty(
name: String,
modifiers: Iterable<KModifier>,
block: PropertySpecBuilderScope = {},
): PropertySpec = addProperty(name, typeNameOf<T>(), modifiers, block)
): PropertySpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(name, typeNameOf<T>(), modifiers, block)
}

public inline fun <reified T> FileSpec.Builder.addProperty(modifiers: Iterable<KModifier>, noinline
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> =
addProperty(typeNameOf<T>(), modifiers, block)
block: PropertySpecBuilderScope = {}): SubSpecDelegateProvider<PropertySpec> {
contract { callsInPlace(block, EXACTLY_ONCE) }
return addProperty(typeNameOf<T>(), modifiers, block)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeName
import java.lang.reflect.Type
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.typeNameOf
import kotlin.String
import kotlin.collections.Iterable
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract

public inline fun <reified T> PropertySpec(
name: String,
vararg modifiers: KModifier,
block: PropertySpecBuilderScope = {},
): PropertySpec = PropertySpec(name, typeNameOf<T>(), modifiers = modifiers, block)
): PropertySpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return PropertySpec(name, typeNameOf<T>(), modifiers = modifiers, block)
}

public inline fun <reified T> PropertySpec(
name: String,
modifiers: Iterable<KModifier>,
block: PropertySpecBuilderScope = {},
): PropertySpec = PropertySpec(name, typeNameOf<T>(), modifiers, block)
): PropertySpec {
contract { callsInPlace(block, EXACTLY_ONCE) }
return PropertySpec(name, typeNameOf<T>(), modifiers, block)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
@file:OptIn(ExperimentalContracts::class)

package dev.kord.codegen.kotlinpoet

import com.squareup.kotlinpoet.DelicateKotlinPoetApi
import com.squareup.kotlinpoet.TypeAliasSpec
import com.squareup.kotlinpoet.TypeName
import java.lang.reflect.Type
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind.EXACTLY_ONCE
import kotlin.contracts.contract
import kotlin.reflect.KClass
Expand Down
Loading

0 comments on commit da3aabb

Please sign in to comment.