Package com.squareup.kotlinpoet

Types

AnnotationSpec
Link copied to clipboard
class AnnotationSpec : Taggable

A generated annotation on a declaration.

ClassName
Link copied to clipboard
class ClassName : TypeName, Comparable<ClassName>

A fully-qualified class name for top-level and member classes.

CodeBlock
Link copied to clipboard
class CodeBlock

A fragment of a .kt file, potentially containing declarations, statements, and documentation. Code blocks are not necessarily well-formed Kotlin code, and are not validated. This class assumes kotlinc will check correctness later!

DelicateKotlinPoetApi
Link copied to clipboard
annotation class DelicateKotlinPoetApi(message: String)

Marks declarations in the KotlinPoet API that are delicate — they have limited use-case and shall be used with care in general code. Any use of a delicate declaration has to be carefully reviewed to make sure it is properly used and does not create problems like lossy Java -> Kotlin type parsing. Carefully read documentation and message of any declaration marked as DelicateKotlinPoetApi.

Dynamic
Link copied to clipboard
object Dynamic : TypeName
FileSpec
Link copied to clipboard
class FileSpec : Taggable

A Kotlin file containing top level objects like classes, objects, functions, properties, and type aliases.

FunSpec
Link copied to clipboard
class FunSpec : Taggable, OriginatingElementsHolder

A generated function declaration.

Import
Link copied to clipboard
data class Import : Comparable<Import>
KModifier
Link copied to clipboard
enum KModifier : Enum<KModifier>
KOperator
Link copied to clipboard
enum KOperator : Enum<KOperator>
LambdaTypeName
Link copied to clipboard
class LambdaTypeName : TypeName
MemberName
Link copied to clipboard
data class MemberName

Represents the name of a member (such as a function or a property).

NameAllocator
Link copied to clipboard
class NameAllocator

Assigns Kotlin identifier names to avoid collisions, keywords, and invalid characters. To use, first create an instance and allocate all of the names that you need. Typically this is a mix of user-supplied names and constants:

OriginatingElementsHolder
Link copied to clipboard
interface OriginatingElementsHolder

A type that can have originating elements.

ParameterizedTypeName
Link copied to clipboard
class ParameterizedTypeName : TypeName
ParameterSpec
Link copied to clipboard
class ParameterSpec : Taggable

A generated parameter declaration.

PropertySpec
Link copied to clipboard
class PropertySpec : Taggable, OriginatingElementsHolder

A generated property declaration.

Taggable
Link copied to clipboard
interface Taggable

A type that can be tagged with extra metadata of the user's choice.

TypeAliasSpec
Link copied to clipboard
class TypeAliasSpec : Taggable

A generated typealias declaration

TypeName
Link copied to clipboard
sealed class TypeName : Taggable

Any type in Kotlin's type system. This class identifies simple types like Int and String, nullable types like Int?, composite types like Array<String> and Set<String>, and unassignable types like Unit.

TypeSpec
Link copied to clipboard
class TypeSpec : Taggable, OriginatingElementsHolder

A generated class, interface, or enum declaration.

TypeVariableName
Link copied to clipboard
class TypeVariableName : TypeName
WildcardTypeName
Link copied to clipboard
class WildcardTypeName : TypeName

Functions

