Skip to content

Commit

Permalink
add createKSTypeReferenceFromKSType API to Resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Sep 30, 2020
1 parent a43bfc0 commit 37a1e8e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ interface Resolver {
*/
fun getKSNameFromString(name: String): KSName

/**
* Create a [KSTypeReference] from a [KSType]
*/
fun createKSTypeReferenceFromKSType(type: KSType): KSTypeReference

/**
* Provides built in types for convenience. For example, [KSBuiltins.anyType] is the KSType instance for class 'kotlin.Any'.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.google.devtools.ksp.symbol.impl.binary.*
import com.google.devtools.ksp.symbol.impl.findPsi
import com.google.devtools.ksp.symbol.impl.java.*
import com.google.devtools.ksp.symbol.impl.kotlin.*
import com.google.devtools.ksp.symbol.impl.synthetic.KSTypeReferenceSyntheticImpl
import org.jetbrains.kotlin.load.java.components.TypeUsage
import org.jetbrains.kotlin.load.java.lazy.JavaResolverComponents
import org.jetbrains.kotlin.load.java.lazy.LazyJavaResolverContext
Expand Down Expand Up @@ -213,6 +214,10 @@ class ResolverImpl(
return KSNameImpl.getCached(name)
}

override fun createKSTypeReferenceFromKSType(type: KSType): KSTypeReference {
return KSTypeReferenceSyntheticImpl.getCached(type)
}

override fun mapToJvmSignature(declaration: KSDeclaration): String {
return when (declaration) {
is KSClassDeclaration -> resolveClassDeclaration(declaration)?.let { typeMapper.mapType(it).descriptor } ?: ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.google.devtools.ksp.symbol.impl.synthetic

import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.KSObjectCache

class KSTypeReferenceSyntheticImpl(val ksType: KSType) : KSTypeReference {
companion object : KSObjectCache<KSType, KSTypeReferenceSyntheticImpl>() {
fun getCached(ksType: KSType) = KSTypeReferenceSyntheticImpl.cache.getOrPut(ksType) { KSTypeReferenceSyntheticImpl(ksType) }
}

override val annotations: List<KSAnnotation> = emptyList()

override val element: KSReferenceElement? = null

override val location: Location = NonExistLocation

override val modifiers: Set<Modifier> = emptySet()

override val origin: Origin = Origin.SYNTHETIC

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitTypeReference(this, data)
}

override fun resolve(): KSType {
return ksType
}
}

0 comments on commit 37a1e8e

Please sign in to comment.