Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KSP2: implement KSTypeAlias.modifiers for libs #2152

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ abstract class AbstractKSDeclarationImpl(val ktDeclarationSymbol: KaDeclarationS
is KaClassSymbol -> ktDeclarationSymbol.toModifiers()
is KaFunctionSymbol -> ktDeclarationSymbol.toModifiers()
is KaJavaFieldSymbol -> ktDeclarationSymbol.toModifiers()
is KaTypeAliasSymbol -> ktDeclarationSymbol.toModifiers()
else -> throw IllegalStateException("Unexpected symbol type ${ktDeclarationSymbol.javaClass}")
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ package com.google.devtools.ksp.impl.symbol.kotlin
import com.google.devtools.ksp.common.KSObjectCache
import com.google.devtools.ksp.common.impl.KSNameImpl
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.symbol.KSExpectActual
import com.google.devtools.ksp.symbol.KSName
import com.google.devtools.ksp.symbol.KSTypeAlias
import com.google.devtools.ksp.symbol.KSTypeReference
import com.google.devtools.ksp.symbol.KSVisitor
import org.jetbrains.kotlin.analysis.api.symbols.KaTypeAliasSymbol
import org.jetbrains.kotlin.analysis.api.symbols.nameOrAnonymous
import com.google.devtools.ksp.symbol.*
import org.jetbrains.kotlin.analysis.api.symbols.*

class KSTypeAliasImpl private constructor(private val ktTypeAliasSymbol: KaTypeAliasSymbol) :
KSTypeAlias,
Expand Down Expand Up @@ -62,3 +57,12 @@ class KSTypeAliasImpl private constructor(private val ktTypeAliasSymbol: KaTypeA
return ktTypeAliasSymbol.defer(::getCached)
}
}

internal fun KaTypeAliasSymbol.toModifiers(): Set<Modifier> {
val result = mutableSetOf<Modifier>()
result.add(modality.toModifier())
if (visibility != KaSymbolVisibility.PACKAGE_PRIVATE) {
result.add(visibility.toModifier())
}
return result
}
16 changes: 16 additions & 0 deletions kotlin-analysis-api/testData/javaModifiers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
// DependencyOuterKotlinClass.transientProperty: FINAL PUBLIC : FINAL JAVA_TRANSIENT PUBLIC
// DependencyOuterKotlinClass.volatileProperty: FINAL PUBLIC : FINAL JAVA_VOLATILE PUBLIC
// DependencyOuterKotlinClass: OPEN PUBLIC : PUBLIC
// HasTypeAliasFuns: Modifiers: []
// HasTypeAliasFuns: Visibility: PUBLIC
// OuterJavaClass.<init>: FINAL PUBLIC : FINAL PUBLIC
// OuterJavaClass.InnerJavaClass.<init>: FINAL PUBLIC : FINAL PUBLIC
// OuterJavaClass.InnerJavaClass: PUBLIC : PUBLIC
Expand Down Expand Up @@ -96,12 +98,19 @@
// OuterKotlinClass.transientProperty: : FINAL JAVA_TRANSIENT PUBLIC
// OuterKotlinClass.volatileProperty: : FINAL JAVA_VOLATILE PUBLIC
// OuterKotlinClass: OPEN : PUBLIC
// TypeAliasInKt: Modifiers: [PRIVATE]
// TypeAliasInKt: Visibility: PRIVATE
// TypeAliasInLib: Modifiers: [FINAL, PUBLIC]
// TypeAliasInLib: Visibility: PUBLIC
// END
// MODULE: module1
// FILE: ALib.kt
fun interface ALib {
fun test(): Boolean
}

public typealias TypeAliasInLib = Int

// FILE: DependencyOuterJavaClass.java
public class DependencyOuterJavaClass {
public class DependencyInnerJavaClass {}
Expand Down Expand Up @@ -149,6 +158,13 @@ open class DependencyOuterKotlinClass {
}
// MODULE: main(module1)
// FILE: ASrc.kt
private typealias TypeAliasInKt = Int

class HasTypeAliasFuns {
fun FunReturnTA1(): TypeAliasInKt = 0
fun FunReturnTA2(): TypeAliasInLib = 0
}

fun interface ASrc {
fun test(): Boolean
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.google.devtools.ksp.processor

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.getVisibility
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
Expand All @@ -45,6 +47,15 @@ class JavaModifierProcessor : AbstractTestProcessor() {
.forEach {
it.superTypes.single().resolve().declaration.accept(ModifierVisitor(resolver), Unit)
}

resolver.getClassDeclarationByName("HasTypeAliasFuns")!!.getDeclaredFunctions().forEach { f ->
val decl = f.returnType!!.resolve().declaration
val declName = decl.simpleName.asString().toString()
val visibility = decl.getVisibility().toString()
val modifiers = decl.modifiers.map { it.toString() }
results.add("$declName: Visibility: $visibility")
results.add("$declName: Modifiers: $modifiers")
}
return emptyList()
}

Expand Down
16 changes: 16 additions & 0 deletions test-utils/testData/api/javaModifiers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
// DependencyOuterKotlinClass.transientProperty: FINAL PUBLIC : FINAL JAVA_TRANSIENT PUBLIC
// DependencyOuterKotlinClass.volatileProperty: FINAL PUBLIC : FINAL JAVA_VOLATILE PUBLIC
// DependencyOuterKotlinClass: OPEN PUBLIC : PUBLIC
// HasTypeAliasFuns: Modifiers: []
// HasTypeAliasFuns: Visibility: PUBLIC
// OuterJavaClass.<init>: FINAL PUBLIC : FINAL PUBLIC
// OuterJavaClass.InnerJavaClass.<init>: FINAL PUBLIC : FINAL PUBLIC
// OuterJavaClass.InnerJavaClass: PUBLIC : PUBLIC
Expand Down Expand Up @@ -96,12 +98,19 @@
// OuterKotlinClass.transientProperty: : FINAL JAVA_TRANSIENT PUBLIC
// OuterKotlinClass.volatileProperty: : FINAL JAVA_VOLATILE PUBLIC
// OuterKotlinClass: OPEN : PUBLIC
// TypeAliasInKt: Modifiers: [PRIVATE]
// TypeAliasInKt: Visibility: PRIVATE
// TypeAliasInLib: Modifiers: [FINAL, PUBLIC]
// TypeAliasInLib: Visibility: PUBLIC
// END
// MODULE: module1
// FILE: ALib.kt
fun interface ALib {
fun test(): Boolean
}

public typealias TypeAliasInLib = Int

// FILE: DependencyOuterJavaClass.java
public class DependencyOuterJavaClass {
public class DependencyInnerJavaClass {}
Expand Down Expand Up @@ -149,6 +158,13 @@ open class DependencyOuterKotlinClass {
}
// MODULE: main(module1)
// FILE: ASrc.kt
private typealias TypeAliasInKt = Int

class HasTypeAliasFuns {
fun FunReturnTA1(): TypeAliasInKt = 0
fun FunReturnTA2(): TypeAliasInLib = 0
}

fun interface ASrc {
fun test(): Boolean
}
Expand Down
Loading