Skip to content

Commit 5aa2e60

Browse files
committed
Add Resolver.mapToJvmClassName() for KSClassDeclaration
1 parent 6f7c046 commit 5aa2e60

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

api/api.base

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ package com.google.devtools.ksp.processing {
168168
method @com.google.devtools.ksp.KspExperimental public boolean isJavaRawType(@NonNull com.google.devtools.ksp.symbol.KSType type);
169169
method @Nullable @com.google.devtools.ksp.KspExperimental public com.google.devtools.ksp.symbol.KSName mapJavaNameToKotlin(@NonNull com.google.devtools.ksp.symbol.KSName javaName);
170170
method @Nullable @com.google.devtools.ksp.KspExperimental public com.google.devtools.ksp.symbol.KSName mapKotlinNameToJava(@NonNull com.google.devtools.ksp.symbol.KSName kotlinName);
171+
method @Nullable @com.google.devtools.ksp.KspExperimental public String mapToJvmClassName(@NonNull com.google.devtools.ksp.symbol.KSClassDeclaration declaration);
171172
method @Nullable @com.google.devtools.ksp.KspExperimental public String mapToJvmSignature(@NonNull com.google.devtools.ksp.symbol.KSDeclaration declaration);
172173
method public boolean overrides(@NonNull com.google.devtools.ksp.symbol.KSDeclaration overrider, @NonNull com.google.devtools.ksp.symbol.KSDeclaration overridee);
173174
method public boolean overrides(@NonNull com.google.devtools.ksp.symbol.KSDeclaration overrider, @NonNull com.google.devtools.ksp.symbol.KSDeclaration overridee, @NonNull com.google.devtools.ksp.symbol.KSClassDeclaration containingClass);

api/src/main/kotlin/com/google/devtools/ksp/processing/Resolver.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ interface Resolver {
115115
@KspExperimental
116116
fun mapToJvmSignature(declaration: KSDeclaration): String?
117117

118+
/**
119+
* Returns the [binary class name](https://asm.ow2.io/javadoc/org/objectweb/asm/Type.html#getClassName()) in JVM
120+
* for the given [KSClassDeclaration].
121+
*/
122+
@KspExperimental
123+
fun mapToJvmClassName(declaration: KSClassDeclaration): String?
124+
118125
/**
119126
* @param overrider the candidate overriding declaration being checked.
120127
* @param overridee the candidate overridden declaration being checked.

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ class ResolverImpl(
367367
else -> null
368368
}
369369

370+
@KspExperimental
371+
override fun mapToJvmClassName(declaration: KSClassDeclaration): String? {
372+
val descriptor = resolveClassDeclaration(declaration) ?: return null
373+
374+
return typeMapper.mapType(descriptor).className
375+
}
376+
370377
override fun overrides(overrider: KSDeclaration, overridee: KSDeclaration): Boolean {
371378
fun resolveForOverride(declaration: KSDeclaration): DeclarationDescriptor? {
372379
return when (declaration) {

compiler-plugin/src/test/kotlin/com/google/devtools/ksp/test/KSPCompilerPluginTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ class KSPCompilerPluginTest : AbstractKSPCompilerPluginTest() {
339339
runTest("../test-utils/testData/api/javaWildcards2.kt")
340340
}
341341

342+
@TestMetadata("jvmClassNameMapper.kt")
343+
@Test
344+
fun testJvmClassNameMapper() {
345+
runTest("../test-utils/testData/api/jvmClassNameMapper.kt")
346+
}
347+
342348
@TestMetadata("lateinitProperties.kt")
343349
@Test
344350
fun testLateinitProperties() {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.KspExperimental
21+
import com.google.devtools.ksp.getClassDeclarationByName
22+
import com.google.devtools.ksp.processing.Resolver
23+
import com.google.devtools.ksp.symbol.KSAnnotated
24+
25+
@KspExperimental
26+
class MapJvmClassNameProcessor : AbstractTestProcessor() {
27+
private val result = mutableListOf<String>()
28+
29+
override fun toResult(): List<String> {
30+
return result
31+
}
32+
33+
override fun process(resolver: Resolver): List<KSAnnotated> {
34+
listOf(
35+
"Cls1",
36+
"Cls1.Cls2",
37+
"Cls1.Cls3",
38+
"Cls1.Cls4",
39+
"JavaInterfaceWithVoid",
40+
"JavaClass1",
41+
"JavaClass1.JavaClass2",
42+
"JavaClass1.JavaClass3",
43+
"JavaClass1.JavaClass4",
44+
"JavaClass5",
45+
"JavaAnno",
46+
)
47+
.map { className ->
48+
resolver.getClassDeclarationByName(className)!!
49+
}.forEach { subject ->
50+
result.add(resolver.mapToJvmClassName(subject)!!)
51+
}
52+
return emptyList()
53+
}
54+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
// TEST PROCESSOR: MapJvmClassNameProcessor
19+
// EXPECTED:
20+
// Cls1
21+
// Cls1$Cls2
22+
// Cls1$Cls3
23+
// Cls1$Cls4
24+
// JavaInterfaceWithVoid
25+
// JavaClass1
26+
// JavaClass1$JavaClass2
27+
// JavaClass1$JavaClass3
28+
// JavaClass1$JavaClass4
29+
// JavaClass5
30+
// JavaAnno
31+
// END
32+
33+
// FILE: Cls1.kt
34+
class Cls1 {
35+
class Cls2
36+
37+
open class Cls3
38+
39+
inner class Cls4
40+
}
41+
42+
// FILE: JavaInterfaceWithVoid.java
43+
interface JavaInterfaceWithVoid {}
44+
45+
// FILE: JavaClass1.java
46+
class JavaClass1 {
47+
static final class JavaClass2 {}
48+
static class JavaClass3 {}
49+
class JavaClass4 {}
50+
}
51+
52+
class JavaClass5 {}
53+
54+
// FILE: JavaAnno.java
55+
@interface JavaAnno {}

0 commit comments

Comments
 (0)