Uast: Support all basic Java types needed for Lint
This commit is contained in:
@@ -18,8 +18,17 @@ package org.jetbrains.uast
|
||||
interface UType : UElement, UNamed, UFqNamed, UAnnotated, UResolvable, NoTraverse {
|
||||
override fun matchesName(name: String) = this.name == name || this.name.endsWith(".$name")
|
||||
|
||||
/* The simple type name is only for the debug purposes. Do not check against it in the production code */
|
||||
override val name: String
|
||||
|
||||
/* Semantics: returns true if the type is either a boxed or an unboxed, false otherwise */
|
||||
val isInt: Boolean
|
||||
val isLong: Boolean
|
||||
val isFloat: Boolean
|
||||
val isDouble: Boolean
|
||||
val isChar: Boolean
|
||||
val isBoolean: Boolean
|
||||
val isByte: Boolean
|
||||
|
||||
override fun logString() = "UType ($name)"
|
||||
override fun renderString() = name
|
||||
@@ -38,6 +47,11 @@ interface UTypeReference : UDeclaration, UResolvable, NoTraverse {
|
||||
|
||||
object UastErrorType : UType, NoAnnotations {
|
||||
override val isInt = false
|
||||
override val isLong = false
|
||||
override val isFloat = false
|
||||
override val isDouble = false
|
||||
override val isChar = false
|
||||
override val isByte = false
|
||||
override val parent = null
|
||||
override val name = "<error>"
|
||||
override val fqName = null
|
||||
|
||||
@@ -39,10 +39,28 @@ class JavaUType(
|
||||
}
|
||||
|
||||
override val isInt: Boolean
|
||||
get() = name == "int"
|
||||
get() = check("int", "java.lang.Integer")
|
||||
|
||||
override val isLong: Boolean
|
||||
get() = check("long", "java.lang.Long")
|
||||
|
||||
override val isFloat: Boolean
|
||||
get() = check("float", "java.lang.Float")
|
||||
|
||||
override val isDouble: Boolean
|
||||
get() = check("double", "java.lang.Double")
|
||||
|
||||
override val isChar: Boolean
|
||||
get() = check("char", "java.lang.Character")
|
||||
|
||||
override val isBoolean: Boolean
|
||||
get() = name == "boolean"
|
||||
get() = check("boolean", "java.lang.Boolean")
|
||||
|
||||
override val isByte: Boolean
|
||||
get() = check("byte", "java.lang.Byte")
|
||||
|
||||
private inline fun check(unboxedType: String, boxedType: String): Boolean =
|
||||
name == unboxedType || (psi as? PsiClassType)?.resolve()?.qualifiedName == boxedType
|
||||
|
||||
override val annotations by lz { psi.getAnnotations(this) }
|
||||
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
package org.jetbrains.kotlin.uast
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.typeUtil.isBooleanOrNullableBoolean
|
||||
import org.jetbrains.uast.*
|
||||
|
||||
class KotlinUType(
|
||||
@@ -42,8 +45,30 @@ class KotlinUType(
|
||||
}
|
||||
|
||||
override val isBoolean: Boolean
|
||||
get() = type.isBooleanOrNullableBoolean()
|
||||
get() = checkType(KotlinBuiltIns.FQ_NAMES._boolean)
|
||||
|
||||
//TODO
|
||||
override val isLong: Boolean
|
||||
get() = checkType(KotlinBuiltIns.FQ_NAMES._long)
|
||||
|
||||
override val isFloat: Boolean
|
||||
get() = checkType(KotlinBuiltIns.FQ_NAMES._float)
|
||||
|
||||
override val isDouble: Boolean
|
||||
get() = checkType(KotlinBuiltIns.FQ_NAMES._double)
|
||||
|
||||
override val isChar: Boolean
|
||||
get() = checkType(KotlinBuiltIns.FQ_NAMES._char)
|
||||
|
||||
override val isByte: Boolean
|
||||
get() = checkType(KotlinBuiltIns.FQ_NAMES._byte)
|
||||
|
||||
private fun checkType(fqNameUnsafe: FqNameUnsafe): Boolean {
|
||||
val descriptor = type.constructor.declarationDescriptor
|
||||
return descriptor is ClassDescriptor
|
||||
&& descriptor.getName() == fqNameUnsafe.shortName()
|
||||
&& fqNameUnsafe == DescriptorUtils.getFqName(descriptor)
|
||||
}
|
||||
|
||||
//TODO support descriptor annotations
|
||||
override val annotations = emptyList<UAnnotation>()
|
||||
}
|
||||
Reference in New Issue
Block a user