Do not render nullability in UltraLightChecker when it's irrelevant

It's necessary because after next changes,
they become different for ultra and old light classes.
The former (ultra) is more correct but it doesn't help when
we need to compare our implementation with reference
This commit is contained in:
Denis Zharkov
2018-11-30 15:28:57 +03:00
parent d7d0407afb
commit 30c60c7b1f
2 changed files with 27 additions and 10 deletions
@@ -29,21 +29,19 @@ public final class ArrayOfAnonymous /* ArrayOfAnonymous*/ {
} }
final class C /* C*/ { final class C /* C*/ {
@null()
private final int y;
@org.jetbrains.annotations.NotNull() @org.jetbrains.annotations.NotNull()
private final kotlin.jvm.functions.Function0<java.lang.Object> initChild; private final kotlin.jvm.functions.Function0<java.lang.Object> initChild;
@null() private final int y;
public C(@null() int);
@null() @null()
public final int getY(); public C(int);
@org.jetbrains.annotations.NotNull() @org.jetbrains.annotations.NotNull()
public final kotlin.jvm.functions.Function0<java.lang.Object> getInitChild(); public final kotlin.jvm.functions.Function0<java.lang.Object> getInitChild();
public final int getY();
} }
public abstract class Super /* Super*/ { public abstract class Super /* Super*/ {
@@ -13,6 +13,9 @@ import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
import org.jetbrains.kotlin.asJava.classes.KtLightClassForSourceDeclaration import org.jetbrains.kotlin.asJava.classes.KtLightClassForSourceDeclaration
import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass
import org.jetbrains.kotlin.asJava.classes.isPrivateOrParameterInPrivateMethod
import org.jetbrains.kotlin.asJava.elements.KtLightNullabilityAnnotation
import org.jetbrains.kotlin.load.kotlin.NON_EXISTENT_CLASS_NAME
import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.junit.Assert import org.junit.Assert
@@ -58,9 +61,13 @@ object UltraLightChecker {
private fun PsiAnnotation.renderAnnotation() = private fun PsiAnnotation.renderAnnotation() =
"@" + qualifiedName + "(" + parameterList.attributes.joinToString { it.name + "=" + (it.value?.text ?: "?") } + ")" "@" + qualifiedName + "(" + parameterList.attributes.joinToString { it.name + "=" + (it.value?.text ?: "?") } + ")"
private fun PsiModifierListOwner.renderModifiers(): String { private fun PsiModifierListOwner.renderModifiers(typeIfApplicable: PsiType? = null): String {
val buffer = StringBuilder() val buffer = StringBuilder()
for (annotation in annotations) { for (annotation in annotations) {
if (annotation is KtLightNullabilityAnnotation<*> && skipRenderingNullability(typeIfApplicable)) {
continue
}
buffer.append(annotation.renderAnnotation()) buffer.append(annotation.renderAnnotation())
buffer.append(if (this is PsiParameter) " " else "\n") buffer.append(if (this is PsiParameter) " " else "\n")
} }
@@ -70,6 +77,18 @@ object UltraLightChecker {
return buffer.toString() return buffer.toString()
} }
private fun PsiModifierListOwner.skipRenderingNullability(typeIfApplicable: PsiType?) =
isPrimitiveOrNonExisting(typeIfApplicable) || isPrivateOrParameterInPrivateMethod()
private val NON_EXISTENT_QUALIFIED_CLASS_NAME = NON_EXISTENT_CLASS_NAME.replace("/", ".")
private fun isPrimitiveOrNonExisting(typeIfApplicable: PsiType?): Boolean {
if (typeIfApplicable is PsiPrimitiveType) return true
if (typeIfApplicable?.getCanonicalText(false) == NON_EXISTENT_QUALIFIED_CLASS_NAME) return true
return typeIfApplicable is PsiPrimitiveType
}
private fun PsiType.renderType() = getCanonicalText(true) private fun PsiType.renderType() = getCanonicalText(true)
private fun PsiReferenceList?.renderRefList(keyword: String): String { private fun PsiReferenceList?.renderRefList(keyword: String): String {
@@ -78,7 +97,7 @@ object UltraLightChecker {
} }
private fun PsiVariable.renderVar(): String { private fun PsiVariable.renderVar(): String {
var result = this.renderModifiers() + type.renderType() + " " + name var result = this.renderModifiers(type) + type.renderType() + " " + name
if (this is PsiParameter && this.isVarArgs) { if (this is PsiParameter && this.isVarArgs) {
result += " /* vararg */" result += " /* vararg */"
} }
@@ -97,12 +116,12 @@ object UltraLightChecker {
} + "> " } + "> "
private fun PsiMethod.renderMethod() = private fun PsiMethod.renderMethod() =
renderModifiers() + renderModifiers(returnType) +
(if (isVarArgs) "/* vararg */ " else "") + (if (isVarArgs) "/* vararg */ " else "") +
renderTypeParams() + renderTypeParams() +
(returnType?.renderType() ?: "") + " " + (returnType?.renderType() ?: "") + " " +
name + name +
"(" + parameterList.parameters.joinToString { it.renderModifiers() + it.type.renderType() } + ")" + "(" + parameterList.parameters.joinToString { it.renderModifiers(it.type) + it.type.renderType() } + ")" +
(this as? PsiAnnotationMethod)?.defaultValue?.let { " default " + it.text }.orEmpty() + (this as? PsiAnnotationMethod)?.defaultValue?.let { " default " + it.text }.orEmpty() +
throwsList.referencedTypes.let { thrownTypes -> throwsList.referencedTypes.let { thrownTypes ->
if (thrownTypes.isEmpty()) "" if (thrownTypes.isEmpty()) ""