@@ -19,18 +19,17 @@ package spp.jetbrains.marker.jvm.service.utils
1919import com.intellij.lang.jvm.util.JvmClassUtil
2020import com.intellij.openapi.diagnostic.logger
2121import com.intellij.psi.*
22+ import com.intellij.psi.util.PsiTreeUtil
2223import com.intellij.psi.util.PsiUtil
2324import org.jetbrains.kotlin.backend.jvm.ir.psiElement
2425import org.jetbrains.kotlin.builtins.KotlinBuiltIns
2526import org.jetbrains.kotlin.descriptors.CallableDescriptor
2627import org.jetbrains.kotlin.idea.base.psi.KotlinPsiHeuristics
2728import org.jetbrains.kotlin.idea.base.utils.fqname.fqName
2829import 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.*
3331import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
32+ import org.jetbrains.kotlin.types.KotlinType
3433import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
3534import spp.jetbrains.artifact.service.*
3635import 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