asClassName
Link copied to clipboard
@DelicateKotlinPoetApi(message = "Java reflection APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun Class<*>.asClassName(): ClassName
@JvmName(name = "get")
fun KClass<*>.asClassName(): ClassName
@DelicateKotlinPoetApi(message = "Element APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun TypeElement.asClassName(): ClassName

Returns the class name for element.

asParameterizedTypeName
Link copied to clipboard
@DelicateKotlinPoetApi(message = "Java reflection APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun ParameterizedType.asParameterizedTypeName(): ParameterizedTypeName

Returns a parameterized type equivalent to type.

asTypeName
Link copied to clipboard
@JvmName(name = "get")
fun Type.asTypeName(): TypeName

Returns a TypeName equivalent to this Type.

@DelicateKotlinPoetApi(message = "Mirror APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun TypeMirror.asTypeName(): TypeName

Returns a TypeName equivalent to this TypeMirror.

@JvmName(name = "get")
fun KClass<*>.asTypeName(): ClassName

Returns a TypeName equivalent to this KClass.

fun KType.asTypeName(): TypeName

Returns a TypeName equivalent to the given Kotlin KType using reflection, maybe using kotlin-reflect if required.

asTypeVariableName
Link copied to clipboard
@DelicateKotlinPoetApi(message = "Element APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun TypeParameterElement.asTypeVariableName(): TypeVariableName

Returns type variable equivalent to element.

@DelicateKotlinPoetApi(message = "Java reflection APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun TypeVariable.asTypeVariableName(): TypeVariableName

Returns type variable equivalent to mirror.

fun KTypeParameter.asTypeVariableName(): TypeVariableName
asWildcardTypeName
Link copied to clipboard
@DelicateKotlinPoetApi(message = "Java reflection APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun WildcardType.asWildcardTypeName(): TypeName
@DelicateKotlinPoetApi(message = "Mirror APIs don't give complete information on Kotlin types. Consider using the kotlinpoet-metadata APIs instead.")
@JvmName(name = "get")
fun WildcardType.asWildcardTypeName(): TypeName
buildCodeBlock
Link copied to clipboard
inline fun buildCodeBlock(builderAction: CodeBlock.Builder.() -> Unit): CodeBlock

Builds new CodeBlock by populating newly created CodeBlock.Builder using provided builderAction and then converting it to CodeBlock.

joinToCode
Link copied to clipboard
fun Collection<CodeBlock>.joinToCode(separator: CharSequence = ", ", prefix: CharSequence = "", suffix: CharSequence = ""): CodeBlock
tag
Link copied to clipboard
inline fun <T : Any> Taggable.tag(): T?

Returns the tag attached with T as a key, or null if no tag is attached with that key.

inline fun <T : Any> AnnotationSpec.Builder.tag(tag: T?): AnnotationSpec.Builder
inline fun <T : Any> FileSpec.Builder.tag(tag: T?): FileSpec.Builder
inline fun <T : Any> FunSpec.Builder.tag(tag: T?): FunSpec.Builder
inline fun <T : Any> ParameterSpec.Builder.tag(tag: T?): ParameterSpec.Builder
inline fun <T : Any> PropertySpec.Builder.tag(tag: T?): PropertySpec.Builder
inline fun <T : Any> TypeAliasSpec.Builder.tag(tag: T?): TypeAliasSpec.Builder
inline fun <T : Any> TypeSpec.Builder.tag(tag: T?): TypeSpec.Builder

Attaches tag to the request using T as a key. Tags can be read from a request using Taggable.tag. Use null to remove any existing tag assigned for T.

typeNameOf
Link copied to clipboard
inline fun <T> typeNameOf(): TypeName

Returns a TypeName equivalent of the reified type parameter T using reflection, maybe using kotlin-reflect if required.

withIndent
Link copied to clipboard
inline fun CodeBlock.Builder.withIndent(builderAction: CodeBlock.Builder.() -> Unit): CodeBlock.Builder

Calls CodeBlock.Builder.indent then executes the provided builderAction on the CodeBlock.Builder and then executes CodeBlock.Builder.unindent before returning the original CodeBlock.Builder.

Properties

ANNOTATION
Link copied to clipboard
val ANNOTATION: ClassName
ANY
Link copied to clipboard
val ANY: ClassName
ARRAY
Link copied to clipboard
val ARRAY: ClassName
BOOLEAN
Link copied to clipboard
val BOOLEAN: ClassName
BOOLEAN_ARRAY
Link copied to clipboard
val BOOLEAN_ARRAY: ClassName
BYTE
Link copied to clipboard
val BYTE: ClassName
BYTE_ARRAY
Link copied to clipboard
val BYTE_ARRAY: ClassName
CHAR
Link copied to clipboard
val CHAR: ClassName
CHAR_ARRAY
Link copied to clipboard
val CHAR_ARRAY: ClassName
CHAR_SEQUENCE
Link copied to clipboard
val CHAR_SEQUENCE: ClassName
COLLECTION
Link copied to clipboard
val COLLECTION: ClassName
COMPARABLE
Link copied to clipboard
val COMPARABLE: ClassName
DOUBLE
Link copied to clipboard
val DOUBLE: ClassName
DOUBLE_ARRAY
Link copied to clipboard
val DOUBLE_ARRAY: ClassName
DYNAMIC
Link copied to clipboard
val DYNAMIC: Dynamic

Dynamic is a singleton object type, so this is a shorthand for it in Java.

ENUM
Link copied to clipboard
val ENUM: ClassName
FLOAT
Link copied to clipboard
val FLOAT: ClassName
FLOAT_ARRAY
Link copied to clipboard
val FLOAT_ARRAY: ClassName
INT
Link copied to clipboard
val INT: ClassName
INT_ARRAY
Link copied to clipboard
val INT_ARRAY: ClassName
ITERABLE
Link copied to clipboard
val ITERABLE: ClassName
LIST
Link copied to clipboard
val LIST: ClassName
LONG
Link copied to clipboard
val LONG: ClassName
LONG_ARRAY
Link copied to clipboard
val LONG_ARRAY: ClassName
MAP
Link copied to clipboard
val MAP: ClassName
MAP_ENTRY
Link copied to clipboard
val MAP_ENTRY: ClassName
MUTABLE_COLLECTION
Link copied to clipboard
val MUTABLE_COLLECTION: ClassName
MUTABLE_ITERABLE
Link copied to clipboard
val MUTABLE_ITERABLE: ClassName
MUTABLE_LIST
Link copied to clipboard
val MUTABLE_LIST: ClassName
MUTABLE_MAP
Link copied to clipboard
val MUTABLE_MAP: ClassName
MUTABLE_MAP_ENTRY
Link copied to clipboard
val MUTABLE_MAP_ENTRY: ClassName
MUTABLE_SET
Link copied to clipboard
val MUTABLE_SET: ClassName
NOTHING
Link copied to clipboard
val NOTHING: ClassName
NUMBER
Link copied to clipboard
val NUMBER: ClassName
SET
Link copied to clipboard
val SET: ClassName
SHORT
Link copied to clipboard
val SHORT: ClassName
SHORT_ARRAY
Link copied to clipboard
val SHORT_ARRAY: ClassName
STAR
Link copied to clipboard
val STAR: WildcardTypeName

The wildcard type * which is shorthand for out Any?.

STRING
Link copied to clipboard
val STRING: ClassName
THROWABLE
Link copied to clipboard
val THROWABLE: ClassName
U_BYTE
Link copied to clipboard
val U_BYTE: ClassName
U_BYTE_ARRAY
Link copied to clipboard
val U_BYTE_ARRAY: ClassName
U_INT
Link copied to clipboard
val U_INT: ClassName
U_INT_ARRAY
Link copied to clipboard
val U_INT_ARRAY: ClassName
U_LONG
Link copied to clipboard
val U_LONG: ClassName
U_LONG_ARRAY
Link copied to clipboard
val U_LONG_ARRAY: ClassName
U_SHORT
Link copied to clipboard
val U_SHORT: ClassName
U_SHORT_ARRAY
Link copied to clipboard
val U_SHORT_ARRAY: ClassName
UNIT
Link copied to clipboard
val UNIT: ClassName