[ObjCExport] KtResolvedObjCNameAnnotation: Return null if ObjCName annotation is absent
This commit is contained in:
committed by
Space Team
parent
98fda13523
commit
7ef62d1779
+4
-4
@@ -30,8 +30,8 @@ private class ObjCExportNamerImpl : KtObjCExportNamer {
|
||||
val resolvedObjCNameAnnotation = symbol.resolveObjCNameAnnotation()
|
||||
|
||||
return ObjCExportClassOrProtocolName(
|
||||
objCName = resolvedObjCNameAnnotation.objCName ?: symbol.nameOrAnonymous.asString(),
|
||||
swiftName = resolvedObjCNameAnnotation.swiftName ?: symbol.nameOrAnonymous.asString()
|
||||
objCName = resolvedObjCNameAnnotation?.objCName ?: symbol.nameOrAnonymous.asString(),
|
||||
swiftName = resolvedObjCNameAnnotation?.swiftName ?: symbol.nameOrAnonymous.asString()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ private class ObjCExportNamerImpl : KtObjCExportNamer {
|
||||
val resolveObjCNameAnnotation = symbol.resolveObjCNameAnnotation()
|
||||
|
||||
return ObjCExportPropertyName(
|
||||
objCName = resolveObjCNameAnnotation.objCName ?: symbol.name.asString(),
|
||||
swiftName = resolveObjCNameAnnotation.swiftName ?: symbol.name.asString()
|
||||
objCName = resolveObjCNameAnnotation?.objCName ?: symbol.name.asString(),
|
||||
swiftName = resolveObjCNameAnnotation?.swiftName ?: symbol.name.asString()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+23
-23
@@ -6,7 +6,9 @@
|
||||
package org.jetbrains.kotlin.objcexport
|
||||
|
||||
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplicationWithArgumentsInfo
|
||||
import org.jetbrains.kotlin.analysis.api.annotations.KtConstantAnnotationValue
|
||||
import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue
|
||||
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
||||
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue.KtStringConstantValue
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtAnnotatedSymbol
|
||||
@@ -43,30 +45,28 @@ internal class KtResolvedObjCNameAnnotation(
|
||||
)
|
||||
|
||||
context(KtAnalysisSession)
|
||||
internal fun KtAnnotatedSymbol.resolveObjCNameAnnotation(): KtResolvedObjCNameAnnotation {
|
||||
var objCName: String? = null
|
||||
var swiftName: String? = null
|
||||
var isExact = false
|
||||
|
||||
annotationsList.annotations.find { it.classId?.asSingleFqName() == KonanFqNames.objCName }?.let { annotation ->
|
||||
annotation.arguments.forEach { argument ->
|
||||
when (argument.name.identifier) {
|
||||
"name" -> objCName = argument.expression.let { it as? KtConstantAnnotationValue }
|
||||
?.constantValue?.let { it as KtStringConstantValue }
|
||||
?.value
|
||||
"swiftName" -> swiftName = argument.expression.let { it as? KtConstantAnnotationValue }
|
||||
?.constantValue?.let { it as KtStringConstantValue }
|
||||
?.value
|
||||
"exact" -> isExact = argument.expression.let { it as? KtConstantAnnotationValue }
|
||||
?.constantValue?.let { it as KtConstantValue.KtBooleanConstantValue }
|
||||
?.value ?: isExact
|
||||
}
|
||||
}
|
||||
}
|
||||
internal fun KtAnnotatedSymbol.resolveObjCNameAnnotation(): KtResolvedObjCNameAnnotation? {
|
||||
val annotation = annotationsList.annotations.find { it.classId?.asSingleFqName() == KonanFqNames.objCName } ?: return null
|
||||
|
||||
return KtResolvedObjCNameAnnotation(
|
||||
objCName = objCName,
|
||||
swiftName = swiftName,
|
||||
isExact = isExact
|
||||
objCName = annotation.findArgument("name")?.resolveStringConstantValue(),
|
||||
swiftName = annotation.findArgument("swiftName")?.resolveStringConstantValue(),
|
||||
isExact = annotation.findArgument("exact")?.resolveBooleanConstantValue() ?: false
|
||||
)
|
||||
}
|
||||
|
||||
private fun KtAnnotationApplicationWithArgumentsInfo.findArgument(name: String): KtNamedAnnotationValue? {
|
||||
return arguments.find { it.name.identifier == name }
|
||||
}
|
||||
|
||||
private fun KtNamedAnnotationValue.resolveStringConstantValue(): String? {
|
||||
return expression.let { it as? KtConstantAnnotationValue }?.constantValue
|
||||
?.let { it as? KtStringConstantValue }
|
||||
?.value
|
||||
}
|
||||
|
||||
private fun KtNamedAnnotationValue.resolveBooleanConstantValue(): Boolean? {
|
||||
return expression.let { it as? KtConstantAnnotationValue }?.constantValue
|
||||
?.let { it as? KtConstantValue.KtBooleanConstantValue }
|
||||
?.value
|
||||
}
|
||||
|
||||
+5
-8
@@ -5,15 +5,15 @@
|
||||
|
||||
package org.jetbrains.kotlin.objcexport.tests
|
||||
|
||||
import org.jetbrains.kotlin.objcexport.resolveObjCNameAnnotation
|
||||
import org.jetbrains.kotlin.analysis.api.analyze
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionSymbol
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.objcexport.resolveObjCNameAnnotation
|
||||
import org.jetbrains.kotlin.objcexport.testUtils.InlineSourceCodeAnalysis
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@@ -26,10 +26,7 @@ class KtResolvedObjCNameAnnotationTest(
|
||||
val ktFile = inlineSourceCodeAnalysis.createKtFile("class Foo")
|
||||
analyze(ktFile) {
|
||||
val fooSymbol = ktFile.getFileSymbol().getFileScope().getClassifierSymbols(Name.identifier("Foo")).single() as KtClassLikeSymbol
|
||||
val resolvedObjCAnnotation = fooSymbol.resolveObjCNameAnnotation()
|
||||
assertNull(resolvedObjCAnnotation.swiftName)
|
||||
assertNull(resolvedObjCAnnotation.objCName)
|
||||
assertFalse(resolvedObjCAnnotation.isExact)
|
||||
assertNull(fooSymbol.resolveObjCNameAnnotation())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +40,7 @@ class KtResolvedObjCNameAnnotationTest(
|
||||
)
|
||||
analyze(ktFile) {
|
||||
val fooSymbol = ktFile.getFileSymbol().getFileScope().getClassifierSymbols(Name.identifier("Foo")).single() as KtClassLikeSymbol
|
||||
val resolvedObjCAnnotation = fooSymbol.resolveObjCNameAnnotation()
|
||||
val resolvedObjCAnnotation = assertNotNull(fooSymbol.resolveObjCNameAnnotation())
|
||||
assertEquals("FooObjC", resolvedObjCAnnotation.objCName)
|
||||
assertEquals("FooSwift", resolvedObjCAnnotation.swiftName)
|
||||
assertTrue(resolvedObjCAnnotation.isExact)
|
||||
@@ -60,7 +57,7 @@ class KtResolvedObjCNameAnnotationTest(
|
||||
)
|
||||
analyze(ktFile) {
|
||||
val fooSymbol = ktFile.getFileSymbol().getFileScope().getCallableSymbols(Name.identifier("foo")).single() as KtFunctionSymbol
|
||||
val resolvedObjCAnnotation = fooSymbol.resolveObjCNameAnnotation()
|
||||
val resolvedObjCAnnotation = assertNotNull(fooSymbol.resolveObjCNameAnnotation())
|
||||
assertEquals("fooObjC", resolvedObjCAnnotation.objCName)
|
||||
assertEquals("fooSwift", resolvedObjCAnnotation.swiftName)
|
||||
assertTrue(resolvedObjCAnnotation.isExact)
|
||||
|
||||
Reference in New Issue
Block a user