1 /*
2  * Copyright 2022 Google LLC
3  * Copyright 2010-2022 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 
18 package com.google.devtools.ksp.visitor
19 
20 import com.google.devtools.ksp.symbol.KSAnnotated
21 import com.google.devtools.ksp.symbol.KSClassDeclaration
22 import com.google.devtools.ksp.symbol.KSFile
23 import com.google.devtools.ksp.symbol.KSFunctionDeclaration
24 import com.google.devtools.ksp.symbol.KSPropertyDeclaration
25 import com.google.devtools.ksp.symbol.KSPropertyGetter
26 import com.google.devtools.ksp.symbol.KSPropertySetter
27 import com.google.devtools.ksp.symbol.KSTypeAlias
28 import com.google.devtools.ksp.symbol.KSTypeParameter
29 import com.google.devtools.ksp.symbol.KSValueParameter
30 import com.google.devtools.ksp.symbol.KSVisitorVoid
31 
32 // TODO: Make visitor a generator
33 class CollectAnnotatedSymbolsVisitor(private val inDepth: Boolean) : KSVisitorVoid() {
34     val symbols = arrayListOf<KSAnnotated>()
35 
visitAnnotatednull36     override fun visitAnnotated(annotated: KSAnnotated, data: Unit) {
37         if (annotated.annotations.any())
38             symbols.add(annotated)
39     }
40 
visitFilenull41     override fun visitFile(file: KSFile, data: Unit) {
42         visitAnnotated(file, data)
43         file.declarations.forEach { it.accept(this, data) }
44     }
45 
visitTypeAliasnull46     override fun visitTypeAlias(typeAlias: KSTypeAlias, data: Unit) {
47         if (typeAlias.annotations.any())
48             symbols.add(typeAlias)
49     }
50 
visitClassDeclarationnull51     override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) {
52         visitAnnotated(classDeclaration, data)
53         classDeclaration.typeParameters.forEach { it.accept(this, data) }
54         classDeclaration.declarations.forEach { it.accept(this, data) }
55     }
56 
visitPropertyGetternull57     override fun visitPropertyGetter(getter: KSPropertyGetter, data: Unit) {
58         visitAnnotated(getter, data)
59     }
60 
visitPropertySetternull61     override fun visitPropertySetter(setter: KSPropertySetter, data: Unit) {
62         setter.parameter.accept(this, data)
63         visitAnnotated(setter, data)
64     }
65 
visitFunctionDeclarationnull66     override fun visitFunctionDeclaration(function: KSFunctionDeclaration, data: Unit) {
67         visitAnnotated(function, data)
68         function.typeParameters.forEach { it.accept(this, data) }
69         function.parameters.forEach { it.accept(this, data) }
70         if (inDepth) {
71             function.declarations.forEach { it.accept(this, data) }
72         }
73     }
74 
visitPropertyDeclarationnull75     override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: Unit) {
76         visitAnnotated(property, data)
77         property.typeParameters.forEach { it.accept(this, data) }
78         property.getter?.accept(this, data)
79         property.setter?.accept(this, data)
80     }
81 
visitTypeParameternull82     override fun visitTypeParameter(typeParameter: KSTypeParameter, data: Unit) {
83         visitAnnotated(typeParameter, data)
84         super.visitTypeParameter(typeParameter, data)
85     }
86 
visitValueParameternull87     override fun visitValueParameter(valueParameter: KSValueParameter, data: Unit) {
88         visitAnnotated(valueParameter, data)
89     }
90 }
91