1 /*
2  * Copyright (C) 2018 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 @file:JvmName("JvmAnnotations")
17 
18 package com.squareup.kotlinpoet.jvm
19 
20 import com.squareup.kotlinpoet.AnnotationSpec
21 import com.squareup.kotlinpoet.AnnotationSpec.UseSiteTarget.FILE
22 import com.squareup.kotlinpoet.FileSpec
23 import com.squareup.kotlinpoet.FunSpec
24 import com.squareup.kotlinpoet.FunSpec.Companion.isAccessor
25 import com.squareup.kotlinpoet.FunSpec.Companion.isConstructor
26 import com.squareup.kotlinpoet.PropertySpec
27 import com.squareup.kotlinpoet.TypeName
28 import com.squareup.kotlinpoet.TypeSpec
29 import com.squareup.kotlinpoet.asTypeName
30 import java.lang.reflect.Type
31 import kotlin.reflect.KClass
32 
jvmNamenull33 public fun FileSpec.Builder.jvmName(name: String): FileSpec.Builder = addAnnotation(
34   AnnotationSpec.builder(JvmName::class)
35     .useSiteTarget(FILE)
36     .addMember("%S", name)
37     .build(),
38 )
39 
40 public fun FileSpec.Builder.jvmMultifileClass(): FileSpec.Builder = addAnnotation(
41   AnnotationSpec.builder(JvmMultifileClass::class)
42     .useSiteTarget(FILE)
43     .build(),
44 )
45 
46 public fun TypeSpec.Builder.jvmSuppressWildcards(suppress: Boolean = true): TypeSpec.Builder =
47   addAnnotation(jvmSuppressWildcardsAnnotation(suppress))
48 
49 private fun jvmSuppressWildcardsAnnotation(suppress: Boolean = true) =
50   AnnotationSpec.builder(JvmSuppressWildcards::class)
51     .apply { if (!suppress) addMember("suppress = false") }
52     .build()
53 
jvmInlinenull54 public fun TypeSpec.Builder.jvmInline(): TypeSpec.Builder = addAnnotation(JvmInline::class)
55 
56 public fun TypeSpec.Builder.jvmRecord(): TypeSpec.Builder = addAnnotation(JvmRecord::class)
57 
58 public fun FunSpec.Builder.jvmStatic(): FunSpec.Builder = apply {
59   check(!name.isConstructor) { "Can't apply @JvmStatic to a constructor!" }
60   addAnnotation(JvmStatic::class)
61 }
62 
<lambda>null63 public fun FunSpec.Builder.jvmOverloads(): FunSpec.Builder = apply {
64   check(!name.isAccessor) {
65     "Can't apply @JvmOverloads to a " + if (name == FunSpec.GETTER) "getter!" else "setter!"
66   }
67   addAnnotation(JvmOverloads::class)
68 }
69 
<lambda>null70 public fun FunSpec.Builder.jvmName(name: String): FunSpec.Builder = apply {
71   check(!this.name.isConstructor) { "Can't apply @JvmName to a constructor!" }
72   addAnnotation(
73     AnnotationSpec.builder(JvmName::class)
74       .addMember("%S", name)
75       .build(),
76   )
77 }
78 
throwsnull79 public fun FunSpec.Builder.throws(vararg exceptionClasses: KClass<out Throwable>): FunSpec.Builder =
80   throws(exceptionClasses.map(KClass<*>::asTypeName))
81 
82 public fun FunSpec.Builder.throws(vararg exceptionClasses: Type): FunSpec.Builder =
83   throws(exceptionClasses.map(Type::asTypeName))
84 
85 public fun FunSpec.Builder.throws(vararg exceptionClasses: TypeName): FunSpec.Builder =
86   throws(exceptionClasses.toList())
87 
88 public fun FunSpec.Builder.throws(exceptionClasses: Iterable<TypeName>): FunSpec.Builder =
89   addAnnotation(
90     AnnotationSpec.builder(Throws::class)
91       .apply { exceptionClasses.forEach { addMember("%T::class", it) } }
92       .build(),
93   )
94 
<lambda>null95 public fun FunSpec.Builder.jvmSuppressWildcards(suppress: Boolean = true): FunSpec.Builder = apply {
96   check(!name.isConstructor) { "Can't apply @JvmSuppressWildcards to a constructor!" }
97   check(!name.isAccessor) {
98     "Can't apply @JvmSuppressWildcards to a " + if (name == FunSpec.GETTER) "getter!" else "setter!"
99   }
100   addAnnotation(jvmSuppressWildcardsAnnotation(suppress))
101 }
102 
<lambda>null103 public fun FunSpec.Builder.synchronized(): FunSpec.Builder = apply {
104   check(!name.isConstructor) { "Can't apply @Synchronized to a constructor!" }
105   addAnnotation(Synchronized::class)
106 }
107 
strictfpnull108 public fun FunSpec.Builder.strictfp(): FunSpec.Builder = addAnnotation(Strictfp::class)
109 
110 public fun PropertySpec.Builder.jvmField(): PropertySpec.Builder = addAnnotation(JvmField::class)
111 
112 public fun PropertySpec.Builder.jvmStatic(): PropertySpec.Builder = addAnnotation(JvmStatic::class)
113 
114 public fun PropertySpec.Builder.jvmSuppressWildcards(
115   suppress: Boolean = true,
116 ): PropertySpec.Builder = addAnnotation(jvmSuppressWildcardsAnnotation(suppress))
117 
118 public fun PropertySpec.Builder.transient(): PropertySpec.Builder = addAnnotation(Transient::class)
119 
120 public fun PropertySpec.Builder.volatile(): PropertySpec.Builder = addAnnotation(Volatile::class)
121 
122 public fun TypeName.jvmSuppressWildcards(suppress: Boolean = true): TypeName =
123   copy(annotations = this.annotations + jvmSuppressWildcardsAnnotation(suppress))
124 
125 public fun TypeName.jvmWildcard(): TypeName =
126   copy(annotations = this.annotations + AnnotationSpec.builder(JvmWildcard::class).build())
127 
128 @Suppress("DEPRECATION")
129 @Deprecated(
130   """
131   'JvmDefault' is deprecated. Switch to new -Xjvm-default modes: `all` or `all-compatibility`.
132   In KotlinPoet 1.15.0 and newer, this method is a no-op. It will be deleted in KotlinPoet 2.0.
133 """,
134 )
135 public fun PropertySpec.Builder.jvmDefault(): PropertySpec.Builder = this
136 
137 @Suppress("DEPRECATION")
138 @Deprecated(
139   """
140   'JvmDefault' is deprecated. Switch to new -Xjvm-default modes: `all` or `all-compatibility`.
141   In KotlinPoet 1.15.0 and newer, this method is a no-op. It will be deleted in KotlinPoet 2.0.
142 """,
143 )
144 public fun FunSpec.Builder.jvmDefault(): FunSpec.Builder = this
145