Skip to content

Commit 30e89c3

Browse files
committed
feat: kotlin support
1 parent b222741 commit 30e89c3

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/detect/endpoint/VertxEndpoint.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class VertxEndpoint : JVMEndpointDetector.JVMEndpointNameDetector {
9292
val importRegex = Regex("""import io\.vertx""")
9393
importRegex.find(artifact.containingFile.text) ?: return
9494

95-
val regex = Regex("""router\.([a-zA-Z]+)\("([^"]+)"\)\.handler\(this::([a-zA-Z]+)\)""")
95+
val regex = Regex("""router\.([a-zA-Z]+)\("([^"]+)"\)\.handler\s?[({]\s?(?:this::)?([a-zA-Z]+).+""")
9696
val match = regex.matchEntire(artifact.text) ?: return
9797
val httpMethod = match.groupValues[1].uppercase()
9898
val endpointName = match.groupValues[2]

marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/utils/JVMMarkerUtils.kt

+38-23
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@ package spp.jetbrains.marker.jvm.service.utils
1919
import com.intellij.lang.jvm.util.JvmClassUtil
2020
import com.intellij.openapi.diagnostic.logger
2121
import com.intellij.psi.*
22+
import com.intellij.psi.util.PsiTreeUtil
2223
import com.intellij.psi.util.PsiUtil
2324
import org.jetbrains.kotlin.backend.jvm.ir.psiElement
2425
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
2526
import org.jetbrains.kotlin.descriptors.CallableDescriptor
2627
import org.jetbrains.kotlin.idea.base.psi.KotlinPsiHeuristics
2728
import org.jetbrains.kotlin.idea.base.utils.fqname.fqName
2829
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
29-
import org.jetbrains.kotlin.psi.KtClass
30-
import org.jetbrains.kotlin.psi.KtFile
31-
import org.jetbrains.kotlin.psi.KtFunction
32-
import org.jetbrains.kotlin.psi.KtNamedFunction
30+
import org.jetbrains.kotlin.psi.*
3331
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
32+
import org.jetbrains.kotlin.types.KotlinType
3433
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
3534
import spp.jetbrains.artifact.service.*
3635
import spp.jetbrains.marker.SourceMarkerUtils
@@ -240,25 +239,10 @@ object JVMMarkerUtils {
240239
if (methodParams.isNotEmpty()) {
241240
methodParams += ","
242241
}
243-
val paramType = (it.resolveToDescriptorIfAny() as? CallableDescriptor)?.returnType
244-
val qualifiedType = if (paramType != null && KotlinBuiltIns.isPrimitiveArray(paramType)) {
245-
val arrayType = KotlinBuiltIns.getPrimitiveArrayElementType(paramType)
246-
arrayType?.let { JvmPrimitiveType.get(it).javaKeywordName + "[]" }
247-
} else if (paramType != null && KotlinBuiltIns.isArray(paramType)
248-
&& paramType.arguments.firstOrNull()?.type?.fqName?.asString() == "kotlin.String"
249-
) {
250-
"java.lang.String[]"
251-
} else {
252-
paramType?.let {
253-
KotlinBuiltIns.getPrimitiveType(it)?.let { JvmPrimitiveType.get(it) }?.javaKeywordName
254-
} ?: if (paramType != null && KotlinBuiltIns.isString(paramType)) {
255-
"java.lang.String"
256-
} else if (paramType?.unwrap()?.constructor?.declarationDescriptor?.psiElement is KtClass) {
257-
val clazz = paramType.unwrap().constructor.declarationDescriptor?.psiElement as KtClass
258-
getFullyQualifiedName(clazz).identifier
259-
} else {
260-
paramType?.fqName?.toString()?.replace(".", "$")
261-
}
242+
val qualifiedType = try {
243+
(it.resolveToDescriptorIfAny() as? CallableDescriptor)?.returnType?.let { getQualifiedName(it) }
244+
} catch (ignore: Exception) {
245+
fallbackImportScan(method, it.typeReference)
262246
}
263247
if (qualifiedType != null) {
264248
methodParams += qualifiedType
@@ -274,6 +258,37 @@ object JVMMarkerUtils {
274258
return "$methodName($methodParams)"
275259
}
276260

261+
private fun fallbackImportScan(element: KtElement, typeReference: KtTypeReference?): String? {
262+
if (typeReference == null) return null
263+
val simpleName = typeReference.text.split("<")[0]
264+
val ktFile = PsiTreeUtil.getParentOfType(element, KtFile::class.java) ?: return null
265+
val importDirective = ktFile.importDirectives.find { it.importedFqName?.shortName()?.asString() == simpleName }
266+
return importDirective?.importedFqName?.asString()
267+
}
268+
269+
private fun getQualifiedName(paramType: KotlinType): String? {
270+
val qualifiedType = if (KotlinBuiltIns.isPrimitiveArray(paramType)) {
271+
val arrayType = KotlinBuiltIns.getPrimitiveArrayElementType(paramType)
272+
arrayType?.let { JvmPrimitiveType.get(it).javaKeywordName + "[]" }
273+
} else if (KotlinBuiltIns.isArray(paramType) &&
274+
paramType.arguments.firstOrNull()?.type?.fqName?.asString() == "kotlin.String"
275+
) {
276+
"java.lang.String[]"
277+
} else {
278+
paramType.let {
279+
KotlinBuiltIns.getPrimitiveType(it)?.let { JvmPrimitiveType.get(it) }?.javaKeywordName
280+
} ?: if (KotlinBuiltIns.isString(paramType)) {
281+
"java.lang.String"
282+
} else if (paramType.unwrap().constructor.declarationDescriptor?.psiElement is KtClass) {
283+
val clazz = paramType.unwrap().constructor.declarationDescriptor?.psiElement as KtClass
284+
getFullyQualifiedName(clazz).identifier
285+
} else {
286+
paramType.fqName?.toString()?.replace(".", "$")
287+
}
288+
}
289+
return qualifiedType
290+
}
291+
277292
private fun getArrayDimensions(s: String): Int {
278293
var arrayDimensions = 0
279294
for (element in s) {

0 commit comments

Comments
 (0)