Uast: Ability to get the default class type from UClass

This commit is contained in:
Yan Zhulanow
2016-03-15 20:33:44 +03:00
parent 5c07e6de9d
commit 78ce025ca3
7 changed files with 32 additions and 19 deletions
@@ -18,9 +18,13 @@ package org.jetbrains.uast
import org.jetbrains.uast.kinds.UastClassKind
interface UClass : UDeclaration, UFqNamed, UModifierOwner, UAnnotated {
/* The simple class name is only for the debug purposes. Do not check against it in the production code */
override val name: String
val isAnonymous: Boolean
val visibility: UastVisibility
val kind: UastClassKind
val defaultType: UType
val companions: List<UClass>
@@ -78,7 +82,7 @@ object UClassNotResolved : UClass {
override val declarations = emptyList<UDeclaration>()
override fun isSubclassOf(name: String) = false
override val companions = emptyList<UClass>()
override val defaultType = UastErrorType
override val nameElement = null
override val parent = null
override val name = "<class not resolved>"
@@ -34,4 +34,13 @@ interface UTypeReference : UDeclaration, UResolvable, NoTraverse {
override fun resolve(context: UastContext): UClass?
override fun resolveOrEmpty(context: UastContext) = resolve(context) ?: UClassNotResolved
}
object UastErrorType : UType, NoAnnotations {
override val isInt = false
override val parent = null
override val name = "<error>"
override val fqName = null
override val isBoolean = false
override fun resolve(context: UastContext) = null
}
@@ -16,6 +16,7 @@
package org.jetbrains.uast.java
import com.intellij.psi.*
import com.intellij.psi.util.PsiTypesUtil
import org.jetbrains.uast.*
import org.jetbrains.uast.kinds.UastClassKind
import org.jetbrains.uast.psi.PsiElementBacked
@@ -50,6 +51,8 @@ class JavaUClass(
}
}
override val defaultType by lz { JavaConverter.convert(PsiTypesUtil.getClassType(psi), this) }
override val companions: List<UClass>
get() = emptyList()
@@ -169,9 +169,9 @@ internal object KotlinConverter : UastConverter {
}
internal fun convert(typeReference: KtTypeReference?, parent: UElement): UType {
if (typeReference == null) return KotlinUErrorType
if (typeReference == null) return UastErrorType
val bindingContext = typeReference.analyze(BodyResolveMode.PARTIAL)
val type = bindingContext[BindingContext.TYPE, typeReference] ?: return KotlinUErrorType
val type = bindingContext[BindingContext.TYPE, typeReference] ?: return UastErrorType
return convert(type, typeReference.project, parent)
}
@@ -66,6 +66,11 @@ class KotlinUClass(
}
}
override val defaultType by lz {
val type = resolveToDescriptor()?.defaultType ?: return@lz UastErrorType
KotlinConverter.convert(type, psi.project, this)
}
override val companions by lz {
(psi as? KtClass)?.getCompanionObjects()?.map { KotlinConverter.convert(it, this) } ?: emptyList()
}
@@ -46,13 +46,4 @@ class KotlinUType(
//TODO
override val annotations = emptyList<UAnnotation>()
}
object KotlinUErrorType : UType, NoAnnotations {
override val isInt = false
override val parent = null
override val name = "<error>"
override val fqName = null
override val isBoolean = false
override fun resolve(context: UastContext) = null
}
@@ -37,8 +37,8 @@ open class KotlinUVariable(
override val initializer by lz { KotlinConverter.convertOrEmpty(psi.initializer, this) }
override val type by lz {
val descriptor = psi.resolveToDescriptorIfAny() as? CallableDescriptor ?: return@lz KotlinUErrorType
val type = descriptor.returnType ?: return@lz KotlinUErrorType
val descriptor = psi.resolveToDescriptorIfAny() as? CallableDescriptor ?: return@lz UastErrorType
val type = descriptor.returnType ?: return@lz UastErrorType
KotlinConverter.convert(type, psi.project, this)
}
@@ -58,11 +58,12 @@ class KotlinDestructuredUVariable(
parent: UElement
) : KotlinUVariable(entry, parent) {
override lateinit var initializer: UExpression
internal set
override val type by lz {
val bindingContext = entry.analyze(BodyResolveMode.PARTIAL)
val resolvedCall = bindingContext[BindingContext.COMPONENT_RESOLVED_CALL, entry] ?: return@lz KotlinUErrorType
val returnType = resolvedCall.resultingDescriptor.returnType ?: return@lz KotlinUErrorType
val resolvedCall = bindingContext[BindingContext.COMPONENT_RESOLVED_CALL, entry] ?: return@lz UastErrorType
val returnType = resolvedCall.resultingDescriptor.returnType ?: return@lz UastErrorType
KotlinConverter.convert(returnType, entry.project, this)
}
}
@@ -75,7 +76,7 @@ class KotlinDestructuringUVariable(
override val initializer by lz { KotlinConverter.convertOrEmpty(psi.initializer, this) }
override val kind = UastVariableKind.LOCAL_VARIABLE
override val type: UType
get() = initializer.getExpressionType() ?: KotlinUErrorType
get() = initializer.getExpressionType() ?: UastErrorType
override val nameElement = null
override fun hasModifier(modifier: UastModifier) = false
@@ -91,13 +92,13 @@ class KotlinParameterUVariable(
override val nameElement by lz { KotlinConverter.asSimpleReference(psi.nameIdentifier, this) }
override val initializer by lz { KotlinConverter.convertOrEmpty(psi.defaultValue, this) }
override val initializer by lz { KotlinConverter.convert(psi.defaultValue, this) as? UExpression }
override val kind = UastVariableKind.VALUE_PARAMETER
override val type by lz {
val bindingContext = psi.analyze(BodyResolveMode.PARTIAL)
val param = bindingContext[BindingContext.VALUE_PARAMETER, psi] ?: return@lz KotlinUErrorType
val param = bindingContext[BindingContext.VALUE_PARAMETER, psi] ?: return@lz UastErrorType
KotlinConverter.convert(param.type, psi.project, this)
}