xref: /aosp_15_r20/external/ksp/api/src/main/kotlin/com/google/devtools/ksp/symbol/KSPropertyDeclaration.kt (revision af87fb4bb8e3042070d2a054e912924f599b22b7)
1 /*
2  * Copyright 2020 Google LLC
3  * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.google.devtools.ksp.symbol
18 
19 /**
20  * A property declaration, can also be used to denote a variable declaration.
21  */
22 interface KSPropertyDeclaration : KSDeclaration {
23 
24     /**
25      * Getter of the property.
26      * Note that when KSPropertyDeclaration is used to model a variable, getter is always null, as a variable can't have a getter.
27      */
28     val getter: KSPropertyGetter?
29 
30     /**
31      * Setter of the property.
32      * Note that when KSPropertyDeclaration is used to model a variable, setter is always null, as a variable can't have a setter.
33      * If a property is immutable, setter is always null as well, as an immutable property can't have a setter.
34      */
35     val setter: KSPropertySetter?
36 
37     /**
38      * Extension receiver if this declaration is an [extension property][https://kotlinlang.org/docs/reference/extensions.html#extension-properties].
39      * Dispatch receiver is [parentDeclaration], if any.
40      */
41     val extensionReceiver: KSTypeReference?
42 
43     /**
44      * The type of this declaration.
45      */
46     val type: KSTypeReference
47 
48     /**
49      * True if this property is mutable.
50      */
51     val isMutable: Boolean
52 
53     /**
54      * True if this property has a backing field.
55      *
56      * Note that, this is specific to the current property and does not check for properties that are overridden by this
57      * property.
58      *
59      * https://kotlinlang.org/docs/properties.html#backing-fields
60      */
61     val hasBackingField: Boolean
62 
63     /**
64      * Indicates whether this is a delegated property.
65      */
isDelegatednull66     fun isDelegated(): Boolean
67 
68     /**
69      * Find the closest overridee of this property, if overriding.
70      *
71      * For the following input:
72      * ```
73      * abstract class A {
74      *   open val x:Int
75      *   open val y:Int
76      * }
77      * abstract class B : A() {
78      *   override val x:Int
79      * }
80      * abstract class C : B() {
81      *   override val x:Int
82      *   override val y:Int
83      * }
84      * ```
85      * Calling `findOverridee` on `C.x` will return `B.x`.
86      * Calling `findOverridee` on `C.y` will return `A.y`.
87      *
88      * When there are multiple super classes / interfaces with the property, the closest declaration
89      * to the current containing declaration is selected. If they are in the same level, the
90      * property of the first specified interface (in source) will be returned.
91      *
92      * @return [KSPropertyDeclaration] for the overridden property, if overriding, otherwise null.
93      * Calling [findOverridee] is expensive and should be avoided if possible.
94      */
95     fun findOverridee(): KSPropertyDeclaration?
96 
97     /**
98      * Returns the type of the [property] when it is viewed as member of the [containing] type.
99      *
100      * For instance, for the following input:
101      * ```
102      * class Base<T>(val x:T)
103      * val foo: Base<Int>
104      * val bar: Base<String>
105      * ```
106      * When `x` is viewed as member of `foo`, this method will return the [KSType] for `Int`
107      * whereas when `x` is viewed as member of `bar`, this method will return the [KSType]
108      * representing `String`.
109      *
110      * If the substitution fails (e.g. if [containing] is an error type, a [KSType] with [KSType.isError] `true` is
111      * returned.
112      *
113      * @param containing The type that contains [property]
114      * @throws IllegalArgumentException Throws [IllegalArgumentException] when [containing] does not contain
115      * [property] or if the [property] is not declared in a class, object or interface.
116      */
117     fun asMemberOf(containing: KSType): KSType
118 }
119