Reformat treeBasedTypes.kt

This commit is contained in:
Dmitriy Novozhilov
2022-08-09 16:45:39 +03:00
parent b32f9dcac2
commit 1f2f6f4067
@@ -28,20 +28,22 @@ import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
import javax.lang.model.type.TypeKind import javax.lang.model.type.TypeKind
abstract class TreeBasedType<out T : JCTree>( abstract class TreeBasedType<out T : JCTree>(
val tree: T, val tree: T,
val compilationUnit: CompilationUnitTree, val compilationUnit: CompilationUnitTree,
val javac: JavacWrapper, val javac: JavacWrapper,
private val allAnnotations: Collection<JavaAnnotation>, private val allAnnotations: Collection<JavaAnnotation>,
protected val containingElement: JavaElement protected val containingElement: JavaElement
) : JavaType, JavaAnnotationOwner { ) : JavaType, JavaAnnotationOwner {
override val annotations: Collection<JavaAnnotation> override val annotations: Collection<JavaAnnotation>
get() = allAnnotations.filterTypeAnnotations() get() = allAnnotations.filterTypeAnnotations()
companion object { companion object {
fun create(tree: JCTree, compilationUnit: CompilationUnitTree, fun create(
javac: JavacWrapper, annotations: Collection<JavaAnnotation>, tree: JCTree, compilationUnit: CompilationUnitTree,
containingElement: JavaElement): JavaType { javac: JavacWrapper, annotations: Collection<JavaAnnotation>,
containingElement: JavaElement
): JavaType {
return when (tree) { return when (tree) {
is JCTree.JCPrimitiveTypeTree -> TreeBasedPrimitiveType(tree, compilationUnit, javac, annotations, containingElement) is JCTree.JCPrimitiveTypeTree -> TreeBasedPrimitiveType(tree, compilationUnit, javac, annotations, containingElement)
is JCTree.JCArrayTypeTree -> TreeBasedArrayType(tree, compilationUnit, javac, annotations, containingElement) is JCTree.JCArrayTypeTree -> TreeBasedArrayType(tree, compilationUnit, javac, annotations, containingElement)
@@ -50,9 +52,10 @@ abstract class TreeBasedType<out T : JCTree>(
is JCTree.JCAnnotatedType -> { is JCTree.JCAnnotatedType -> {
val underlyingType = tree.underlyingType val underlyingType = tree.underlyingType
val newAnnotations = tree.annotations val newAnnotations = tree.annotations
.map { TreeBasedAnnotation(it, compilationUnit, javac, containingElement) } .map { TreeBasedAnnotation(it, compilationUnit, javac, containingElement) }
create(underlyingType, compilationUnit, javac, newAnnotations, containingElement) create(underlyingType, compilationUnit, javac, newAnnotations, containingElement)
} }
is JCTree.JCExpression -> TreeBasedNonGenericClassifierType(tree, compilationUnit, javac, annotations, containingElement) is JCTree.JCExpression -> TreeBasedNonGenericClassifierType(tree, compilationUnit, javac, annotations, containingElement)
else -> throw UnsupportedOperationException("Unsupported type: $tree") else -> throw UnsupportedOperationException("Unsupported type: $tree")
} }
@@ -73,29 +76,28 @@ abstract class TreeBasedType<out T : JCTree>(
} }
class TreeBasedPrimitiveType( class TreeBasedPrimitiveType(
tree: JCTree.JCPrimitiveTypeTree, tree: JCTree.JCPrimitiveTypeTree,
compilationUnit: CompilationUnitTree, compilationUnit: CompilationUnitTree,
javac: JavacWrapper, javac: JavacWrapper,
allAnnotations: Collection<JavaAnnotation>, allAnnotations: Collection<JavaAnnotation>,
containingElement: JavaElement containingElement: JavaElement
) : TreeBasedType<JCTree.JCPrimitiveTypeTree>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaPrimitiveType { ) : TreeBasedType<JCTree.JCPrimitiveTypeTree>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaPrimitiveType {
override val type: PrimitiveType? override val type: PrimitiveType?
get() = if (tree.primitiveTypeKind == TypeKind.VOID) { get() = if (tree.primitiveTypeKind == TypeKind.VOID) {
null null
} } else {
else {
JvmPrimitiveType.get(tree.toString()).primitiveType JvmPrimitiveType.get(tree.toString()).primitiveType
} }
} }
class TreeBasedArrayType( class TreeBasedArrayType(
tree: JCTree.JCArrayTypeTree, tree: JCTree.JCArrayTypeTree,
compilationUnit: CompilationUnitTree, compilationUnit: CompilationUnitTree,
javac: JavacWrapper, javac: JavacWrapper,
allAnnotations: Collection<JavaAnnotation>, allAnnotations: Collection<JavaAnnotation>,
containingElement: JavaElement containingElement: JavaElement
) : TreeBasedType<JCTree.JCArrayTypeTree>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaArrayType { ) : TreeBasedType<JCTree.JCArrayTypeTree>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaArrayType {
override val componentType: JavaType override val componentType: JavaType
@@ -104,11 +106,11 @@ class TreeBasedArrayType(
} }
class TreeBasedWildcardType( class TreeBasedWildcardType(
tree: JCTree.JCWildcard, tree: JCTree.JCWildcard,
compilationUnit: CompilationUnitTree, compilationUnit: CompilationUnitTree,
javac: JavacWrapper, javac: JavacWrapper,
allAnnotations: Collection<JavaAnnotation>, allAnnotations: Collection<JavaAnnotation>,
containingElement: JavaElement containingElement: JavaElement
) : TreeBasedType<JCTree.JCWildcard>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaWildcardType { ) : TreeBasedType<JCTree.JCWildcard>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaWildcardType {
override val bound: JavaType? override val bound: JavaType?
@@ -120,11 +122,11 @@ class TreeBasedWildcardType(
} }
sealed class TreeBasedClassifierType<out T : JCTree>( sealed class TreeBasedClassifierType<out T : JCTree>(
tree: T, tree: T,
compilationUnit: CompilationUnitTree, compilationUnit: CompilationUnitTree,
javac: JavacWrapper, javac: JavacWrapper,
allAnnotations: Collection<JavaAnnotation>, allAnnotations: Collection<JavaAnnotation>,
containingElement: JavaElement containingElement: JavaElement
) : TreeBasedType<T>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaClassifierType { ) : TreeBasedType<T>(tree, compilationUnit, javac, allAnnotations, containingElement), JavaClassifierType {
override val classifier: JavaClassifier? override val classifier: JavaClassifier?
@@ -145,8 +147,7 @@ sealed class TreeBasedClassifierType<out T : JCTree>(
if (tree is JCTree.JCFieldAccess) { if (tree is JCTree.JCFieldAccess) {
val enclosingType = TreeBasedType.create(tree.selected, compilationUnit, javac, annotations, containingElement) val enclosingType = TreeBasedType.create(tree.selected, compilationUnit, javac, annotations, containingElement)
return (enclosingType as? JavaClassifierType)?.typeArguments ?: emptyList() return (enclosingType as? JavaClassifierType)?.typeArguments ?: emptyList()
} } else {
else {
val classifier = classifier as? JavaClass ?: return emptyList() val classifier = classifier as? JavaClass ?: return emptyList()
if (classifier is MockKotlinClassifier || classifier.isStatic) return emptyList() if (classifier is MockKotlinClassifier || classifier.isStatic) return emptyList()
@@ -190,26 +191,26 @@ class TreeBasedTypeParameterType(override val classifier: JavaTypeParameter) : J
} }
class TreeBasedNonGenericClassifierType( class TreeBasedNonGenericClassifierType(
tree: JCTree.JCExpression, tree: JCTree.JCExpression,
compilationUnit: CompilationUnitTree, compilationUnit: CompilationUnitTree,
javac: JavacWrapper, javac: JavacWrapper,
annotations: Collection<JavaAnnotation>, annotations: Collection<JavaAnnotation>,
containingElement: JavaElement containingElement: JavaElement
) : TreeBasedClassifierType<JCTree.JCExpression>(tree, compilationUnit, javac, annotations, containingElement) { ) : TreeBasedClassifierType<JCTree.JCExpression>(tree, compilationUnit, javac, annotations, containingElement) {
override val isRaw: Boolean override val isRaw: Boolean
get() = (classifier as? MockKotlinClassifier)?.hasTypeParameters get() = (classifier as? MockKotlinClassifier)?.hasTypeParameters
?: (classifier as? JavaClass)?.typeParameters?.isNotEmpty() ?: (classifier as? JavaClass)?.typeParameters?.isNotEmpty()
?: false ?: false
} }
class TreeBasedGenericClassifierType( class TreeBasedGenericClassifierType(
tree: JCTree.JCTypeApply, tree: JCTree.JCTypeApply,
compilationUnit: CompilationUnitTree, compilationUnit: CompilationUnitTree,
javac: JavacWrapper, javac: JavacWrapper,
annotations: Collection<JavaAnnotation>, annotations: Collection<JavaAnnotation>,
containingElement: JavaElement containingElement: JavaElement
) : TreeBasedClassifierType<JCTree.JCTypeApply>(tree, compilationUnit, javac, annotations, containingElement) { ) : TreeBasedClassifierType<JCTree.JCTypeApply>(tree, compilationUnit, javac, annotations, containingElement) {
override val classifier: JavaClassifier? override val classifier: JavaClassifier?
@@ -217,23 +218,29 @@ class TreeBasedGenericClassifierType(
val newTree = tree.clazz val newTree = tree.clazz
if (newTree is JCTree.JCAnnotatedType) { if (newTree is JCTree.JCAnnotatedType) {
javac.resolve(newTree.underlyingType, compilationUnit, containingElement) javac.resolve(newTree.underlyingType, compilationUnit, containingElement)
} } else super.classifier
else super.classifier
} }
override val annotations: Collection<JavaAnnotation> override val annotations: Collection<JavaAnnotation>
get() { get() {
val newTree = tree.clazz val newTree = tree.clazz
return (newTree as? JCTree.JCAnnotatedType)?.annotations?.map { TreeBasedAnnotation(it, compilationUnit, javac, containingElement) } return (newTree as? JCTree.JCAnnotatedType)?.annotations?.map {
?.toMutableList<JavaAnnotation>() TreeBasedAnnotation(
?.apply { addAll(super.annotations) } it,
?: super.annotations compilationUnit,
javac,
containingElement
)
}
?.toMutableList<JavaAnnotation>()
?.apply { addAll(super.annotations) }
?: super.annotations
} }
override val typeArguments: List<JavaType?> override val typeArguments: List<JavaType?>
get() = tree.arguments.map { create(it, compilationUnit, javac, emptyList(), containingElement) } get() = tree.arguments.map { create(it, compilationUnit, javac, emptyList(), containingElement) }
.toMutableList<JavaType?>() .toMutableList<JavaType?>()
.apply { addAll(super.typeArguments) } .apply { addAll(super.typeArguments) }
override val isRaw: Boolean override val isRaw: Boolean
get() = classifier.let { get() = classifier.let {
@@ -243,4 +250,4 @@ class TreeBasedGenericClassifierType(
} }
} }
} }