Simplify CallTypeExtractor implementations
This commit is contained in:
committed by
Yan Zhulanow
parent
f98d50265b
commit
b9ae33b068
@@ -3,6 +3,7 @@ package com.intellij.debugger.streams.kotlin.psi
|
||||
|
||||
import com.intellij.debugger.streams.trace.impl.handler.type.GenericType
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
/**
|
||||
* @author Vitaliy.Bibaev
|
||||
@@ -13,4 +14,18 @@ interface CallTypeExtractor {
|
||||
|
||||
data class IntermediateCallTypes(val typeBefore: GenericType, val typeAfter: GenericType)
|
||||
data class TerminatorCallTypes(val typeBefore: GenericType, val resultType: GenericType)
|
||||
|
||||
abstract class Base : CallTypeExtractor {
|
||||
override fun extractIntermediateCallTypes(call: KtCallExpression): IntermediateCallTypes =
|
||||
IntermediateCallTypes(extractItemsType(call.receiverType()), extractItemsType(call.resolveType()))
|
||||
|
||||
|
||||
override fun extractTerminalCallTypes(call: KtCallExpression): TerminatorCallTypes =
|
||||
TerminatorCallTypes(extractItemsType(call.receiverType()), getResultType(call.resolveType()))
|
||||
|
||||
|
||||
protected abstract fun extractItemsType(type: KotlinType?): GenericType
|
||||
protected abstract fun getResultType(type: KotlinType): GenericType
|
||||
|
||||
}
|
||||
}
|
||||
+7
-19
@@ -2,25 +2,20 @@
|
||||
package com.intellij.debugger.streams.kotlin.psi.collections
|
||||
|
||||
import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor
|
||||
import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor.IntermediateCallTypes
|
||||
import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor.TerminatorCallTypes
|
||||
import com.intellij.debugger.streams.kotlin.psi.KotlinPsiUtil
|
||||
import com.intellij.debugger.streams.kotlin.psi.receiverType
|
||||
import com.intellij.debugger.streams.kotlin.psi.resolveType
|
||||
import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes
|
||||
import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes.ANY
|
||||
import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes.NULLABLE_ANY
|
||||
import com.intellij.debugger.streams.trace.impl.handler.type.ArrayType
|
||||
import com.intellij.debugger.streams.trace.impl.handler.type.GenericType
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.typeUtil.supertypes
|
||||
|
||||
/**
|
||||
* @author Vitaliy.Bibaev
|
||||
*/
|
||||
class KotlinCollectionsTypeExtractor : CallTypeExtractor {
|
||||
class KotlinCollectionsTypeExtractor : CallTypeExtractor.Base() {
|
||||
private companion object {
|
||||
val LOG = Logger.getInstance(KotlinCollectionsTypeExtractor::class.java)
|
||||
val primitives: Set<GenericType> = setOf(KotlinTypes.BOOLEAN, KotlinTypes.BYTE, KotlinTypes.INT, KotlinTypes.SHORT,
|
||||
@@ -28,19 +23,17 @@ class KotlinCollectionsTypeExtractor : CallTypeExtractor {
|
||||
val primitiveArrays: Set<ArrayType> = primitives.map(KotlinTypes::array).toSet()
|
||||
}
|
||||
|
||||
override fun extractIntermediateCallTypes(call: KtCallExpression): IntermediateCallTypes =
|
||||
IntermediateCallTypes(extractItemsType(call.receiverType()), extractItemsType(call.resolveType()))
|
||||
|
||||
|
||||
override fun extractTerminalCallTypes(call: KtCallExpression): TerminatorCallTypes =
|
||||
TerminatorCallTypes(extractItemsType(call.receiverType()), getResultType(call.resolveType()))
|
||||
|
||||
private fun extractItemsType(type: KotlinType?): GenericType {
|
||||
override fun extractItemsType(type: KotlinType?): GenericType {
|
||||
if (type == null) return NULLABLE_ANY
|
||||
|
||||
return tryToFindElementType(type) ?: defaultType(type)
|
||||
}
|
||||
|
||||
override fun getResultType(type: KotlinType): GenericType {
|
||||
val typeName = KotlinPsiUtil.getTypeWithoutTypeParameters(type)
|
||||
return tryToGetPrimitiveByName(typeName) ?: tryToGetPrimitiveArrayByName(typeName) ?: getAny(type)
|
||||
}
|
||||
|
||||
private fun tryToFindElementType(type: KotlinType): GenericType? {
|
||||
val typeName = KotlinPsiUtil.getTypeWithoutTypeParameters(type)
|
||||
if (typeName == "kotlin.collections.Iterable" || typeName == "kotlin.Array") {
|
||||
@@ -65,11 +58,6 @@ class KotlinCollectionsTypeExtractor : CallTypeExtractor {
|
||||
LOG.warn("Could not find type of items for type ${KotlinPsiUtil.getTypeName(type)}")
|
||||
return getAny(type)
|
||||
}
|
||||
|
||||
private fun getResultType(type: KotlinType): GenericType {
|
||||
val typeName = KotlinPsiUtil.getTypeWithoutTypeParameters(type)
|
||||
return tryToGetPrimitiveByName(typeName) ?: tryToGetPrimitiveArrayByName(typeName) ?: getAny(type)
|
||||
}
|
||||
|
||||
private fun getAny(type: KotlinType): GenericType = if (type.isMarkedNullable) NULLABLE_ANY else ANY
|
||||
|
||||
|
||||
+3
-17
@@ -2,33 +2,19 @@
|
||||
package com.intellij.debugger.streams.kotlin.psi.java
|
||||
|
||||
import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor
|
||||
import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor.IntermediateCallTypes
|
||||
import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor.TerminatorCallTypes
|
||||
import com.intellij.debugger.streams.kotlin.psi.KotlinPsiUtil
|
||||
import com.intellij.debugger.streams.kotlin.psi.receiverType
|
||||
import com.intellij.debugger.streams.kotlin.psi.resolveType
|
||||
import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes
|
||||
import com.intellij.debugger.streams.trace.impl.handler.type.ClassTypeImpl
|
||||
import com.intellij.debugger.streams.trace.impl.handler.type.GenericType
|
||||
import com.intellij.psi.CommonClassNames
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.typeUtil.getImmediateSuperclassNotAny
|
||||
|
||||
/**
|
||||
* @author Vitaliy.Bibaev
|
||||
*/
|
||||
class JavaStreamChainTypeExtractor : CallTypeExtractor {
|
||||
override fun extractIntermediateCallTypes(call: KtCallExpression): IntermediateCallTypes {
|
||||
val resultType = call.resolveType()
|
||||
val receiverType = call.receiverType()
|
||||
return IntermediateCallTypes(extractItemsType(receiverType), extractItemsType(resultType))
|
||||
}
|
||||
|
||||
override fun extractTerminalCallTypes(call: KtCallExpression): TerminatorCallTypes =
|
||||
TerminatorCallTypes(extractItemsType(call.receiverType()), getResultType(call.resolveType()))
|
||||
|
||||
private fun extractItemsType(type: KotlinType?): GenericType {
|
||||
class JavaStreamChainTypeExtractor : CallTypeExtractor.Base() {
|
||||
override fun extractItemsType(type: KotlinType?): GenericType {
|
||||
if (type == null) {
|
||||
return KotlinTypes.NULLABLE_ANY
|
||||
}
|
||||
@@ -42,5 +28,5 @@ class JavaStreamChainTypeExtractor : CallTypeExtractor {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getResultType(type: KotlinType): GenericType = ClassTypeImpl(KotlinPsiUtil.getTypeName(type))
|
||||
override fun getResultType(type: KotlinType): GenericType = ClassTypeImpl(KotlinPsiUtil.getTypeName(type))
|
||||
}
|
||||
+5
-4
@@ -2,17 +2,18 @@
|
||||
package com.intellij.debugger.streams.kotlin.psi.sequence
|
||||
|
||||
import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import com.intellij.debugger.streams.trace.impl.handler.type.GenericType
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
/**
|
||||
* @author Vitaliy.Bibaev
|
||||
*/
|
||||
class SequenceTypeExtractor : CallTypeExtractor {
|
||||
override fun extractIntermediateCallTypes(call: KtCallExpression): CallTypeExtractor.IntermediateCallTypes {
|
||||
class SequenceTypeExtractor : CallTypeExtractor.Base() {
|
||||
override fun extractItemsType(type: KotlinType?): GenericType {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
override fun extractTerminalCallTypes(call: KtCallExpression): CallTypeExtractor.TerminatorCallTypes {
|
||||
override fun getResultType(type: KotlinType): GenericType {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user