All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.squareup.kotlinpoet.Annotatable.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * 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

import kotlin.reflect.KClass

/** A spec or type which can be annotated. */
public interface Annotatable {
  public val annotations: List

  public interface Builder> {
    public val annotations: MutableList

    @Suppress("UNCHECKED_CAST")
    public fun addAnnotation(annotationSpec: AnnotationSpec): T = apply {
      annotations += annotationSpec
    } as T

    @Suppress("UNCHECKED_CAST")
    public fun addAnnotations(annotationSpecs: Iterable): T = apply {
      for (annotationSpec in annotationSpecs) {
        addAnnotation(annotationSpec)
      }
    } as T

    public fun addAnnotation(annotation: ClassName): T = addAnnotation(AnnotationSpec.builder(annotation).build())

    @DelicateKotlinPoetApi(
      message = "Java reflection APIs don't give complete information on Kotlin types. Consider " +
        "using the kotlinpoet-metadata APIs instead.",
    )
    public fun addAnnotation(annotation: Class<*>): T = addAnnotation(annotation.asClassName())

    public fun addAnnotation(annotation: KClass<*>): T = addAnnotation(annotation.asClassName())
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy