Kapt: Refactor kapt type wrappers. Now every ordinary (not NoType) type has a backed PsiType so it's easy to call utility functions from IntelliJ.

(cherry picked from commit 0a684aa)
This commit is contained in:
Yan Zhulanow
2016-06-23 20:16:07 +03:00
committed by Yan Zhulanow
parent 84d62f37eb
commit 6eb3d7e002
24 changed files with 203 additions and 170 deletions
@@ -643,6 +643,10 @@ class GradleMessageCollector(val logger: Logger, val outputCollector: OutputItem
override fun hasErrors() = hasErrors override fun hasErrors() = hasErrors
override fun clear() {
// Do nothing
}
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) { override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
val text = with(StringBuilder()) { val text = with(StringBuilder()) {
append(when (severity) { append(when (severity) {
@@ -50,6 +50,11 @@ public class MavenPluginLogMessageCollector implements MessageCollector {
return Collections.unmodifiableList(collectedErrors); return Collections.unmodifiableList(collectedErrors);
} }
@Override
public void clear() {
// Do nothing
}
@Override @Override
public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) { public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
String path = location.getPath(); String path = location.getPath();
@@ -63,7 +63,7 @@ class AnnotationProcessingExtension(
val filer = KotlinFiler(generatedOutputDir) val filer = KotlinFiler(generatedOutputDir)
val messages = KotlinMessager() val messages = KotlinMessager()
val types = KotlinTypes(javaPsiFacade, projectScope) val types = KotlinTypes(javaPsiFacade, PsiManager.getInstance(project), projectScope)
val elements = KotlinElements(javaPsiFacade, projectScope) val elements = KotlinElements(javaPsiFacade, projectScope)
val processingEnvironment = KotlinProcessingEnvironment(elements, types, messages, options, filer) val processingEnvironment = KotlinProcessingEnvironment(elements, types, messages, options, filer)
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.annotation.processing.impl package org.jetbrains.kotlin.annotation.processing.impl
import com.intellij.psi.* import com.intellij.psi.*
import com.intellij.psi.impl.PsiSubstitutorImpl
import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTypesUtil import com.intellij.psi.util.PsiTypesUtil
import com.intellij.psi.util.TypeConversionUtil import com.intellij.psi.util.TypeConversionUtil
@@ -31,40 +30,71 @@ import javax.lang.model.element.TypeElement
import javax.lang.model.type.* import javax.lang.model.type.*
import javax.lang.model.util.Types import javax.lang.model.util.Types
class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope) : Types { class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val psiManager: PsiManager, val scope: GlobalSearchScope) : Types {
override fun contains(t1: TypeMirror, t2: TypeMirror): Boolean { override fun contains(containing: TypeMirror, contained: TypeMirror): Boolean {
t1 as? JeAbstractType ?: return false assertKindNot(containing, TypeKind.PACKAGE, TypeKind.EXECUTABLE)
t2 as? JeAbstractType ?: return false assertKindNot(contained, TypeKind.PACKAGE, TypeKind.EXECUTABLE)
assertJeType(containing); containing as JePsiType
assertJeType(contained); contained as JePsiType
val classType = t1.psiType as? PsiClassType ?: return false fun L(type: PsiType): PsiType = when {
return t2.psiType in classType.parameters type is PsiWildcardType && type.isSuper -> type.superBound
else -> type
}
fun U(type: PsiType): PsiType = when {
type is PsiWildcardType && type.isExtends -> type.extendsBound
else -> type
}
return when (containing) {
is JeArrayType, is JePrimitiveType -> containing == contained
else -> {
!GenericsUtil.checkNotInBounds(L(contained.psiType), L(containing.psiType), false)
&& !GenericsUtil.checkNotInBounds(U(contained.psiType), U(containing.psiType), false)
}
}
} }
override fun getArrayType(componentType: TypeMirror): ArrayType { override fun getArrayType(componentType: TypeMirror): ArrayType {
val psiType = (componentType as? JeAbstractType)?.psiType if (componentType is ExecutableType || componentType is NoType) error(componentType)
?: throw IllegalArgumentException("Invalid component type: $componentType") assertJeType(componentType); componentType as JePsiType
return JeArrayType(PsiArrayType(psiType))
return JeArrayType(PsiArrayType(componentType.psiType), psiManager)
} }
override fun isAssignable(t1: TypeMirror, t2: TypeMirror): Boolean { override fun isAssignable(t1: TypeMirror, t2: TypeMirror): Boolean {
fun error(t: TypeMirror?): Nothing = throw IllegalArgumentException("Invalid type: $t") assertKindNot(t1, TypeKind.PACKAGE, TypeKind.EXECUTABLE, TypeKind.NONE)
if (t1 is ExecutableType || t1 is NoType) error(t1) assertKindNot(t2, TypeKind.PACKAGE, TypeKind.EXECUTABLE, TypeKind.NONE)
if (t2 is ExecutableType || t2 is NoType) error(t2)
t1 as? JePsiType ?: return false
t1 as? JeAbstractType ?: return false t2 as? JePsiType ?: return false
t2 as? JeAbstractType ?: return false return t2.psiType.isAssignableFrom(t1.psiType)
return t1.psiType.isAssignableFrom(t2.psiType)
} }
override fun getNullType() = JeNullType override fun getNullType() = JeNullType
override fun getWildcardType(extendsBound: TypeMirror, superBound: TypeMirror): JeWildcardTypeWithBounds { override fun getWildcardType(extendsBound: TypeMirror?, superBound: TypeMirror?): JeWildcardType {
return JeWildcardTypeWithBounds(extendsBound, superBound) if (extendsBound != null && superBound != null) {
throw IllegalArgumentException("Both extendsBound and superBound should not be specified.")
}
if (extendsBound != null && extendsBound !is JePsiType) illegalArg("extendsBound should have PsiType")
if (superBound != null && superBound !is JePsiType) illegalArg("superBound should have PsiType")
return JeWildcardType(if (extendsBound != null) {
PsiWildcardType.createExtends(psiManager, (extendsBound as JePsiType).psiType)
} else if (superBound != null) {
PsiWildcardType.createSuper(psiManager, (superBound as JePsiType).psiType)
} else {
PsiWildcardType.createUnbounded(psiManager)
})
} }
override fun unboxedType(t: TypeMirror): PrimitiveType? { override fun unboxedType(t: TypeMirror): PrimitiveType? {
fun error(): Nothing = throw IllegalArgumentException("This type could not be unboxed: $t") fun error(): Nothing = throw IllegalArgumentException("This type could not be unboxed: $t")
t as? JeAbstractType ?: error() t as? JePsiType ?: error()
val unboxedType = PsiPrimitiveType.getUnboxedType(t.psiType) ?: error() val unboxedType = PsiPrimitiveType.getUnboxedType(t.psiType) ?: error()
return unboxedType.toJePrimitiveType() return unboxedType.toJePrimitiveType()
} }
@@ -73,14 +103,14 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
override fun erasure(t: TypeMirror): TypeMirror { override fun erasure(t: TypeMirror): TypeMirror {
if (t is NoType) throw IllegalArgumentException("Invalid type: $t") if (t is NoType) throw IllegalArgumentException("Invalid type: $t")
t as? JeAbstractType ?: return t t as? JePsiType ?: return t
return TypeConversionUtil.erasure(t.psiType).toJeType() return TypeConversionUtil.erasure(t.psiType).toJeType(psiManager)
} }
override fun directSupertypes(t: TypeMirror): List<TypeMirror> { override fun directSupertypes(t: TypeMirror): List<TypeMirror> {
if (t is NoType) throw IllegalArgumentException("Invalid type: $t") if (t is NoType) throw IllegalArgumentException("Invalid type: $t")
val psiType = (t as? JeAbstractType)?.psiType as? PsiClassType ?: return emptyList() val psiType = (t as? JePsiType)?.psiType as? PsiClassType ?: return emptyList()
return psiType.superTypes.map { it.toJeType() } return psiType.superTypes.map { it.toJeType(psiManager) }
} }
override fun boxedClass(p: PrimitiveType): TypeElement? { override fun boxedClass(p: PrimitiveType): TypeElement? {
@@ -102,8 +132,8 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
} }
override fun isSubtype(t1: TypeMirror, t2: TypeMirror): Boolean { override fun isSubtype(t1: TypeMirror, t2: TypeMirror): Boolean {
val psiType1 = (t1 as? JeAbstractType)?.psiType ?: return false val psiType1 = (t1 as? JePsiType)?.psiType ?: return false
val psiType2 = (t2 as? JeAbstractType)?.psiType ?: return false val psiType2 = (t2 as? JePsiType)?.psiType ?: return false
return TypeConversionUtil.isAssignable(psiType2, psiType1, false) return TypeConversionUtil.isAssignable(psiType2, psiType1, false)
} }
@@ -130,10 +160,12 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
} }
val typeArgs = typeArgMirrors.mapIndexed { val typeArgs = typeArgMirrors.mapIndexed {
i, t -> (t as? JeAbstractType)?.psiType ?: throw IllegalArgumentException("Invalid type argument #$i: $t") i, t -> (t as? JePsiType)?.psiType ?: throw IllegalArgumentException("Invalid type argument #$i: $t")
} }
return JeCompoundDeclaredType(psiClass, typeElem, null, typeArgs, typeArgMirrors.toList()) val psiType = createDeclaredType(psiClass, typeArgs) ?:
throw IllegalStateException("Can't create declared type ($psiClass, $typeArgs)")
return JeDeclaredType(psiType, psiClass, typeArgumentMirrors = typeArgMirrors.toList())
} }
override fun getDeclaredType( override fun getDeclaredType(
@@ -150,7 +182,7 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
throw IllegalArgumentException("$typeElem is a top-level class element") throw IllegalArgumentException("$typeElem is a top-level class element")
} }
val containingType = (containing as? JeAbstractType)?.psiType as? PsiClassType val containingType = (containing as? JePsiType)?.psiType as? PsiClassType
?: throw IllegalArgumentException("Illegal containing type: $containing") ?: throw IllegalArgumentException("Illegal containing type: $containing")
val containingClass = containingType.resolve() val containingClass = containingType.resolve()
?: throw IllegalArgumentException("Class type can't be resolved: $containing") ?: throw IllegalArgumentException("Class type can't be resolved: $containing")
@@ -160,10 +192,12 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
} }
val typeArgs = typeArgMirrors.mapIndexed { val typeArgs = typeArgMirrors.mapIndexed {
i, t -> (t as? JeAbstractType)?.psiType ?: throw IllegalArgumentException("Invalid type argument #$i: $t") i, t -> (t as? JePsiType)?.psiType ?: throw IllegalArgumentException("Invalid type argument #$i: $t")
} }
return JeCompoundDeclaredType(psiClass, typeElem, containing, typeArgs, typeArgMirrors.toList()) val psiType = createDeclaredType(psiClass, typeArgs) ?:
throw IllegalStateException("Can't create declared type ($psiClass, $typeArgs)")
return JeDeclaredType(psiType, psiClass, containing, typeArgMirrors.toList())
} }
override fun asMemberOf(containing: DeclaredType, element: Element): TypeMirror { override fun asMemberOf(containing: DeclaredType, element: Element): TypeMirror {
@@ -172,10 +206,6 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
val result = containing.psiType.resolveGenerics() val result = containing.psiType.resolveGenerics()
if (result.isValidResult) result.substitutor else PsiSubstitutor.EMPTY if (result.isValidResult) result.substitutor else PsiSubstitutor.EMPTY
} }
is JeCompoundDeclaredType -> {
val mapping = containing.psiClass.typeParameters.zip(containing.typeArgs).toMap()
PsiSubstitutorImpl.createSubstitutor(mapping)
}
else -> throw IllegalArgumentException("Invalid containing type: $containing") else -> throw IllegalArgumentException("Invalid containing type: $containing")
} }
@@ -191,7 +221,7 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
} }
} }
is JeClassInitializerExecutableElement -> element.asType() is JeClassInitializerExecutableElement -> element.asType()
is JeVariableElement -> substitutor.substitute(element.psi.type).toJeType() is JeVariableElement -> substitutor.substitute(element.psi.type).toJeType(psiManager)
else -> throw IllegalArgumentException("Invalid element type: $element") else -> throw IllegalArgumentException("Invalid element type: $element")
} }
} }
@@ -200,16 +230,18 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
val m1 = (t1 as JeClassInitializerExecutableTypeMirror).initializer val m1 = (t1 as JeClassInitializerExecutableTypeMirror).initializer
val m2 = (t2 as JeClassInitializerExecutableTypeMirror).initializer val m2 = (t2 as JeClassInitializerExecutableTypeMirror).initializer
// No parameters
if (m1 is PsiClassInitializer && m2 is PsiClassInitializer) return true
if (m1 !is PsiMethod || m2 !is PsiMethod) return false if (m1 !is PsiMethod || m2 !is PsiMethod) return false
if (m1.parameterList.parametersCount != m2.parameterList.parametersCount) return false if (m1.parameterList.parametersCount != m2.parameterList.parametersCount) return false
for (i in 0..(m1.parameterList.parametersCount - 1)) { for (i in 0..(m1.parameterList.parametersCount - 1)) {
val p1 = m1.parameterList.parameters[i] val p1 = m1.parameterList.parameters[i].type
val p2 = m2.parameterList.parameters[i] val p2 = m2.parameterList.parameters[i].type
if (!TypeConversionUtil.isAssignable(p2.type, p1.type, false)) { if (p1 != p2 && TypeConversionUtil.erasure(p1) != TypeConversionUtil.erasure(p2)) return false
return false
}
} }
return true return true
@@ -218,4 +250,17 @@ class KotlinTypes(val javaPsiFacade: JavaPsiFacade, val scope: GlobalSearchScope
override fun capture(t: TypeMirror): TypeMirror? { override fun capture(t: TypeMirror): TypeMirror? {
TODO() TODO()
} }
}
private fun illegalArg(text: String? = null): Nothing {
val message = if (text != null) ": $text" else ""
throw IllegalArgumentException("Illegal argument" + message)
}
private fun assertKindNot(typeMirror: TypeMirror, vararg kinds: TypeKind): Unit {
if (typeMirror.kind in kinds) illegalArg("type must not be kind of " + typeMirror.kind.name + ", got ${typeMirror.kind}")
}
private fun assertJeType(type: TypeMirror) {
illegalArg("Must be a subclass of JePsiType, got ${type.javaClass.canonicalName}")
} }
@@ -51,7 +51,7 @@ private class JeEnumValueAnnotationValue(val psi: PsiEnumConstant) : AnnotationV
} }
private class JeTypeAnnotationValue(val psi: PsiClassObjectAccessExpression) : AnnotationValue { private class JeTypeAnnotationValue(val psi: PsiClassObjectAccessExpression) : AnnotationValue {
override fun getValue() = psi.operand.type.toJeType() override fun getValue() = psi.operand.type.toJeType(psi.manager)
override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitType(value, p) override fun <R : Any?, P : Any?> accept(v: AnnotationValueVisitor<R, P>, p: P) = v.visitType(value, p)
} }
@@ -36,7 +36,7 @@ class JeMethodExecutableElement(override val psi: PsiMethod) : JeElement(), Exec
return JeName(psi.name) return JeName(psi.name)
} }
override fun getThrownTypes() = psi.throwsList.referencedTypes.map { it.toJeType() } override fun getThrownTypes() = psi.throwsList.referencedTypes.map { it.toJeType(psi.manager) }
override fun getTypeParameters() = psi.typeParameters.map { JeTypeParameterElement(it, this) } override fun getTypeParameters() = psi.typeParameters.map { JeTypeParameterElement(it, this) }
@@ -48,7 +48,7 @@ class JeMethodExecutableElement(override val psi: PsiMethod) : JeElement(), Exec
return JeAnnotationValue(defaultValue) return JeAnnotationValue(defaultValue)
} }
override fun getReturnType() = psi.returnType?.let { it.toJeType() } ?: JeNoneType override fun getReturnType() = psi.returnType?.let { it.toJeType(psi.manager) } ?: JeNoneType
override fun getReceiverType() = psi.getReceiverTypeMirror() override fun getReceiverType() = psi.getReceiverTypeMirror()
@@ -89,7 +89,7 @@ fun PsiMethod.getReceiverTypeMirror(): TypeMirror {
val containingClass = containingClass val containingClass = containingClass
if (containingClass != null && !containingClass.hasModifierProperty(PsiModifier.STATIC)) { if (containingClass != null && !containingClass.hasModifierProperty(PsiModifier.STATIC)) {
containingClass.containingClass?.let { containingClass.containingClass?.let {
return PsiTypesUtil.getClassType(it).toJeType() return PsiTypesUtil.getClassType(it).toJeType(manager)
} }
} }
@@ -97,6 +97,6 @@ fun PsiMethod.getReceiverTypeMirror(): TypeMirror {
} }
val containingClass = containingClass ?: return JeNoneType val containingClass = containingClass ?: return JeNoneType
return PsiTypesUtil.getClassType(containingClass).toJeType() return PsiTypesUtil.getClassType(containingClass).toJeType(manager)
} }
@@ -41,10 +41,10 @@ class JeTypeElement(override val psi: PsiClass) : JeElement(), TypeElement, JeAn
override fun getSuperclass(): TypeMirror { override fun getSuperclass(): TypeMirror {
val superClass = psi.superClass ?: return JeNoneType val superClass = psi.superClass ?: return JeNoneType
return PsiTypesUtil.getClassType(superClass).toJeType() return PsiTypesUtil.getClassType(superClass).toJeType(psi.manager)
} }
override fun getInterfaces() = psi.interfaces.map { PsiTypesUtil.getClassType(it).toJeType() } override fun getInterfaces() = psi.interfaces.map { PsiTypesUtil.getClassType(it).toJeType(psi.manager) }
override fun getTypeParameters() = psi.typeParameters.map { JeTypeParameterElement(it, this) } override fun getTypeParameters() = psi.typeParameters.map { JeTypeParameterElement(it, this) }
@@ -79,7 +79,7 @@ class JeTypeElement(override val psi: PsiClass) : JeElement(), TypeElement, JeAn
else -> ElementKind.CLASS else -> ElementKind.CLASS
} }
override fun asType() = PsiTypesUtil.getClassType(psi).toJeType() override fun asType() = PsiTypesUtil.getClassType(psi).toJeType(psi.manager)
override fun <R : Any?, P : Any?> accept(v: ElementVisitor<R, P>, p: P) = v.visitType(this, p) override fun <R : Any?, P : Any?> accept(v: ElementVisitor<R, P>, p: P) = v.visitType(this, p)
@@ -39,13 +39,13 @@ class JeTypeParameterElement(
override fun getKind() = ElementKind.TYPE_PARAMETER override fun getKind() = ElementKind.TYPE_PARAMETER
override fun asType() = PsiTypesUtil.getClassType(psi).toJeType() override fun asType() = PsiTypesUtil.getClassType(psi).toJeType(psi.manager)
override fun <R : Any?, P : Any?> accept(v: ElementVisitor<R, P>, p: P) = v.visitTypeParameter(this, p) override fun <R : Any?, P : Any?> accept(v: ElementVisitor<R, P>, p: P) = v.visitTypeParameter(this, p)
override fun getEnclosedElements() = emptyList<Element>() override fun getEnclosedElements() = emptyList<Element>()
override fun getBounds() = psi.superTypes.map { it.toJeType() } override fun getBounds() = psi.superTypes.map { it.toJeType(psi.manager) }
override fun getGenericElement() = parent override fun getGenericElement() = parent
@@ -45,7 +45,7 @@ class JeVariableElement(override val psi: PsiVariable) : JeElement(), VariableEl
else -> ElementKind.LOCAL_VARIABLE else -> ElementKind.LOCAL_VARIABLE
} }
override fun asType() = psi.type.toJeType() override fun asType() = psi.type.toJeType(psi.manager)
override fun <R : Any?, P : Any?> accept(v: ElementVisitor<R, P>, p: P) = v.visitVariable(this, p) override fun <R : Any?, P : Any?> accept(v: ElementVisitor<R, P>, p: P) = v.visitVariable(this, p)
@@ -17,14 +17,15 @@
package org.jetbrains.kotlin.java.model.types package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiArrayType import com.intellij.psi.PsiArrayType
import com.intellij.psi.PsiManager
import javax.lang.model.type.ArrayType import javax.lang.model.type.ArrayType
import javax.lang.model.type.TypeKind import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
class JeArrayType(override val psiType: PsiArrayType) : JeAbstractType(), ArrayType { class JeArrayType(override val psiType: PsiArrayType, override val psiManager: PsiManager) : JePsiType(), JeTypeWithManager, ArrayType {
override fun getKind() = TypeKind.ARRAY override fun getKind() = TypeKind.ARRAY
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitArray(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitArray(this, p)
override fun getComponentType() = psiType.componentType.toJeType() override fun getComponentType() = psiType.componentType.toJeType(psiManager)
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
@@ -17,14 +17,18 @@
package org.jetbrains.kotlin.java.model.types package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiClassInitializer import com.intellij.psi.PsiClassInitializer
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiModifier import com.intellij.psi.PsiModifier
import javax.lang.model.type.* import javax.lang.model.type.*
class JeClassInitializerExecutableTypeMirror(val initializer: PsiClassInitializer) : JeTypeBase(), ExecutableType { class JeClassInitializerExecutableTypeMirror(val initializer: PsiClassInitializer) : JeTypeMirror, JeTypeWithManager, ExecutableType {
override fun getKind() = TypeKind.EXECUTABLE override fun getKind() = TypeKind.EXECUTABLE
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitExecutable(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitExecutable(this, p)
override val psiManager: PsiManager
get() = initializer.manager
override fun getReturnType() = CustomJeNoneType(TypeKind.VOID) override fun getReturnType() = CustomJeNoneType(TypeKind.VOID)
override fun getReceiverType() = JeNoneType override fun getReceiverType() = JeNoneType
@@ -1,64 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiType
import javax.lang.model.element.TypeElement
import javax.lang.model.type.DeclaredType
import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVisitor
class JeCompoundDeclaredType(
val psiClass: PsiClass,
val baseType: TypeElement,
val enclosingTypeMirror: TypeMirror?,
val typeArgs: List<PsiType>,
val typeArgMirrors: List<TypeMirror>
) : JeTypeBase(), DeclaredType {
override fun getKind() = TypeKind.DECLARED
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitDeclared(this, p)
override fun getTypeArguments() = typeArgMirrors
override fun asElement() = baseType
override fun getEnclosingType() = enclosingTypeMirror ?: JeNoneType
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as JeCompoundDeclaredType
if (baseType != other.baseType) return false
if (enclosingTypeMirror != other.enclosingTypeMirror) return false
if (typeArgs != other.typeArgs) return false
return true
}
override fun hashCode(): Int{
var result = baseType.hashCode()
result = 31 * result + (enclosingTypeMirror?.hashCode() ?: 0)
result = 31 * result + typeArgs.hashCode()
return result
}
override fun toString(): String {
return baseType.toString() + typeArgs.joinToString(prefix = "<", postfix = ">")
}
}
@@ -16,8 +16,13 @@
package org.jetbrains.kotlin.java.model.types package org.jetbrains.kotlin.java.model.types
import com.intellij.pom.java.LanguageLevel
import com.intellij.psi.PsiClass import com.intellij.psi.PsiClass
import com.intellij.psi.PsiClassType import com.intellij.psi.PsiClassType
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiType
import com.intellij.psi.impl.light.LightClassReferenceExpression
import com.intellij.psi.impl.source.PsiClassReferenceType
import com.intellij.psi.util.PsiTypesUtil import com.intellij.psi.util.PsiTypesUtil
import org.jetbrains.kotlin.java.model.elements.JeTypeElement import org.jetbrains.kotlin.java.model.elements.JeTypeElement
import javax.lang.model.type.DeclaredType import javax.lang.model.type.DeclaredType
@@ -25,17 +30,34 @@ import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeMirror import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
class JeDeclaredType(override val psiType: PsiClassType, val psiClass: PsiClass) : JeAbstractType(), DeclaredType { fun createDeclaredType(psiClass: PsiClass, typeArgs: List<PsiType>): PsiClassReferenceType? {
val params = typeArgs.toTypedArray()
val text = (psiClass.name ?: return null) + typeArgs.joinToString(prefix = "<", postfix = ">")
return PsiClassReferenceType(object : LightClassReferenceExpression(psiClass.manager, text, psiClass) {
override fun getTypeParameters() = params
}, LanguageLevel.JDK_1_8)
}
class JeDeclaredType(
override val psiType: PsiClassType,
val psiClass: PsiClass,
val enclosingDeclaredType: DeclaredType? = null,
val typeArgumentMirrors: List<TypeMirror>? = null
) : JePsiType(), JeTypeWithManager, DeclaredType {
override fun getKind() = TypeKind.DECLARED override fun getKind() = TypeKind.DECLARED
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitDeclared(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitDeclared(this, p)
override fun getTypeArguments() = psiType.parameters.map { it.toJeType() } override val psiManager: PsiManager
get() = psiClass.manager
override fun getTypeArguments() = typeArgumentMirrors ?: psiType.parameters.map { it.toJeType(psiManager) }
override fun asElement() = JeTypeElement(psiClass) override fun asElement() = JeTypeElement(psiClass)
override fun getEnclosingType(): TypeMirror { override fun getEnclosingType(): TypeMirror {
if (enclosingDeclaredType != null) return enclosingDeclaredType
val psiClass = psiClass.containingClass ?: return JeNoneType val psiClass = psiClass.containingClass ?: return JeNoneType
return PsiTypesUtil.getClassType(psiClass).toJeType() return PsiTypesUtil.getClassType(psiClass).toJeType(psiManager)
} }
override fun equals(other: Any?): Boolean{ override fun equals(other: Any?): Boolean{
@@ -17,12 +17,16 @@
package org.jetbrains.kotlin.java.model.types package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiIntersectionType import com.intellij.psi.PsiIntersectionType
import com.intellij.psi.PsiManager
import javax.lang.model.type.IntersectionType import javax.lang.model.type.IntersectionType
import javax.lang.model.type.TypeKind import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
class JeIntersectionType(override val psiType: PsiIntersectionType) : JeAbstractType(), IntersectionType { class JeIntersectionType(
override val psiType: PsiIntersectionType,
override val psiManager: PsiManager
) : JePsiType(), JeTypeWithManager, IntersectionType {
override fun getKind() = TypeKind.INTERSECTION override fun getKind() = TypeKind.INTERSECTION
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitIntersection(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitIntersection(this, p)
override fun getBounds() = psiType.superTypes.map { it.toJeType() } override fun getBounds() = psiType.superTypes.map { it.toJeType(psiManager) }
} }
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.java.model.types package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiMethod import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiType import com.intellij.psi.PsiType
import com.intellij.psi.util.MethodSignature import com.intellij.psi.util.MethodSignature
@@ -27,37 +28,40 @@ import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
class JeMethodExecutableTypeMirror( class JeMethodExecutableTypeMirror(
val method: PsiMethod, val psi: PsiMethod,
val signature: MethodSignature? = null, val signature: MethodSignature? = null,
val returnType: PsiType? = null val returnType: PsiType? = null
) : JeTypeBase(), ExecutableType { ) : JeTypeMirror, JeTypeWithManager, ExecutableType {
override fun getKind() = TypeKind.EXECUTABLE override fun getKind() = TypeKind.EXECUTABLE
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitExecutable(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitExecutable(this, p)
override fun getReturnType() = (returnType ?: method.returnType)?.let { it.toJeType() } ?: CustomJeNoneType(TypeKind.VOID) override fun getReturnType() = (returnType ?: psi.returnType)?.let { it.toJeType(psi.manager) } ?: CustomJeNoneType(TypeKind.VOID)
override fun getReceiverType() = method.getReceiverTypeMirror() override fun getReceiverType() = psi.getReceiverTypeMirror()
override fun getThrownTypes() = method.throwsList.referencedTypes.map { it.toJeType() } override fun getThrownTypes() = psi.throwsList.referencedTypes.map { it.toJeType(psi.manager) }
override val psiManager: PsiManager
get() = psi.manager
override fun getParameterTypes(): List<TypeMirror> { override fun getParameterTypes(): List<TypeMirror> {
signature?.parameterTypes?.let { types -> return types.map { it.toJeType() } } signature?.parameterTypes?.let { types -> return types.map { it.toJeType(psi.manager) } }
return method.parameterList.parameters.map { it.type.toJeType() } return psi.parameterList.parameters.map { it.type.toJeType(psi.manager) }
} }
override fun getTypeVariables(): List<JeTypeVariableType> { override fun getTypeVariables(): List<JeTypeVariableType> {
val typeParameters = signature?.typeParameters ?: method.typeParameters val typeParameters = signature?.typeParameters ?: psi.typeParameters
return typeParameters.map { JeTypeVariableType(PsiTypesUtil.getClassType(it), it) } return typeParameters.map { JeTypeVariableType(PsiTypesUtil.getClassType(it), it) }
} }
override fun toString() = (method.containingClass?.qualifiedName?.let { it + "." } ?: "") + method.name override fun toString() = (psi.containingClass?.qualifiedName?.let { it + "." } ?: "") + psi.name
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other?.javaClass != javaClass) return false if (other?.javaClass != javaClass) return false
return method == (other as JeMethodExecutableTypeMirror).method return psi == (other as JeMethodExecutableTypeMirror).psi
} }
override fun hashCode() = method.hashCode() override fun hashCode() = psi.hashCode()
} }
@@ -16,14 +16,12 @@
package org.jetbrains.kotlin.java.model.types package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiType
import javax.lang.model.type.NullType import javax.lang.model.type.NullType
import javax.lang.model.type.TypeKind import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
object JeNullType : JeAbstractType(), NullType { object JeNullType : JeTypeMirror, NullType {
override fun getKind() = TypeKind.NULL override fun getKind() = TypeKind.NULL
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNull(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNull(this, p)
override val psiType = PsiType.NULL
override fun toString() = "null" override fun toString() = "null"
} }
@@ -22,7 +22,7 @@ import javax.lang.model.type.PrimitiveType
import javax.lang.model.type.TypeKind import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
class JePrimitiveType(override val psiType: PsiPrimitiveType) : JeAbstractType(), PrimitiveType { class JePrimitiveType(override val psiType: PsiPrimitiveType) : JePsiType(), PrimitiveType {
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitPrimitive(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitPrimitive(this, p)
override fun getKind() = when (psiType) { override fun getKind() = when (psiType) {
@@ -18,14 +18,14 @@ package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiType import com.intellij.psi.PsiType
abstract class JeAbstractType : JeTypeBase() { abstract class JePsiType : JeTypeMirror {
abstract val psiType: PsiType abstract val psiType: PsiType
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other?.javaClass != javaClass) return false if (other?.javaClass != javaClass) return false
other as JeAbstractType other as JePsiType
if (kind != other.kind) return false if (kind != other.kind) return false
if (psiType != other.psiType) return false if (psiType != other.psiType) return false
@@ -20,7 +20,7 @@ import javax.lang.model.element.AnnotationMirror
import javax.lang.model.type.TypeMirror import javax.lang.model.type.TypeMirror
//TODO support type annotations //TODO support type annotations
abstract class JeTypeBase : TypeMirror { interface JeTypeMirror : TypeMirror {
override fun getAnnotationMirrors() = emptyList<AnnotationMirror>() override fun getAnnotationMirrors() = emptyList<AnnotationMirror>()
override fun <A : Annotation> getAnnotation(annotationClass: Class<A>?) = null override fun <A : Annotation> getAnnotation(annotationClass: Class<A>?) = null
@@ -24,10 +24,16 @@ import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVariable import javax.lang.model.type.TypeVariable
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
class JeTypeVariableType(override val psiType: PsiClassType, val parameter: PsiTypeParameter) : JeAbstractType(), TypeVariable { class JeTypeVariableType(
override val psiType: PsiClassType,
val parameter: PsiTypeParameter
) : JePsiType(), JeTypeWithManager, TypeVariable {
override fun getKind() = TypeKind.TYPEVAR override fun getKind() = TypeKind.TYPEVAR
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitTypeVariable(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitTypeVariable(this, p)
override val psiManager: PsiManager
get() = parameter.manager
override fun getLowerBound(): TypeMirror? { override fun getLowerBound(): TypeMirror? {
//TODO support captured lower bounds //TODO support captured lower bounds
return JeNullType return JeNullType
@@ -14,6 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.annotation.processing package org.jetbrains.kotlin.java.model.types
class AnnotationProcessingConfigurationService(val aptClassLoader: ClassLoader) import com.intellij.psi.PsiManager
interface JeTypeWithManager : JeTypeMirror {
val psiManager: PsiManager
}
@@ -16,18 +16,23 @@
package org.jetbrains.kotlin.java.model.types package org.jetbrains.kotlin.java.model.types
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiWildcardType import com.intellij.psi.PsiWildcardType
import javax.lang.model.type.TypeKind import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVisitor import javax.lang.model.type.TypeVisitor
import javax.lang.model.type.WildcardType import javax.lang.model.type.WildcardType
class JeWildcardType(override val psiType: PsiWildcardType) : JeAbstractType(), WildcardType { class JeWildcardType(override val psiType: PsiWildcardType) : JePsiType(), JeTypeWithManager, WildcardType {
override fun getKind() = TypeKind.WILDCARD override fun getKind() = TypeKind.WILDCARD
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitWildcard(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitWildcard(this, p)
override fun getSuperBound() = psiType.superBound.toJeType()
override fun getExtendsBound() = psiType.extendsBound.toJeType() override fun getSuperBound() = psiType.superBound.toJeType(psiManager)
override fun equals(other: Any?): Boolean{ override fun getExtendsBound() = psiType.extendsBound.toJeType(psiManager)
override val psiManager: PsiManager
get() = psiType.manager
override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other?.javaClass != javaClass) return false if (other?.javaClass != javaClass) return false
if (!super.equals(other)) return false if (!super.equals(other)) return false
@@ -40,11 +45,4 @@ class JeWildcardType(override val psiType: PsiWildcardType) : JeAbstractType(),
result = 31 * result + psiType.hashCode() result = 31 * result + psiType.hashCode()
return result return result
} }
}
class JeWildcardTypeWithBounds(private val extendsType: TypeMirror, private val superType: TypeMirror) : JeTypeBase(), WildcardType {
override fun getKind() = TypeKind.WILDCARD
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitWildcard(this, p)
override fun getSuperBound() = superType
override fun getExtendsBound() = extendsType
} }
@@ -18,32 +18,34 @@ package org.jetbrains.kotlin.java.model.types
import javax.lang.model.type.* import javax.lang.model.type.*
object JePackageTypeMirror : JeTypeBase(), NoType { interface JeNoType : JeTypeMirror, NoType
object JePackageTypeMirror : JeNoType {
override fun getKind() = TypeKind.PACKAGE override fun getKind() = TypeKind.PACKAGE
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p)
} }
object JeNoneType : JeTypeBase(), NoType { object JeNoneType : JeNoType {
override fun getKind() = TypeKind.NONE override fun getKind() = TypeKind.NONE
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p)
} }
object JeVoidType : JeTypeBase(), NoType { object JeVoidType : JeNoType {
override fun getKind() = TypeKind.VOID override fun getKind() = TypeKind.VOID
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p)
} }
class CustomJeNoneType(private val _kind: TypeKind) : JeTypeBase(), NoType { class CustomJeNoneType(private val _kind: TypeKind) : JeNoType {
override fun getKind() = _kind override fun getKind() = _kind
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p)
} }
object JeErrorType : JeTypeBase(), NoType { object JeErrorType : JeNoType {
override fun getKind() = TypeKind.ERROR override fun getKind() = TypeKind.ERROR
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p)
} }
object JeDeclaredErrorType : JeTypeBase(), DeclaredType, NoType { object JeDeclaredErrorType : JeNoType, DeclaredType {
override fun getKind() = TypeKind.ERROR override fun getKind() = TypeKind.ERROR
override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p) override fun <R : Any?, P : Any?> accept(v: TypeVisitor<R, P>, p: P) = v.visitNoType(this, p)
@@ -41,11 +41,11 @@ fun TypeKind?.toJePrimitiveType() = PSI_PRIMITIVES_MAP[TYPE_KIND_TO_PSI_PRIMITIV
fun PsiType.toJePrimitiveType() = PSI_PRIMITIVES_MAP[this] fun PsiType.toJePrimitiveType() = PSI_PRIMITIVES_MAP[this]
fun PsiType.toJeType(): TypeMirror = when (this) { fun PsiType.toJeType(manager: PsiManager): TypeMirror = when (this) {
PsiType.VOID -> JeVoidType PsiType.VOID -> JeVoidType
PsiType.NULL -> JeNullType PsiType.NULL -> JeNullType
is PsiPrimitiveType -> PSI_PRIMITIVES_MAP[this] ?: JeErrorType is PsiPrimitiveType -> PSI_PRIMITIVES_MAP[this] ?: JeErrorType
is PsiArrayType -> JeArrayType(this) is PsiArrayType -> JeArrayType(this, manager)
is PsiWildcardType -> JeWildcardType(this) is PsiWildcardType -> JeWildcardType(this)
is PsiClassType -> { is PsiClassType -> {
val resolvedClass = this.resolve() val resolvedClass = this.resolve()
@@ -55,7 +55,7 @@ fun PsiType.toJeType(): TypeMirror = when (this) {
else -> JeErrorType else -> JeErrorType
} }
} }
is PsiIntersectionType -> JeIntersectionType(this) is PsiIntersectionType -> JeIntersectionType(this, manager)
else -> JeErrorType else -> JeErrorType
} }