Don't build light classes for local classes with parse errors (KT-24323, EA-107235)

Also use checks for building light classes in `getLightClassDataHolder`.
This commit is contained in:
Nikolay Krasko
2018-05-08 17:51:53 +03:00
parent 15a704fe36
commit 062a212b61
6 changed files with 113 additions and 36 deletions
@@ -23,7 +23,10 @@ import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithJava
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.test.ConfigurationKind
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.util.KotlinFrontEndException
import org.junit.Assert
import java.io.File
abstract class AbstractCompilerLightClassTest : KotlinMultiFileTestWithJava<Void, Void>() {
@@ -34,11 +37,13 @@ abstract class AbstractCompilerLightClassTest : KotlinMultiFileTestWithJava<Void
override fun doMultiFileTest(file: File, modules: MutableMap<String, ModuleAndDependencies>, files: MutableList<Void>) {
val environment = createEnvironment(file)
val expectedFile = KotlinTestUtils.replaceExtension(file, "java")
val allowFrontendExceptions = InTextDirectivesUtils.isDirectiveDefined(file.readText(), "// ALLOW_FRONTEND_EXCEPTION")
LightClassTestCommon.testLightClass(
expectedFile,
file,
{ fqname -> findLightClass(environment, fqname) },
LightClassTestCommon::removeEmptyDefaultImpls
expectedFile,
file,
{ fqname -> findLightClass(allowFrontendExceptions, environment, fqname) },
LightClassTestCommon::removeEmptyDefaultImpls
)
}
@@ -47,17 +52,41 @@ abstract class AbstractCompilerLightClassTest : KotlinMultiFileTestWithJava<Void
override fun createTestFile(module: Void?, fileName: String, text: String, directives: Map<String, String>): Void? = null
companion object {
fun findLightClass(environment: KotlinCoreEnvironment, fqname: String): PsiClass? {
KotlinTestUtils.resolveAllKotlinFiles(environment)
fun findLightClass(allowFrontendExceptions: Boolean, environment: KotlinCoreEnvironment, fqname: String): PsiClass? {
assertException(allowFrontendExceptions, KotlinFrontEndException::class.java) {
KotlinTestUtils.resolveAllKotlinFiles(environment)
}
val lightCLassForScript = KotlinAsJavaSupport
.getInstance(environment.project)
.getScriptClasses(FqName(fqname), GlobalSearchScope.allScope(environment.project))
.firstOrNull()
.getInstance(environment.project)
.getScriptClasses(FqName(fqname), GlobalSearchScope.allScope(environment.project))
.firstOrNull()
return lightCLassForScript ?: JavaElementFinder
.getInstance(environment.project)
.findClass(fqname, GlobalSearchScope.allScope(environment.project))
.getInstance(environment.project)
.findClass(fqname, GlobalSearchScope.allScope(environment.project))
}
private fun assertException(shouldOccur: Boolean, klass: Class<out Throwable>, f: () -> Unit) {
if (!shouldOccur) {
f()
return
}
var wasThrown = false
try {
f()
} catch (e: Throwable) {
wasThrown = true
if (!shouldOccur || !klass.isAssignableFrom(e.javaClass)) {
throw e
}
} finally {
if (shouldOccur && !wasThrown) {
Assert.fail("Expected exception wasn't thrown")
}
}
}
}
}