Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract interface for constructs that can hold a TypeSpec and their builders #1723

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions kotlinpoet/api/kotlinpoet.api
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public final class com/squareup/kotlinpoet/Dynamic : com/squareup/kotlinpoet/Typ
public abstract interface annotation class com/squareup/kotlinpoet/ExperimentalKotlinPoetApi : java/lang/annotation/Annotation {
}

public final class com/squareup/kotlinpoet/FileSpec : com/squareup/kotlinpoet/Annotatable, com/squareup/kotlinpoet/Taggable {
public final class com/squareup/kotlinpoet/FileSpec : com/squareup/kotlinpoet/Annotatable, com/squareup/kotlinpoet/Taggable, com/squareup/kotlinpoet/TypeSpecHolder {
public static final field Companion Lcom/squareup/kotlinpoet/FileSpec$Companion;
public static final fun builder (Lcom/squareup/kotlinpoet/ClassName;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public static final fun builder (Lcom/squareup/kotlinpoet/MemberName;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
Expand All @@ -196,6 +196,7 @@ public final class com/squareup/kotlinpoet/FileSpec : com/squareup/kotlinpoet/An
public final fun getName ()Ljava/lang/String;
public final fun getPackageName ()Ljava/lang/String;
public fun getTags ()Ljava/util/Map;
public fun getTypeSpecs ()Ljava/util/List;
public fun hashCode ()I
public final fun isScript ()Z
public static final fun scriptBuilder (Ljava/lang/String;Ljava/lang/String;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
Expand All @@ -213,7 +214,7 @@ public final class com/squareup/kotlinpoet/FileSpec : com/squareup/kotlinpoet/An
public final fun writeTo (Ljavax/annotation/processing/Filer;)V
}

public final class com/squareup/kotlinpoet/FileSpec$Builder : com/squareup/kotlinpoet/Annotatable$Builder, com/squareup/kotlinpoet/Taggable$Builder {
public final class com/squareup/kotlinpoet/FileSpec$Builder : com/squareup/kotlinpoet/Annotatable$Builder, com/squareup/kotlinpoet/Taggable$Builder, com/squareup/kotlinpoet/TypeSpecHolder$Builder {
public final fun addAliasedImport (Lcom/squareup/kotlinpoet/ClassName;Ljava/lang/String;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun addAliasedImport (Lcom/squareup/kotlinpoet/ClassName;Ljava/lang/String;Ljava/lang/String;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun addAliasedImport (Lcom/squareup/kotlinpoet/MemberName;Ljava/lang/String;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
Expand Down Expand Up @@ -251,8 +252,11 @@ public final class com/squareup/kotlinpoet/FileSpec$Builder : com/squareup/kotli
public final fun addNamedCode (Ljava/lang/String;Ljava/util/Map;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun addProperty (Lcom/squareup/kotlinpoet/PropertySpec;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun addStatement (Ljava/lang/String;[Ljava/lang/Object;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun addType (Lcom/squareup/kotlinpoet/TypeSpec;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public fun addType (Lcom/squareup/kotlinpoet/TypeSpec;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public synthetic fun addType (Lcom/squareup/kotlinpoet/TypeSpec;)Lcom/squareup/kotlinpoet/TypeSpecHolder$Builder;
public final fun addTypeAlias (Lcom/squareup/kotlinpoet/TypeAliasSpec;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public fun addTypes (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public synthetic fun addTypes (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/TypeSpecHolder$Builder;
public final fun beginControlFlow (Ljava/lang/String;[Ljava/lang/Object;)Lcom/squareup/kotlinpoet/FileSpec$Builder;
public final fun build ()Lcom/squareup/kotlinpoet/FileSpec;
public final fun clearBody ()Lcom/squareup/kotlinpoet/FileSpec$Builder;
Expand Down Expand Up @@ -925,7 +929,7 @@ public final class com/squareup/kotlinpoet/TypeNames {
public static final fun get (Lkotlin/reflect/KClass;)Lcom/squareup/kotlinpoet/ClassName;
}

public final class com/squareup/kotlinpoet/TypeSpec : com/squareup/kotlinpoet/Annotatable, com/squareup/kotlinpoet/ContextReceivable, com/squareup/kotlinpoet/Documentable, com/squareup/kotlinpoet/OriginatingElementsHolder, com/squareup/kotlinpoet/Taggable {
public final class com/squareup/kotlinpoet/TypeSpec : com/squareup/kotlinpoet/Annotatable, com/squareup/kotlinpoet/ContextReceivable, com/squareup/kotlinpoet/Documentable, com/squareup/kotlinpoet/OriginatingElementsHolder, com/squareup/kotlinpoet/Taggable, com/squareup/kotlinpoet/TypeSpecHolder {
public static final field Companion Lcom/squareup/kotlinpoet/TypeSpec$Companion;
public static final fun annotationBuilder (Lcom/squareup/kotlinpoet/ClassName;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public static final fun annotationBuilder (Ljava/lang/String;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
Expand Down Expand Up @@ -959,7 +963,7 @@ public final class com/squareup/kotlinpoet/TypeSpec : com/squareup/kotlinpoet/An
public final fun getSuperclassConstructorParameters ()Ljava/util/List;
public final fun getSuperinterfaces ()Ljava/util/Map;
public fun getTags ()Ljava/util/Map;
public final fun getTypeSpecs ()Ljava/util/List;
public fun getTypeSpecs ()Ljava/util/List;
public final fun getTypeVariables ()Ljava/util/List;
public fun hashCode ()I
public static final fun interfaceBuilder (Lcom/squareup/kotlinpoet/ClassName;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
Expand All @@ -981,7 +985,7 @@ public final class com/squareup/kotlinpoet/TypeSpec : com/squareup/kotlinpoet/An
public static final fun valueClassBuilder (Ljava/lang/String;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
}

public final class com/squareup/kotlinpoet/TypeSpec$Builder : com/squareup/kotlinpoet/Annotatable$Builder, com/squareup/kotlinpoet/ContextReceivable$Builder, com/squareup/kotlinpoet/Documentable$Builder, com/squareup/kotlinpoet/OriginatingElementsHolder$Builder, com/squareup/kotlinpoet/Taggable$Builder {
public final class com/squareup/kotlinpoet/TypeSpec$Builder : com/squareup/kotlinpoet/Annotatable$Builder, com/squareup/kotlinpoet/ContextReceivable$Builder, com/squareup/kotlinpoet/Documentable$Builder, com/squareup/kotlinpoet/OriginatingElementsHolder$Builder, com/squareup/kotlinpoet/Taggable$Builder, com/squareup/kotlinpoet/TypeSpecHolder$Builder {
public synthetic fun addAnnotation (Lcom/squareup/kotlinpoet/AnnotationSpec;)Lcom/squareup/kotlinpoet/Annotatable$Builder;
public fun addAnnotation (Lcom/squareup/kotlinpoet/AnnotationSpec;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public synthetic fun addAnnotation (Lcom/squareup/kotlinpoet/ClassName;)Lcom/squareup/kotlinpoet/Annotatable$Builder;
Expand Down Expand Up @@ -1023,10 +1027,12 @@ public final class com/squareup/kotlinpoet/TypeSpec$Builder : com/squareup/kotli
public static synthetic fun addSuperinterface$default (Lcom/squareup/kotlinpoet/TypeSpec$Builder;Ljava/lang/reflect/Type;Lcom/squareup/kotlinpoet/CodeBlock;ILjava/lang/Object;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public static synthetic fun addSuperinterface$default (Lcom/squareup/kotlinpoet/TypeSpec$Builder;Lkotlin/reflect/KClass;Lcom/squareup/kotlinpoet/CodeBlock;ILjava/lang/Object;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public final fun addSuperinterfaces (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public final fun addType (Lcom/squareup/kotlinpoet/TypeSpec;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public fun addType (Lcom/squareup/kotlinpoet/TypeSpec;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public synthetic fun addType (Lcom/squareup/kotlinpoet/TypeSpec;)Lcom/squareup/kotlinpoet/TypeSpecHolder$Builder;
public final fun addTypeVariable (Lcom/squareup/kotlinpoet/TypeVariableName;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public final fun addTypeVariables (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public final fun addTypes (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public fun addTypes (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/TypeSpec$Builder;
public synthetic fun addTypes (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/TypeSpecHolder$Builder;
public final fun build ()Lcom/squareup/kotlinpoet/TypeSpec;
public synthetic fun contextReceivers (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/ContextReceivable$Builder;
public final fun getAnnotationSpecs ()Ljava/util/List;
Expand Down Expand Up @@ -1081,6 +1087,15 @@ public final class com/squareup/kotlinpoet/TypeSpec$Kind : java/lang/Enum {
public static fun values ()[Lcom/squareup/kotlinpoet/TypeSpec$Kind;
}

public abstract interface class com/squareup/kotlinpoet/TypeSpecHolder {
public abstract fun getTypeSpecs ()Ljava/util/List;
}

public abstract interface class com/squareup/kotlinpoet/TypeSpecHolder$Builder {
public abstract fun addType (Lcom/squareup/kotlinpoet/TypeSpec;)Lcom/squareup/kotlinpoet/TypeSpecHolder$Builder;
public fun addTypes (Ljava/lang/Iterable;)Lcom/squareup/kotlinpoet/TypeSpecHolder$Builder;
}

public final class com/squareup/kotlinpoet/TypeVariableName : com/squareup/kotlinpoet/TypeName {
public static final field Companion Lcom/squareup/kotlinpoet/TypeVariableName$Companion;
public final fun copy (ZLjava/util/List;Ljava/util/List;ZLjava/util/Map;)Lcom/squareup/kotlinpoet/TypeVariableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ import kotlin.reflect.KClass
public class FileSpec private constructor(
builder: Builder,
private val tagMap: TagMap = builder.buildTagMap(),
) : Taggable by tagMap, Annotatable {
) : Taggable by tagMap, Annotatable, TypeSpecHolder {
override val annotations: List<AnnotationSpec> = builder.annotations.toImmutableList()
override val typeSpecs: List<TypeSpec> = builder.members.filterIsInstance<TypeSpec>().toImmutableList()
public val comment: CodeBlock = builder.comment.build()
public val packageName: String = builder.packageName
public val name: String = builder.name
Expand Down Expand Up @@ -230,7 +231,7 @@ public class FileSpec private constructor(
public val packageName: String,
public val name: String,
public val isScript: Boolean,
) : Taggable.Builder<Builder>, Annotatable.Builder<Builder> {
) : Taggable.Builder<Builder>, Annotatable.Builder<Builder>, TypeSpecHolder.Builder<Builder> {
override val annotations: MutableList<AnnotationSpec> = mutableListOf()
internal val comment = CodeBlock.builder()
internal val memberImports = sortedSetOf<Import>()
Expand Down Expand Up @@ -275,14 +276,19 @@ public class FileSpec private constructor(
comment.clear()
}

public fun addType(typeSpec: TypeSpec): Builder = apply {
override fun addType(typeSpec: TypeSpec): Builder = apply {
if (isScript) {
body.add("%L", typeSpec)
} else {
members += typeSpec
}
}

//region Overrides for binary compatibility
@Suppress("RedundantOverride")
override fun addTypes(typeSpecs: Iterable<TypeSpec>): Builder = super.addTypes(typeSpecs)
//endregion

public fun addFunction(funSpec: FunSpec): Builder = apply {
require(!funSpec.isConstructor && !funSpec.isAccessor) {
"cannot add ${funSpec.name} to file $name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public class TypeSpec private constructor(
OriginatingElementsHolder by delegateOriginatingElements,
ContextReceivable by contextReceivers,
Annotatable,
Documentable {
Documentable,
TypeSpecHolder {
public val kind: Kind = builder.kind
public val name: String? = builder.name
override val kdoc: CodeBlock = builder.kdoc.build()
Expand Down Expand Up @@ -76,7 +77,7 @@ public class TypeSpec private constructor(
public val initializerBlock: CodeBlock = builder.initializerBlock.build()
public val initializerIndex: Int = builder.initializerIndex
public val funSpecs: List<FunSpec> = builder.funSpecs.toImmutableList()
public val typeSpecs: List<TypeSpec> = builder.typeSpecs.toImmutableList()
public override val typeSpecs: List<TypeSpec> = builder.typeSpecs.toImmutableList()
internal val nestedTypesSimpleNames = typeSpecs.map { it.name }.toImmutableSet()

@Deprecated("Use annotations property", ReplaceWith("annotations"), ERROR)
Expand Down Expand Up @@ -475,7 +476,8 @@ public class TypeSpec private constructor(
OriginatingElementsHolder.Builder<Builder>,
ContextReceivable.Builder<Builder>,
Annotatable.Builder<Builder>,
Documentable.Builder<Builder> {
Documentable.Builder<Builder>,
TypeSpecHolder.Builder<Builder> {
internal var primaryConstructor: FunSpec? = null
internal var superclass: TypeName = ANY
internal val initializerBlock = CodeBlock.builder()
Expand Down Expand Up @@ -725,11 +727,7 @@ public class TypeSpec private constructor(
funSpecs += funSpec
}

public fun addTypes(typeSpecs: Iterable<TypeSpec>): Builder = apply {
this.typeSpecs += typeSpecs
}

public fun addType(typeSpec: TypeSpec): Builder = apply {
override fun addType(typeSpec: TypeSpec): Builder = apply {
typeSpecs += typeSpec
}

Expand Down Expand Up @@ -764,6 +762,9 @@ public class TypeSpec private constructor(

@Suppress("RedundantOverride")
override fun addKdoc(block: CodeBlock): Builder = super.addKdoc(block)

@Suppress("RedundantOverride")
override fun addTypes(typeSpecs: Iterable<TypeSpec>): Builder = super.addTypes(typeSpecs)
//endregion

public fun build(): TypeSpec {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.kotlinpoet

/** A spec which can contain other [TypeSpec] */
public interface TypeSpecHolder {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name follows convention in the project OriginatingElementsHolder

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But differs from Annotatable. Honestly I've been mulling this change because I couldn't think of an -able name. Probably should have gone with AnnotationHolder...

public val typeSpecs: List<TypeSpec>

public interface Builder<out T : Builder<T>> {

public fun addType(typeSpec: TypeSpec): T
Copy link
Contributor Author

@drawers drawers Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we haven't provided a default implementation for addType, this interface is less like a trait than Documentable and Annotatable.

It also doesn't achieve a mutable property val typeSpecs: MutableList<TypeSpec> like Documentable and Annotatable


@Suppress("UNCHECKED_CAST")
public fun addTypes(typeSpecs: Iterable<TypeSpec>): T = apply {
for (typeSpec in typeSpecs) addType(typeSpec)
} as T
}
}