diff options
Diffstat (limited to 'kotlinpoet')
-rw-r--r-- | kotlinpoet/api/kotlinpoet.api | 39 | ||||
-rw-r--r-- | kotlinpoet/build.gradle.kts | 71 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Annotatable.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/Annotatable.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/AnnotationSpec.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/AnnotationSpec.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ClassName.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/ClassName.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeBlock.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt) | 14 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ContextReceivable.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/ContextReceivable.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/DelicateKotlinPoetApi.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/DelicateKotlinPoetApi.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Documentable.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/Documentable.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Dynamic.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/Dynamic.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ExperimentalKotlinPoetApi.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/ExperimentalKotlinPoetApi.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/FileSpec.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/FileSpec.kt) | 82 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/FunSpec.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/FunSpec.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Import.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/Import.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/KModifier.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KOperator.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/KOperator.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/LambdaTypeName.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/LambdaTypeName.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/LineWrapper.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/LineWrapper.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/MemberName.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/MemberName.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/NameAllocator.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/NameAllocator.kt) | 36 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/OriginatingElementsHolder.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ParameterSpec.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterSpec.kt) | 2 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ParameterizedTypeName.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterizedTypeName.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/PropertySpec.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/PropertySpec.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Taggable.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/Taggable.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeAliasSpec.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeAliasSpec.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeName.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeName.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpec.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeSpec.kt) | 55 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpecHolder.kt | 31 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeVariableName.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeVariableName.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt) | 2 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/WildcardTypeName.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/WildcardTypeName.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/JvmAnnotations.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/jvm/JvmAnnotations.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/tags/TypeAliasTag.kt (renamed from kotlinpoet/src/main/java/com/squareup/kotlinpoet/tags/TypeAliasTag.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AbstractTypesTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/AbstractTypesTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AnnotatedTypeNameTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/AnnotatedTypeNameTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AnnotationSpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/AnnotationSpecTest.kt) | 48 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AssertThrows.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/AssertThrows.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/Cased/Weird/Sup.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/Cased/Weird/Sup.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ClassNameTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/ClassNameTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/CodeBlockTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CrossplatformTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/CrossplatformTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/DelegatedConstructorCallTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/DelegatedConstructorCallTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ExpectDeclarationsTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/ExpectDeclarationsTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ExternalDeclarationsTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/ExternalDeclarationsTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileReadingTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileReadingTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileSpecTest.kt) | 31 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileWritingTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileWritingTest.kt) | 47 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FunSpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/KotlinPoetTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LambdaTypeNameTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/LambdaTypeNameTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LineWrapperTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/LineWrapperTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LineWrappingTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/LineWrappingTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/MemberNameTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/MemberNameTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/NameAllocatorTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/NameAllocatorTest.kt) | 6 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ParameterSpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterSpecTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/PropertySpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/PropertySpecTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/StringsTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/StringsTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TaggableTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TaggableTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TestFiler.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TestFiler.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeAliasSpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeAliasSpecTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeNameKotlinTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeNameKotlinTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeSpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt) | 38 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeVariableNameTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeVariableNameTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypesEclipseTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypesEclipseTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypesTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypesTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/UtilTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/UtilTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ValueTypeSpecTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/ValueTypeSpecTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/WildcardTypeNameTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/WildcardTypeNameTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/jvm/JvmAnnotationsTest.kt (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/jvm/JvmAnnotationsTest.kt) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/JavaAnnotationSpecTest.kt | 78 | ||||
-rw-r--r-- | kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/JavaClassWithArrayValueAnnotation.java (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/JavaClassWithArrayValueAnnotation.java) | 0 | ||||
-rw-r--r-- | kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/TypeNameTest.java (renamed from kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeNameTest.java) | 0 |
75 files changed, 400 insertions, 180 deletions
diff --git a/kotlinpoet/api/kotlinpoet.api b/kotlinpoet/api/kotlinpoet.api index 114bd190..4e92873d 100644 --- a/kotlinpoet/api/kotlinpoet.api +++ b/kotlinpoet/api/kotlinpoet.api @@ -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; @@ -195,7 +195,9 @@ public final class com/squareup/kotlinpoet/FileSpec : com/squareup/kotlinpoet/An public final fun getMembers ()Ljava/util/List; public final fun getName ()Ljava/lang/String; public final fun getPackageName ()Ljava/lang/String; + public final fun getRelativePath ()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; @@ -207,13 +209,15 @@ public final class com/squareup/kotlinpoet/FileSpec : com/squareup/kotlinpoet/An public static synthetic fun toBuilder$default (Lcom/squareup/kotlinpoet/FileSpec;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Lcom/squareup/kotlinpoet/FileSpec$Builder; public final fun toJavaFileObject ()Ljavax/tools/JavaFileObject; public fun toString ()Ljava/lang/String; - public final fun writeTo (Ljava/io/File;)V + public final fun writeTo (Ljava/io/File;)Ljava/io/File; + public final synthetic fun writeTo (Ljava/io/File;)V public final fun writeTo (Ljava/lang/Appendable;)V - public final fun writeTo (Ljava/nio/file/Path;)V + public final fun writeTo (Ljava/nio/file/Path;)Ljava/nio/file/Path; + public final synthetic fun writeTo (Ljava/nio/file/Path;)V 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; @@ -251,8 +255,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; @@ -554,6 +561,7 @@ public final class com/squareup/kotlinpoet/MemberName$Companion { public final class com/squareup/kotlinpoet/NameAllocator { public fun <init> ()V + public fun <init> (Z)V public final fun copy ()Lcom/squareup/kotlinpoet/NameAllocator; public final fun get (Ljava/lang/Object;)Ljava/lang/String; public final fun newName (Ljava/lang/String;)Ljava/lang/String; @@ -925,7 +933,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; @@ -959,7 +967,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; @@ -981,7 +989,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; @@ -1023,10 +1031,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; @@ -1081,6 +1091,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; diff --git a/kotlinpoet/build.gradle.kts b/kotlinpoet/build.gradle.kts index 9627b60a..48669b33 100644 --- a/kotlinpoet/build.gradle.kts +++ b/kotlinpoet/build.gradle.kts @@ -13,40 +13,59 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -tasks.jar { - manifest { - attributes("Automatic-Module-Name" to "com.squareup.kotlinpoet") - } -} - -tasks.compileTestKotlin { - compilerOptions { - freeCompilerArgs.add("-opt-in=com.squareup.kotlinpoet.DelicateKotlinPoetApi") - } +plugins { + kotlin("multiplatform") } spotless { kotlin { targetExclude( // Non-Square licensed files - "src/main/java/com/squareup/kotlinpoet/ClassName.kt", - "src/test/java/com/squareup/kotlinpoet/AbstractTypesTest.kt", - "src/test/java/com/squareup/kotlinpoet/ClassNameTest.kt", - "src/test/java/com/squareup/kotlinpoet/TypesEclipseTest.kt", - "src/test/java/com/squareup/kotlinpoet/TypesTest.kt", + "src/commonMain/kotlin/com/squareup/kotlinpoet/ClassName.kt", + "src/commonTest/kotlin/com/squareup/kotlinpoet/AbstractTypesTest.kt", + "src/commonTest/kotlin/com/squareup/kotlinpoet/ClassNameTest.kt", + "src/commonTest/kotlin/com/squareup/kotlinpoet/TypesEclipseTest.kt", + "src/commonTest/kotlin/com/squareup/kotlinpoet/TypesTest.kt", ) } } -dependencies { - implementation(libs.kotlin.reflect) - testImplementation(libs.kotlin.junit) - testImplementation(libs.truth) - testImplementation(libs.guava) - testImplementation(libs.compileTesting) - testImplementation(libs.jimfs) - testImplementation(libs.ecj) - testImplementation(libs.kotlinCompileTesting) - testImplementation(libs.kotlin.annotationProcessingEmbeddable) - testImplementation(libs.kotlin.compilerEmbeddable) +kotlin { + jvm { + withJava() + } + + sourceSets { + val commonMain by getting { + dependencies { + implementation(libs.kotlin.reflect) + } + } + val commonTest by getting { + dependencies { + implementation(libs.kotlin.junit) + implementation(libs.truth) + implementation(libs.guava) + implementation(libs.compileTesting) + implementation(libs.jimfs) + implementation(libs.ecj) + implementation(libs.kotlinCompileTesting) + implementation(libs.kotlin.annotationProcessingEmbeddable) + implementation(libs.kotlin.compilerEmbeddable) + } + } + } +} + +tasks.withType(org.gradle.jvm.tasks.Jar::class.java) { + manifest { + attributes("Automatic-Module-Name" to "com.squareup.kotlinpoet") + } } + +tasks.named<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>("compileTestKotlinJvm") { + compilerOptions { + freeCompilerArgs.add("-opt-in=com.squareup.kotlinpoet.DelicateKotlinPoetApi") + } +} + diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Annotatable.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Annotatable.kt index 25533105..25533105 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Annotatable.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Annotatable.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/AnnotationSpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/AnnotationSpec.kt index a9390923..a9390923 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/AnnotationSpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/AnnotationSpec.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ClassName.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ClassName.kt index 5eeb21b0..5eeb21b0 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ClassName.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ClassName.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeBlock.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt index 6a99631c..6a99631c 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeBlock.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeBlock.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt index ff1de015..d4489f24 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/CodeWriter.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt @@ -445,8 +445,8 @@ internal class CodeWriter constructor( return className.canonicalName } - // If the class is in the same package, we're done. - if (packageName == className.packageName) { + // If the class is in the same package and there's no import alias for that class, we're done. + if (packageName == className.packageName && imports[className.canonicalName]?.alias == null) { referencedNames.add(className.topLevelClassName().simpleName) return className.simpleNames.joinToString(".") } @@ -722,11 +722,11 @@ internal class CodeWriter constructor( importsCollector.close() return CodeWriter( - out, - indent, - memberImports + generatedImports.filterKeys { it !in memberImports }, - suggestedTypeImports, - suggestedMemberImports, + out = out, + indent = indent, + imports = memberImports + generatedImports.filterKeys { it !in memberImports }, + importedTypes = suggestedTypeImports, + importedMembers = suggestedMemberImports, ) } diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ContextReceivable.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ContextReceivable.kt index 929096bc..929096bc 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ContextReceivable.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ContextReceivable.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/DelicateKotlinPoetApi.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/DelicateKotlinPoetApi.kt index 11369334..11369334 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/DelicateKotlinPoetApi.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/DelicateKotlinPoetApi.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Documentable.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Documentable.kt index 3dff284e..3dff284e 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Documentable.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Documentable.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Dynamic.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Dynamic.kt index 413b7b7a..413b7b7a 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Dynamic.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Dynamic.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ExperimentalKotlinPoetApi.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ExperimentalKotlinPoetApi.kt index 2e58556d..2e58556d 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ExperimentalKotlinPoetApi.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ExperimentalKotlinPoetApi.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/FileSpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/FileSpec.kt index ef3f032c..288848c9 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/FileSpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/FileSpec.kt @@ -20,16 +20,19 @@ import java.io.ByteArrayInputStream import java.io.File import java.io.IOException import java.io.InputStream -import java.io.OutputStreamWriter import java.net.URI import java.nio.charset.StandardCharsets.UTF_8 -import java.nio.file.Files import java.nio.file.Path import javax.annotation.processing.Filer import javax.tools.JavaFileObject import javax.tools.JavaFileObject.Kind import javax.tools.SimpleJavaFileObject import javax.tools.StandardLocation +import kotlin.DeprecationLevel.HIDDEN +import kotlin.io.path.createDirectories +import kotlin.io.path.isDirectory +import kotlin.io.path.notExists +import kotlin.io.path.outputStream import kotlin.reflect.KClass /** @@ -46,8 +49,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 @@ -59,6 +63,20 @@ public class FileSpec private constructor( private val indent = builder.indent private val extension = if (isScript) "kts" else "kt" + /** + * The relative path of the file which would be produced by a call to [writeTo]. + * This value always uses unix-style path separators (`/`). + */ + public val relativePath: String = buildString { + for (packageComponent in packageName.split('.').dropLastWhile { it.isEmpty() }) { + append(packageComponent) + append('/') + } + append(name) + append('.') + append(extension) + } + @Throws(IOException::class) public fun writeTo(out: Appendable) { val codeWriter = CodeWriter.withCollectedImports( @@ -71,28 +89,39 @@ public class FileSpec private constructor( codeWriter.close() } - /** Writes this to `directory` as UTF-8 using the standard directory structure. */ + @Deprecated("", level = HIDDEN) + @JvmName("writeTo") // For binary compatibility. + public fun oldWriteTo(directory: Path) { + writeTo(directory) + } + + /** + * Writes this to [directory] as UTF-8 using the standard directory structure + * and returns the newly output path. + */ @Throws(IOException::class) - public fun writeTo(directory: Path) { - require(Files.notExists(directory) || Files.isDirectory(directory)) { + public fun writeTo(directory: Path): Path { + require(directory.notExists() || directory.isDirectory()) { "path $directory exists but is not a directory." } - var outputDirectory = directory - if (packageName.isNotEmpty()) { - for (packageComponent in packageName.split('.').dropLastWhile { it.isEmpty() }) { - outputDirectory = outputDirectory.resolve(packageComponent) - } - } - - Files.createDirectories(outputDirectory) + val outputPath = directory.resolve(relativePath) + outputPath.parent.createDirectories() + outputPath.outputStream().bufferedWriter().use(::writeTo) + return outputPath + } - val outputPath = outputDirectory.resolve("$name.$extension") - OutputStreamWriter(Files.newOutputStream(outputPath), UTF_8).use { writer -> writeTo(writer) } + @Deprecated("", level = HIDDEN) + @JvmName("writeTo") // For binary compatibility. + public fun oldWriteTo(directory: File) { + writeTo(directory) } - /** Writes this to `directory` as UTF-8 using the standard directory structure. */ + /** + * Writes this to [directory] as UTF-8 using the standard directory structure + * and returns the newly output file. + */ @Throws(IOException::class) - public fun writeTo(directory: File): Unit = writeTo(directory.toPath()) + public fun writeTo(directory: File): File = writeTo(directory.toPath()).toFile() /** Writes this to `filer`. */ @Throws(IOException::class) @@ -191,13 +220,7 @@ public class FileSpec private constructor( override fun toString(): String = buildString { writeTo(this) } public fun toJavaFileObject(): JavaFileObject { - val uri = URI.create( - if (packageName.isEmpty()) { - name - } else { - packageName.replace('.', '/') + '/' + name - } + ".$extension", - ) + val uri = URI.create(relativePath) return object : SimpleJavaFileObject(uri, Kind.SOURCE) { private val lastModified = System.currentTimeMillis() override fun getCharContent(ignoreEncodingErrors: Boolean): String { @@ -230,7 +253,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>() @@ -275,7 +298,7 @@ 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 { @@ -283,6 +306,11 @@ public class FileSpec private constructor( } } + //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" diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/FunSpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/FunSpec.kt index f42308ea..f42308ea 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/FunSpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/FunSpec.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Import.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Import.kt index 59b6b12e..59b6b12e 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Import.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Import.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/KModifier.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt index 81d03570..81d03570 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/KModifier.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KModifier.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/KOperator.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KOperator.kt index 461a39f2..461a39f2 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/KOperator.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/KOperator.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/LambdaTypeName.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/LambdaTypeName.kt index 9f462f81..9f462f81 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/LambdaTypeName.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/LambdaTypeName.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/LineWrapper.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/LineWrapper.kt index 3a1e9a6b..3a1e9a6b 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/LineWrapper.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/LineWrapper.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/MemberName.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/MemberName.kt index c70a10ac..c70a10ac 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/MemberName.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/MemberName.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/NameAllocator.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/NameAllocator.kt index cda10042..a6c570b9 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/NameAllocator.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/NameAllocator.kt @@ -77,7 +77,39 @@ public class NameAllocator private constructor( private val allocatedNames: MutableSet<String>, private val tagToName: MutableMap<Any, String>, ) { - public constructor() : this(mutableSetOf(), mutableMapOf()) + public constructor() : this(preallocateKeywords = true) + + /** + * @param preallocateKeywords If true, all Kotlin keywords will be preallocated. Requested names which + * collide with keywords will be suffixed with underscores to avoid being used as identifiers: + * + * ```kotlin + * val nameAllocator = NameAllocator(preallocateKeywords = true) + * println(nameAllocator.newName("when")) // prints "when_" + * ``` + * + * If false, keywords will not get any special treatment: + * + * ```kotlin + * val nameAllocator = NameAllocator(preallocateKeywords = false) + * println(nameAllocator.newName("when")) // prints "when" + * ``` + * + * Note that you can use the `%N` placeholder when emitting a name produced by [NameAllocator] to + * ensure it's properly escaped for use as an identifier: + * + * ```kotlin + * val nameAllocator = NameAllocator(preallocateKeywords = false) + * println(CodeBlock.of("%N", nameAllocator.newName("when"))) // prints "`when`" + * ``` + * + * The default behaviour of [NameAllocator] is to preallocate keywords - this is the behaviour you'll + * get when using the no-arg constructor. + */ + public constructor(preallocateKeywords: Boolean) : this( + allocatedNames = if (preallocateKeywords) KEYWORDS.toMutableSet() else mutableSetOf(), + tagToName = mutableMapOf(), + ) /** * Return a new name using `suggestion` that will not be a Java identifier or clash with other @@ -89,7 +121,7 @@ public class NameAllocator private constructor( tag: Any = UUID.randomUUID().toString(), ): String { var result = toJavaIdentifier(suggestion) - while (result.isKeyword || !allocatedNames.add(result)) { + while (!allocatedNames.add(result)) { result += "_" } diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/OriginatingElementsHolder.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt index 4e877742..4e877742 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/OriginatingElementsHolder.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/OriginatingElementsHolder.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterSpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ParameterSpec.kt index 15cf4bf0..91dc8b06 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterSpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ParameterSpec.kt @@ -54,10 +54,8 @@ public class ParameterSpec private constructor( internal fun emit( codeWriter: CodeWriter, includeType: Boolean = true, - emitKdoc: Boolean = false, inlineAnnotations: Boolean = true, ) { - if (emitKdoc) codeWriter.emitKdoc(kdoc.ensureEndsWithNewLine()) codeWriter.emitAnnotations(annotations, inlineAnnotations) codeWriter.emitModifiers(modifiers) if (name.isNotEmpty()) codeWriter.emitCode("%N", this) diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterizedTypeName.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ParameterizedTypeName.kt index b6bdf798..b6bdf798 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/ParameterizedTypeName.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/ParameterizedTypeName.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/PropertySpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/PropertySpec.kt index 9ba9409c..9ba9409c 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/PropertySpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/PropertySpec.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Taggable.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Taggable.kt index f3313aea..f3313aea 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Taggable.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Taggable.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeAliasSpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeAliasSpec.kt index f9a607fb..f9a607fb 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeAliasSpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeAliasSpec.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeName.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeName.kt index 60dea8bf..60dea8bf 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeName.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeName.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeSpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpec.kt index 5f52e4ff..af0613b3 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeSpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpec.kt @@ -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() @@ -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) @@ -208,7 +209,7 @@ public class TypeSpec private constructor( ) param.emitDefaultValue(codeWriter) } else { - param.emit(codeWriter, emitKdoc = true, inlineAnnotations = false) + param.emit(codeWriter, inlineAnnotations = false) } } @@ -373,31 +374,25 @@ public class TypeSpec private constructor( /** * Returns KDoc comments including those of the primary constructor and its parameters. * - * If the primary constructor parameter is not mapped to a property, or if the property doesn't - * have its own KDoc - the parameter's KDoc will be attached to the parameter. Otherwise, if both - * the parameter and the property have KDoc - the property's KDoc will be attached to the - * property/parameter, and the parameter's KDoc will be printed in the type header. + * Parameters' KDocs, if present, will always be printed in the type header. */ private fun kdocWithConstructorDocs(): CodeBlock { - if (primaryConstructor == null) { - return kdoc.ensureEndsWithNewLine() - } - val constructorProperties = constructorProperties() - val parametersWithKdoc = primaryConstructor.parameters.filter { parameter -> - val propertyKdoc = constructorProperties[parameter.name]?.kdoc ?: CodeBlock.EMPTY - return@filter parameter.kdoc.isNotEmpty() && propertyKdoc.isNotEmpty() && - parameter.kdoc != propertyKdoc - } - return with(kdoc.ensureEndsWithNewLine().toBuilder()) { - if (kdoc.isNotEmpty()) add("\n") - if (primaryConstructor.kdoc.isNotEmpty()) { - add("@constructor %L", primaryConstructor.kdoc.ensureEndsWithNewLine()) - } - parametersWithKdoc.forEach { parameter -> - add("@param %L %L", parameter.name, parameter.kdoc.ensureEndsWithNewLine()) + val classKdoc = kdoc.ensureEndsWithNewLine() + val constructorKdoc = buildCodeBlock { + if (primaryConstructor != null) { + if (primaryConstructor.kdoc.isNotEmpty()) { + add("@constructor %L", primaryConstructor.kdoc.ensureEndsWithNewLine()) + } + primaryConstructor.parameters.forEach { parameter -> + if (parameter.kdoc.isNotEmpty()) { + add("@param %L %L", parameter.name, parameter.kdoc.ensureEndsWithNewLine()) + } + } } - build() } + return listOf(classKdoc, constructorKdoc) + .filter(CodeBlock::isNotEmpty) + .joinToCode(separator = "\n") } private val hasInitializer: Boolean get() = initializerIndex != -1 && initializerBlock.isNotEmpty() @@ -475,7 +470,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() @@ -725,11 +721,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 } @@ -764,6 +756,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 { diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpecHolder.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpecHolder.kt new file mode 100644 index 00000000..aad67e63 --- /dev/null +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpecHolder.kt @@ -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 { + public val typeSpecs: List<TypeSpec> + + public interface Builder<out T : Builder<T>> { + + public fun addType(typeSpec: TypeSpec): T + + @Suppress("UNCHECKED_CAST") + public fun addTypes(typeSpecs: Iterable<TypeSpec>): T = apply { + for (typeSpec in typeSpecs) addType(typeSpec) + } as T + } +} diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeVariableName.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeVariableName.kt index 3313b2c9..3313b2c9 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/TypeVariableName.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeVariableName.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt index 16a443dd..c226f397 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/Util.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/Util.kt @@ -169,7 +169,7 @@ private val IDENTIFIER_REGEX = internal val String.isIdentifier get() = IDENTIFIER_REGEX.matches(this) // https://kotlinlang.org/docs/reference/keyword-reference.html -private val KEYWORDS = setOf( +internal val KEYWORDS = setOf( // Hard keywords "as", "break", diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/WildcardTypeName.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/WildcardTypeName.kt index bd1e0965..bd1e0965 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/WildcardTypeName.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/WildcardTypeName.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/jvm/JvmAnnotations.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/JvmAnnotations.kt index fcaae681..fcaae681 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/jvm/JvmAnnotations.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/jvm/JvmAnnotations.kt diff --git a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/tags/TypeAliasTag.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/tags/TypeAliasTag.kt index 22be037b..22be037b 100644 --- a/kotlinpoet/src/main/java/com/squareup/kotlinpoet/tags/TypeAliasTag.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/tags/TypeAliasTag.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AbstractTypesTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AbstractTypesTest.kt index b78a96e8..b78a96e8 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AbstractTypesTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AbstractTypesTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AnnotatedTypeNameTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AnnotatedTypeNameTest.kt index 3e7b766c..3e7b766c 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AnnotatedTypeNameTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AnnotatedTypeNameTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AnnotationSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AnnotationSpecTest.kt index fff366f3..b35e65f2 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AnnotationSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AnnotationSpecTest.kt @@ -345,32 +345,6 @@ class AnnotationSpecTest { ) } - @Test fun getOnValueArrayTypeMirrorShouldNameValueArg() { - val myClazz = compilation.elements - .getTypeElement(JavaClassWithArrayValueAnnotation::class.java.canonicalName) - val classBuilder = TypeSpec.classBuilder("Result") - - myClazz.annotationMirrors.map { AnnotationSpec.get(it) } - .forEach { - classBuilder.addAnnotation(it) - } - - assertThat(toString(classBuilder.build())).isEqualTo( - """ - |package com.squareup.tacos - | - |import com.squareup.kotlinpoet.JavaClassWithArrayValueAnnotation - |import java.lang.Boolean - |import java.lang.Object - | - |@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = arrayOf(Object::class, - | Boolean::class)) - |public class Result - | - """.trimMargin(), - ) - } - @Test fun getOnVarargMirrorShouldNameValueArg() { val myClazz = compilation.elements .getTypeElement(KotlinClassWithVarargAnnotation::class.java.canonicalName) @@ -399,28 +373,6 @@ class AnnotationSpecTest { ) } - @Test fun getOnValueArrayTypeAnnotationShouldNameValueArg() { - val annotation = JavaClassWithArrayValueAnnotation::class.java.getAnnotation( - JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue::class.java, - ) - val classBuilder = TypeSpec.classBuilder("Result") - .addAnnotation(AnnotationSpec.get(annotation)) - - assertThat(toString(classBuilder.build()).trim()).isEqualTo( - """ - |package com.squareup.tacos - | - |import com.squareup.kotlinpoet.JavaClassWithArrayValueAnnotation - |import java.lang.Boolean - |import java.lang.Object - | - |@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = arrayOf(Object::class, - | Boolean::class)) - |public class Result - """.trimMargin(), - ) - } - @Test fun getOnVarargAnnotationShouldNameValueArg() { val annotation = KotlinClassWithVarargAnnotation::class.java .getAnnotation(AnnotationWithArrayValue::class.java) diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AssertThrows.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AssertThrows.kt index facd7022..facd7022 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/AssertThrows.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/AssertThrows.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/Cased/Weird/Sup.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/Cased/Weird/Sup.kt index 8d3f439b..8d3f439b 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/Cased/Weird/Sup.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/Cased/Weird/Sup.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ClassNameTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ClassNameTest.kt index 92e4f144..92e4f144 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ClassNameTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ClassNameTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/CodeBlockTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt index be4e62dd..be4e62dd 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/CodeBlockTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CodeBlockTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/CrossplatformTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CrossplatformTest.kt index cc119be5..cc119be5 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/CrossplatformTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/CrossplatformTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/DelegatedConstructorCallTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/DelegatedConstructorCallTest.kt index 80a90358..80a90358 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/DelegatedConstructorCallTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/DelegatedConstructorCallTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ExpectDeclarationsTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ExpectDeclarationsTest.kt index 00b0c88c..00b0c88c 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ExpectDeclarationsTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ExpectDeclarationsTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ExternalDeclarationsTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ExternalDeclarationsTest.kt index 8cc5f496..8cc5f496 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ExternalDeclarationsTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ExternalDeclarationsTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileReadingTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileReadingTest.kt index 35719cf8..35719cf8 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileReadingTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileReadingTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt index c363f592..a855cddc 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt @@ -500,6 +500,37 @@ class FileSpecTest { ) } + // https://github.com/square/kotlinpoet/issues/1696 + @Test fun aliasedImportInSamePackage() { + val packageName = "com.mypackage" + val className = ClassName(packageName, "StringKey") + val source = FileSpec.builder(packageName, "K") + .addAliasedImport(className, "S") + .addType( + TypeSpec + .objectBuilder("K") + .addProperty( + PropertySpec.builder("test", className) + .initializer("%T(%L)", className, 0) + .build(), + ) + .build(), + ) + .build() + assertThat(source.toString()).isEqualTo( + """ + |package com.mypackage + | + |import com.mypackage.StringKey as S + | + |public object K { + | public val test: S = S(0) + |} + | + """.trimMargin(), + ) + } + @Test fun conflictingParentName() { val source = FileSpec.builder("com.squareup.tacos", "A") .addType( diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileWritingTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileWritingTest.kt index 83574f31..0e79ac7e 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FileWritingTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileWritingTest.kt @@ -69,37 +69,46 @@ class FileWritingTest { @Test fun pathDefaultPackage() { val type = TypeSpec.classBuilder("Test").build() - FileSpec.get("", type).writeTo(fsRoot) + val fileSpec = FileSpec.get("", type) + assertThat(fileSpec.relativePath).isEqualTo("Test.kt") - val testPath = fsRoot.resolve("Test.kt") + val testPath = fileSpec.writeTo(fsRoot) + assertThat(testPath).isEqualTo(fsRoot.resolve("Test.kt")) assertThat(Files.exists(testPath)).isTrue() } @Test fun pathDefaultPackageWithSubdirectory() { val type = TypeSpec.classBuilder("Test").build() - FileSpec.get("", type).writeTo(fsRoot.resolve("sub")) + val testPath = FileSpec.get("", type).writeTo(fsRoot.resolve("sub")) - val testPath = fsRoot.resolve("sub/Test.kt") + assertThat(testPath).isEqualTo(fsRoot.resolve("sub/Test.kt")) assertThat(Files.exists(testPath)).isTrue() } @Test fun fileDefaultPackage() { val type = TypeSpec.classBuilder("Test").build() - FileSpec.get("", type).writeTo(tmp.root) + val testFile = FileSpec.get("", type).writeTo(tmp.root) - val testFile = File(tmp.root, "Test.kt") + assertThat(testFile).isEqualTo(File(tmp.root, "Test.kt")) assertThat(testFile.exists()).isTrue() } @Test fun pathNestedClasses() { val type = TypeSpec.classBuilder("Test").build() - FileSpec.get("foo", type).writeTo(fsRoot) - FileSpec.get("foo.bar", type).writeTo(fsRoot) - FileSpec.get("foo.bar.baz", type).writeTo(fsRoot) - - val fooPath = fsRoot.resolve(fs.getPath("foo", "Test.kt")) - val barPath = fsRoot.resolve(fs.getPath("foo", "bar", "Test.kt")) - val bazPath = fsRoot.resolve(fs.getPath("foo", "bar", "baz", "Test.kt")) + val fooSpec = FileSpec.get("foo", type) + val barSpec = FileSpec.get("foo.bar", type) + val bazSpec = FileSpec.get("foo.bar.baz", type) + assertThat(fooSpec.relativePath).isEqualTo("foo/Test.kt") + assertThat(barSpec.relativePath).isEqualTo("foo/bar/Test.kt") + assertThat(bazSpec.relativePath).isEqualTo("foo/bar/baz/Test.kt") + + val fooPath = fooSpec.writeTo(fsRoot) + val barPath = barSpec.writeTo(fsRoot) + val bazPath = bazSpec.writeTo(fsRoot) + + assertThat(fooPath).isEqualTo(fsRoot.resolve(fs.getPath("foo", "Test.kt"))) + assertThat(barPath).isEqualTo(fsRoot.resolve(fs.getPath("foo", "bar", "Test.kt"))) + assertThat(bazPath).isEqualTo(fsRoot.resolve(fs.getPath("foo", "bar", "baz", "Test.kt"))) assertThat(Files.exists(fooPath)).isTrue() assertThat(Files.exists(barPath)).isTrue() assertThat(Files.exists(bazPath)).isTrue() @@ -107,16 +116,16 @@ class FileWritingTest { @Test fun fileNestedClasses() { val type = TypeSpec.classBuilder("Test").build() - FileSpec.get("foo", type).writeTo(tmp.root) - FileSpec.get("foo.bar", type).writeTo(tmp.root) - FileSpec.get("foo.bar.baz", type).writeTo(tmp.root) + val fooFile = FileSpec.get("foo", type).writeTo(tmp.root) + val barFile = FileSpec.get("foo.bar", type).writeTo(tmp.root) + val bazFile = FileSpec.get("foo.bar.baz", type).writeTo(tmp.root) val fooDir = File(tmp.root, "foo") - val fooFile = File(fooDir, "Test.kt") + assertThat(fooFile).isEqualTo(File(fooDir, "Test.kt")) val barDir = File(fooDir, "bar") - val barFile = File(barDir, "Test.kt") + assertThat(barFile).isEqualTo(File(barDir, "Test.kt")) val bazDir = File(barDir, "baz") - val bazFile = File(bazDir, "Test.kt") + assertThat(bazFile).isEqualTo(File(bazDir, "Test.kt")) assertThat(fooFile.exists()).isTrue() assertThat(barFile.exists()).isTrue() assertThat(bazFile.exists()).isTrue() diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FunSpecTest.kt index 4fff9fe3..4fff9fe3 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FunSpecTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/KotlinPoetTest.kt index 642b65c5..642b65c5 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/KotlinPoetTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/LambdaTypeNameTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LambdaTypeNameTest.kt index 1bcaef4a..1bcaef4a 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/LambdaTypeNameTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LambdaTypeNameTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/LineWrapperTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LineWrapperTest.kt index 8fdbda83..8fdbda83 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/LineWrapperTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LineWrapperTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/LineWrappingTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LineWrappingTest.kt index 3471607e..3471607e 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/LineWrappingTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/LineWrappingTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/MemberNameTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/MemberNameTest.kt index 69271d7d..69271d7d 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/MemberNameTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/MemberNameTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/NameAllocatorTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/NameAllocatorTest.kt index 28c7d34d..a61877c0 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/NameAllocatorTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/NameAllocatorTest.kt @@ -70,6 +70,12 @@ class NameAllocatorTest { assertThat(nameAllocator[1]).isEqualTo("when_") } + @Test fun kotlinKeywordNotPreAllocated() { + val nameAllocator = NameAllocator(preallocateKeywords = false) + assertThat(nameAllocator.newName("when", 1)).isEqualTo("when") + assertThat(nameAllocator[1]).isEqualTo("when") + } + @Test fun tagReuseForbidden() { val nameAllocator = NameAllocator() nameAllocator.newName("foo", 1) diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ParameterSpecTest.kt index 5528e493..5528e493 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ParameterSpecTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt index a94dddd5..a94dddd5 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ParameterizedTypeNameTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/PropertySpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/PropertySpecTest.kt index af373c25..af373c25 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/PropertySpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/PropertySpecTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/StringsTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/StringsTest.kt index d25ca4ed..d25ca4ed 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/StringsTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/StringsTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TaggableTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TaggableTest.kt index 504798b6..504798b6 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TaggableTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TaggableTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TestFiler.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TestFiler.kt index bb24d564..bb24d564 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TestFiler.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TestFiler.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeAliasSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeAliasSpecTest.kt index f5c7eb22..f5c7eb22 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeAliasSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeAliasSpecTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeNameKotlinTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeNameKotlinTest.kt index 3c16242a..3c16242a 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeNameKotlinTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeNameKotlinTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeSpecTest.kt index ebac6356..94732b61 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeSpecTest.kt @@ -1975,14 +1975,12 @@ class TypeSpecTest { | * [random][java.util.Random] tex-mex stuff we could find in the pantry | * and some [kotlin.String] cheese. | * + | * @param temperature Taco temperature. Can be as cold as the famous ice tacos from + | * the Andes, or hot with lava-like cheese from the depths of + | * the Ninth Circle. | * @param mild Whether the taco is mild (ew) or crunchy (ye). | */ |public class Taco( - | /** - | * Taco temperature. Can be as cold as the famous ice tacos from - | * the Andes, or hot with lava-like cheese from the depths of - | * the Ninth Circle. - | */ | temperature: Double, | /** | * True for a soft flour tortilla; false for a crunchy corn tortilla. @@ -4805,6 +4803,9 @@ class TypeSpecTest { | * This is a thing for stuff. | * | * @constructor Construct a thing! + | * @param first the first thing + | * @param second the second thing + | * @param third the third thing | */ |public class MyType( | /** @@ -4839,10 +4840,10 @@ class TypeSpecTest { .build() assertThat(typeSpec.toString()).isEqualTo( """ + |/** + | * @param first the first thing + | */ |public class MyType( - | /** - | * the first thing - | */ | first: kotlin.Int, |) | @@ -5615,6 +5616,27 @@ class TypeSpecTest { ) } + @Test fun classKdoc() { + val type = TypeSpec.classBuilder("MyClass") + .addKdoc("This is my class") + .primaryConstructor( + FunSpec.constructorBuilder() + .build(), + ) + .build() + + //language=kotlin + assertThat(type.toString()).isEqualTo( + """ + /** + * This is my class + */ + public class MyClass() + + """.trimIndent(), + ) + } + // https://github.com/square/kotlinpoet/issues/1630 @Test fun primaryConstructorKDoc() { val type = TypeSpec.classBuilder("MyClass") diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeVariableNameTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeVariableNameTest.kt index 628b569e..628b569e 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeVariableNameTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeVariableNameTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypesEclipseTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypesEclipseTest.kt index a3ae52b5..a3ae52b5 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypesEclipseTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypesEclipseTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypesTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypesTest.kt index 43bdbb7c..43bdbb7c 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypesTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypesTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/UtilTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/UtilTest.kt index f8bfc0b9..f8bfc0b9 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/UtilTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/UtilTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ValueTypeSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ValueTypeSpecTest.kt index 363b9898..363b9898 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/ValueTypeSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/ValueTypeSpecTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/WildcardTypeNameTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/WildcardTypeNameTest.kt index db42d003..db42d003 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/WildcardTypeNameTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/WildcardTypeNameTest.kt diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/jvm/JvmAnnotationsTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/jvm/JvmAnnotationsTest.kt index 66147363..66147363 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/jvm/JvmAnnotationsTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/jvm/JvmAnnotationsTest.kt diff --git a/kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/JavaAnnotationSpecTest.kt b/kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/JavaAnnotationSpecTest.kt new file mode 100644 index 00000000..4b265964 --- /dev/null +++ b/kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/JavaAnnotationSpecTest.kt @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2015 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 + +import com.google.common.truth.Truth.assertThat +import com.google.testing.compile.CompilationRule +import kotlin.test.Test +import org.junit.Rule + +class JavaAnnotationSpecTest { + + @Rule @JvmField + val compilation = CompilationRule() + + @Test fun getOnValueArrayTypeMirrorShouldNameValueArg() { + val myClazz = compilation.elements + .getTypeElement(JavaClassWithArrayValueAnnotation::class.java.canonicalName) + val classBuilder = TypeSpec.classBuilder("Result") + + myClazz.annotationMirrors.map { AnnotationSpec.get(it) } + .forEach { + classBuilder.addAnnotation(it) + } + + assertThat(toString(classBuilder.build())).isEqualTo( + """ + |package com.squareup.tacos + | + |import com.squareup.kotlinpoet.JavaClassWithArrayValueAnnotation + |import java.lang.Boolean + |import java.lang.Object + | + |@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = arrayOf(Object::class, + | Boolean::class)) + |public class Result + | + """.trimMargin(), + ) + } + + @Test fun getOnValueArrayTypeAnnotationShouldNameValueArg() { + val annotation = JavaClassWithArrayValueAnnotation::class.java.getAnnotation( + JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue::class.java, + ) + val classBuilder = TypeSpec.classBuilder("Result") + .addAnnotation(AnnotationSpec.get(annotation)) + + assertThat(toString(classBuilder.build()).trim()).isEqualTo( + """ + |package com.squareup.tacos + | + |import com.squareup.kotlinpoet.JavaClassWithArrayValueAnnotation + |import java.lang.Boolean + |import java.lang.Object + | + |@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = arrayOf(Object::class, + | Boolean::class)) + |public class Result + """.trimMargin(), + ) + } + + private fun toString(typeSpec: TypeSpec) = + FileSpec.get("com.squareup.tacos", typeSpec).toString() +} diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/JavaClassWithArrayValueAnnotation.java b/kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/JavaClassWithArrayValueAnnotation.java index c48b3a4c..c48b3a4c 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/JavaClassWithArrayValueAnnotation.java +++ b/kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/JavaClassWithArrayValueAnnotation.java diff --git a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeNameTest.java b/kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/TypeNameTest.java index 70019f9d..70019f9d 100644 --- a/kotlinpoet/src/test/java/com/squareup/kotlinpoet/TypeNameTest.java +++ b/kotlinpoet/src/jvmTest/java/com/squareup/kotlinpoet/TypeNameTest.java |