|
| 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 | + |
| 18 | +package com.google.devtools.ksp.processor |
| 19 | + |
| 20 | +import com.google.devtools.ksp.closestClassDeclaration |
| 21 | +import com.google.devtools.ksp.getClassDeclarationByName |
| 22 | +import com.google.devtools.ksp.processing.Resolver |
| 23 | +import com.google.devtools.ksp.symbol.KSClassDeclaration |
| 24 | +import com.google.devtools.ksp.symbol.KSFunctionDeclaration |
| 25 | + |
| 26 | +@Suppress("unused") // used by tests |
| 27 | +class OverrideeProcessor: AbstractTestProcessor() { |
| 28 | + private val results = mutableListOf<String>() |
| 29 | + |
| 30 | + override fun toResult() = results |
| 31 | + |
| 32 | + override fun process(resolver: Resolver) { |
| 33 | + logSubject(resolver, "Subject") |
| 34 | + logSubject(resolver, "JavaSubject.Subject") |
| 35 | + logSubject(resolver, "lib.Subject") |
| 36 | + logSubject(resolver, "ConflictingSubject1") |
| 37 | + logSubject(resolver, "ConflictingSubject2") |
| 38 | + logSubject(resolver, "ConflictingSubject3") |
| 39 | + logSubject(resolver, "ConflictingSubject4") |
| 40 | + logSubject(resolver, "OverrideOrder1") |
| 41 | + logSubject(resolver, "OverrideOrder2") |
| 42 | + } |
| 43 | + |
| 44 | + private fun logSubject(resolver: Resolver, qName:String) { |
| 45 | + results.add("$qName:") |
| 46 | + val subject = resolver.getClassDeclarationByName(qName)!! |
| 47 | + subject.declarations.filterIsInstance<KSClassDeclaration>().forEach { |
| 48 | + logClass(it) |
| 49 | + } |
| 50 | + logClass(subject) |
| 51 | + } |
| 52 | + |
| 53 | + private fun logClass(subject: KSClassDeclaration) { |
| 54 | + subject.declarations.filterIsInstance<KSFunctionDeclaration>() |
| 55 | + .filterNot { it.simpleName.asString() in IGNORED_METHOD_NAMES } |
| 56 | + .forEach { |
| 57 | + val signature = it.toSignature() |
| 58 | + val overrideeSignature = it.findOverridee()?.toSignature() |
| 59 | + results.add("$signature -> $overrideeSignature") |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private fun KSFunctionDeclaration.toSignature(): String { |
| 64 | + val self = this |
| 65 | + return buildString { |
| 66 | + append(self.closestClassDeclaration()?.simpleName?.asString()) |
| 67 | + append(".") |
| 68 | + append(self.simpleName.asString()) |
| 69 | + append( |
| 70 | + self.parameters.joinToString(", ", prefix = "(", postfix = ")") { |
| 71 | + "${it.name?.asString()}:${it.type.resolve().declaration.simpleName.asString()}" |
| 72 | + } |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + companion object { |
| 78 | + // ignore these methods as we receive syntetics of it from compiled code |
| 79 | + private val IGNORED_METHOD_NAMES = listOf("equals", "hashCode", "toString") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +interface MyInterface { |
| 84 | + fun openFoo(): Int { return 1} |
| 85 | + fun absFoo(): Unit |
| 86 | +} |
| 87 | + |
| 88 | +interface MyInterface2 { |
| 89 | + fun absFoo(): Unit |
| 90 | +} |
| 91 | + |
| 92 | +abstract class MyAbstract: MyInterface { |
| 93 | + override fun absFoo(): Unit {val a = 1} |
| 94 | + override fun openFoo(): Int { return 2 } |
| 95 | +} |
| 96 | + |
| 97 | +class Subject2: MyInterface, MyAbstract() { |
| 98 | + override fun absFoo(): Unit = TODO() |
| 99 | +} |
0 commit comments