Thanks to @drawers, @rickclephas for contributing to this release.
2024-01-18
NameAllocator
API to control keyword pre-allocation (#1803).suspend
modifier in KSTypeReference.toTypeName
(#1793).Thanks to @gabrielittner for contributing to this release.
2023-12-04
KSTypeReference.toTypeName
(#1756).Thanks to @evant for contributing to this release.
2023-11-30
2023-11-19
2023-11-18
Thanks to @drawers, @fejesjoco, @takahirom, @martinbonnin, @mcarleio for contributing to this release.
In this release the :kotlinpoet
module has been converted to a Kotlin Multiplatform module (#1654), though for now it only supports the JVM target. Important: unless you're building with Gradle, you will now need to depend on the kotlinpoet-jvm
artifact, instead of kotlinpoet
- see Downloads for instructions.
TypeSpecHolder
interface for constructs that can hold a TypeSpec and their builders (#1723).FileSpec
(#1720).FileSpec.writeTo()
. (#1514).TypeSpec.expectClassBuilder()
and TypeSpec.valueClassBuilder()
(#1589).KSAnnotation
to AnnotationSpec
while omitting default values (#1538).FileSpec.builder
convenience for MemberName
(#1585).DecimalFormatSymbols.minusSign
for consistency across locales (#1658).:kotlinpoet-metadata
module, as most Flags
-API extensions have been removed in favor of the now-available first-party versions.2023-05-30
Annotatable.Builder
(#1581).2023-05-29
2023-05-29
Thanks to @Omico, @drawers, @RBusarow for contributing to this release.
New: Kotlin 1.8.21.
New: KSP 1.8.21-1.0.11.
New: Enable default methods in Java bytecode (#1561).
New: Group Kotlin and Renovate updates together in Renovate (#1562).
New: Extract trait interface for annotatable constructs and their builders (#1564).
New: Extract trait interface for documentable constructs and their builders (#1571).
New: Document the usage of STAR
(#1572).
New: Add builder for FunSpec
which accepts a MemberName
(#1574).
Fix: Omit public modifier on override function or constructor parameters (#1550).
Fix: Correct handling of members in various types (#1558).
Fix: Function return types now default to Unit
unless explicitly set (#1559).
Previously, when FunSpec
didn't have a return type specified and an expression body was produced, no return type would be emitted. However, starting from 1.14.0
, KotlinPoet will not add Unit
as a return type in such cases. In order to correct the generated output, you are to specify the actual return type of the FunSpec
.
Before 1.14.0
, if omitted, no return type is produced:
val funSpec = FunSpec.builder("foo") .addStatement("return 1") .build()
public fun foo() = 1
From 1.14.0
, the return type defaults to Unit
if not otherwise set:
val funSpec = FunSpec.builder("foo") .addStatement("return 1") .build()
public fun foo(): Unit = 1 // ❌
To fix it, explicitly define the return type:
val funSpec = FunSpec.builder("foo") + .returns(INT) .addStatement("return 1") .build()
public fun foo(): Int = 1 // ✅
Additionally, as part of this change, FunSpec.returnType
has changed to be non-nullable. This is a source- and binary-compatible change, although if you were performing null-checks then new warnings may appear after upgrade.
Fix: Append nested class names to alias during name lookup (#1568).
Fix: Allow PropertySpec with context receivers and without getter or setter (#1575).
2023-05-05
Thanks to @Squiry for contributing to this release.
KSType.toTypeName
fixed to work with aliased types (#1534).2023-04-28
Thanks to @rickclephas for contributing to this release.
KSType.toTypeName()
(#1529).2023-04-06
Thanks to @popematt, @bitPogo, @mars885, @sjudd, @Sironheart, @polarene, @DeoTimeTheGithubUser, @drawers for contributing to this release.
OriginatingElements
and TagMap
implementations (#1270).FileSpec.builder(ClassName)
(#1397).KSTypeArgument.toTypeName()
for a wildcard in a recursive type bound (#1272).shouldEmitPublicModifier
returns false (#1342).LambdaTypeName
(#1454).bestGuess
for KClass.asClassName
(#1469).TypeName
equals (#1477).ClassName
(#1506).2022-06-13
Thanks to @WhosNickDoglio, @sullis, @DRSchlaubi, @martinbonnin, @seriouslyhypersonic, @ephemient, @dkilmer, @aksh1618, @zsqw123, @roihershberg for contributing to this release.
interop-ksp
API promoted to stable.KSTypeAlias
with an unused type parameter.KSTypeAlias
-es recursively.@PropertySpec
and fix issues with annotations.header
and impl
as keywords (workaround for KT-52315).%N
instead of %L
for annotation arg names so keywords are handled.return
expressions.2022-03-24
Thanks to @liujingxing and @BoD for contributing to this release.
FileSpec
.val spec = FileSpec.scriptBuilder("Taco") .addStatement("println(%S)", "hello world!") .addKotlinDefaultImports() .build()
Generates a Taco.kts
file with the following contents:
println("hello world!")
KSAnnotation.toAnnotationSpec()
.Unit
and CharSequence
conversions in javapoet-interop
.FileSpec
.ClassName
.val
property if its getter is inline
.yield
to the list of reserved keywords.ParameterSpec
(i.e. crossinline
, vararg
, and noinline
).CodeBlock
s in class delegation getting toString()
'd instead of participating in code writing.KSType.isError
is true) to TypeName
.2021-10-22
Thanks to @glureau and @goooler for contributing to this release.
AnnotationSpec.get()
to use the arrayOf()
syntax instead of []
.@Deprecated
annotations on synthetic methods for annotations.2021-09-21
Thanks to @evant for contributing to this release.
TypeParameterResolver.EMPTY
argument to fun KSTypeArgument.toTypeName
in KSP interop.2021-09-20
Thanks to @martinbonnin, @idanakav, @goooler, and @anandwana001 for contributing to this release.
ParameterizedTypeName
with new type arguments via new copy()
overload.com.squareup:kotlinpoet-metadata
maven artifact. The previous kotlinpoet-metadata-*
subartifacts are no longer published.TypeNameAliasTag
has been moved to KotlinPoet's main artifact under TypeAliasTag
, for reuse with KSP interop.ImmutableKm*
classes have been removed. They were deemed to be a needless abstraction over the base kotlinx-metadata
Km types. All usages of these should be substituted with their non-immutable base types.AnnotationSpec.get
.kotlin-reflect
usage.Unit
types where possible.2021-06-22
@DelicateKotlinPoetApi
annotation.CodeBlock.Builder.withIndent
helper function.ParameterSpec.Builder
and PropertySpec.Builder
after they were set.MemberName.isExtension
property that instructs KotlinPoet to always import the member, even if conflicting declarations are present in the same scope.TypeSpec.primaryConstructor
.2021-03-29
KModifier.VALUE
to support value class
declarations.ClassLoader
with ReflectiveClassInspector
.ImmutableKmProperty.toMutable()
copies fieldSignature
.MemberName
and a member in current scope.expect
and external
functions.expect
or external
classes to initialize supertypes.external
classes.abstract
modifiers on members inside interfaces2020-10-20
return·
and throw·
prefixes.2020-10-15
"org.gradle.jvm.version"
to "11"
in Gradle metadata, making it impossible to use the library on earlier Java versions (see #999). 1.7.1 is published with JDK 8, which fixes the problem.2020-10-14
2020-05-28
New: Deprecate Mirror API integrations.
Mirror API integrations, such as TypeElement.asClassName()
and FunSpec.overriding(ExecutableElement)
, are being deprecated in this release. These KotlinPoet APIs are most often used in annotation processors. Since kapt runs annotation processors over stubs, which are Java files, a lot of the Kotlin-specific information gets lost in translation and cannot be accessed by KotlinPoet through the Mirror API integrations. Examples include:
kotlin.String
, get converted to their JVM representations, such as java.lang.String
.suspend
functions are seen as simple functions with an additional Continuation
parameter.The correct solution is to switch to KotlinPoet-metadata or KotlinPoet-metadata-specs API, which fetches Kotlin-specific information from the @Metadata
annotation and produces correct KotlinPoet Specs. We may explore adding new metadata-based alternatives to the deprecated APIs in the future.
New: Kotlin 1.3.72.
New: Improve MemberName
to support operator overloading.
New: Support generics in AnnotationSpec
.
New: Add support for functional interfaces.
New: Make more FunSpec.Builder
members public for easier mutation.
Fix: Properly propagate implicit type and function modifiers in nested declarations.
Fix: Properly escape type names containing $
character.
Fix: Don't emit LambdaTypeName
annotations twice.
Fix: Preserve tags in TypeName.copy()
.
2020-01-09
KotlinPoet now targets JDK8, which means that executing a build that includes KotlinPoet as a dependency on a machine with an older version of JDK installed won‘t work. This has no effect on the code that KotlinPoet produces: the code can still be compiled against JDK6, as long as it doesn’t use any features that were introduced in newer releases.
AnnotationSpec
s with the annotation mirror when available.TypeSpec
s from metadata.TypeSpec
s with names that can be escaped with backticks.TypeSpec
. Note: with this change, any properties declared after any initializer blocks will not be added to the primary constructor and will instead be emitted inside the TypeSpec
body.2019-11-16
2019-10-30
2019-10-28
2019-10-18
TypeAliasSpec
.Metadata
.ImmutableKmDeclarationContainer
.auto-common
.Asset<A : Asset<A>>
) from Kotlin Metadata
.2019-09-24
New: This release introduces the new KotlinPoet-metadata API that makes it easy to introspect Kotlin types and build KotlinPoet Specs based on that data.
The strategy for type introspection is driven by ClassInspector
, which is a basic interface for looking up JVM information about a given Class. This optionally is used by the toTypeSpec()
/toFileSpec()
APIs in kotlinpoet-metadata-specs
artifact to inform about Classes with information that isn’t present in metadata (overrides, JVM modifiers, etc). There are two batteries-included implementations available in ReflectiveClassInspector
(for reflection) and ElementsClassInspector
(for the javax Elements API in annotation processing). These implementations are available through their respective kotlinpoet-classinspector-*
artifacts. For more information refer to the KotlinPoet-metadata-specs README.
At the time of this release the API is in experimental mode and has to be opted into via the KotlinPoetMetadataPreview
annotation.
New: Kotlin 1.3.50.
New: A new constructor to simplify creation of ParameterSpec
instances.
New: New ClassName
constructors.
New: TypeName
and subclasses can now store tags.
New: Optional parameters added to toBuilder()
methods of most Specs.
New: List
overrides for Spec methods that accept vararg
s.
New: CodeBlock.Builder.clear()
helper method.
New: FunSpec.Builder.clearBody()
helper method.
Fix: Properly escape enum constant names.
Fix: Ensure trailing newlines in KDoc and function bodies.
Fix: TypeVariableName
s with empty bounds will now default to Any?
.
Fix: Don't emit parens for primary constructors.
Fix: ClassName
s with empty simple names are not allowed anymore.
Fix: Throw if names contain illegal characters that can't be escaped with backticks.
2019-05-30
2019-03-28
2019-02-28
%M
and MemberName
type.MemberName
from a ClassName
, KClass
and Class
.CodeBlock
s as arguments to %P
.toString()
.LineWrapper
to enable proper line wrapping in KDoc.TypeSpec
Kdoc with no tags.FunSpec
.CodeWriter.resolve()
by precomputing all of the nested simple names of a TypeSpec
.KType.asTypeName()
.FileSpec
.2019-01-02
2018-12-10
2018-11-28
%P
placeholder for string templates.Unit
as return type.%>
, %<
, %[
, %]
placeholders with ⇥
, ⇤
, «
, »
.%W
with space, and add ·
as a non-breaking space.TypeName
to sealed class.TypeName
modifier methods with copy()
.WildcardTypeName
to match with the producer/consumer generics model.TypeName.nullable
into TypeName.isNullable
.LambdaTypeName.suspending
into LambdaTypeName.isSuspending
.TypeVariableName.reified
into TypeVariableName.isReified
.Any?
upper bound.2018-10-22
beginControlFlow()
.ParameterSpec
, collapse into parent KDoc.TypeVariable
s in PropertySpec
.LambdaTypeName
.buildCodeBlock()
helper function.CodeBlock
s with statements as property initializers and default parameter values.NameAllocator.clone()
into `NameAllocator.copy().TypeName.asNonNullable()
to TypeName.asNonNull()
.PropertySpec.varBuilder()
(use mutable()
instead).IntArray
and Array<Int>
when creating TypeName
.TypeName
instead of ClassName
as parameter type of plusParameter()
.TypeName
from KType
.FileSpec.Builder
blank package and subfolder fix.toBuilder()
.2018-07-16
external
support.KType
and KTypeParameter
.TypeSpec#toBuilder()
.ParameterizedTypeName
API.WildcardTypeName.STAR
constant.Any?
as the default type variable bounds.TypeName
.TypeVariable
bound when more bounds are added.2018-02-16
PropertySpec
s as CodeBlock
literals.TypeAliasSpec
.ParameterSpec
which is also a keyword.LambdaTypeName
parameters.Any?
.2017-11-03
import bar.Bar as bBar
.LambdaTypeName
.2017-09-13
addFun()
to addFunction()
.KotlinFile
to FileSpec
.KotlinFile.addFileAnnotation()
to addAnnotation()
.KotlinFile.addFileComment()
to addComment()
.HEADER
and IMPL
modifiers.2017-08-08
asClassName()
to be top-level functions.constructor
keyword.NameAllocator
.AnnotationSpec
for throws.2017-06-11
TypeAliasSpec
to create type aliases.LambdaTypeName
to create lambda types.Runnable::class.asClassName()
.Filer
writing and originating elements concept. These stem from javac
annotation processors.KModifier
for varargs.ParameterizedTypeName
for array types.KClass
instead of Java name.Double::class
instead of Double.class
.2017-05-21
:
instead of extends
and implements
.init {}
.2017-05-16