Enable debug log for PerModulePackageService only in J2K

This commit is contained in:
Simon Ogorodnik
2019-02-15 16:19:02 +03:00
parent 9a99f832e5
commit 2b9cf06c3f
2 changed files with 24 additions and 11 deletions
@@ -18,6 +18,7 @@ import com.intellij.openapi.project.rootManager
import com.intellij.openapi.roots.ModuleRootEvent
import com.intellij.openapi.roots.ModuleRootListener
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
@@ -40,10 +41,12 @@ import org.jetbrains.kotlin.idea.util.sourceRoot
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.NotNullableUserDataProperty
import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
import org.jetbrains.kotlin.idea.caches.PerModulePackageCacheService.Companion.DEBUG_LOG_ENABLE_PerModulePackageCache
class KotlinPackageContentModificationListener(private val project: Project) {
init {
@@ -85,7 +88,7 @@ class KotlinPackageContentModificationListener(private val project: Project) {
class KotlinPackageStatementPsiTreeChangePreprocessor(private val project: Project) : PsiTreeChangePreprocessor {
override fun treeChanged(event: PsiTreeChangeEventImpl) {
val eFile = event.file ?: event.child as? PsiFile
if (eFile == null) LOG.debugInTests("Got PsiEvent: $event without file", true)
if (eFile == null) LOG.debugIfEnabled(project, "Got PsiEvent: $event without file", true)
val file = eFile as? KtFile ?: return
when (event.code) {
@@ -94,7 +97,7 @@ class KotlinPackageStatementPsiTreeChangePreprocessor(private val project: Proje
PsiTreeChangeEventImpl.PsiEventType.CHILD_REPLACED,
PsiTreeChangeEventImpl.PsiEventType.CHILD_REMOVED -> {
val child = event.child ?: run {
LOG.debugInTests("Got PsiEvent: $event without child", true)
LOG.debugIfEnabled(project, "Got PsiEvent: $event without child", true)
return
}
if (child.getParentOfType<KtPackageDirective>(false) != null)
@@ -102,7 +105,7 @@ class KotlinPackageStatementPsiTreeChangePreprocessor(private val project: Proje
}
PsiTreeChangeEventImpl.PsiEventType.CHILDREN_CHANGED -> {
val parent = event.parent ?: run {
LOG.debugInTests("Got PsiEvent: $event without parent", true)
LOG.debugIfEnabled(project, "Got PsiEvent: $event without parent", true)
return
}
if (parent.getChildrenOfType<KtPackageDirective>().any())
@@ -243,7 +246,7 @@ class PerModulePackageCacheService(private val project: Project) {
}
private fun invalidateCacheForModuleSourceInfo(moduleSourceInfo: ModuleSourceInfo) {
LOG.debugInTests("Invalidated cache for $moduleSourceInfo", false)
LOG.debugIfEnabled(project, "Invalidated cache for $moduleSourceInfo", false)
val perSourceInfoData = cache[moduleSourceInfo.module] ?: return
val dataForSourceInfo = perSourceInfoData[moduleSourceInfo] ?: return
dataForSourceInfo.clear()
@@ -263,14 +266,14 @@ class PerModulePackageCacheService(private val project: Project) {
if (sourceRootUrls.any { url ->
vfile.containedInOrContains(url)
}) {
LOG.debugInTests("Invalidated cache for $module")
LOG.debugIfEnabled(project, "Invalidated cache for $module")
data.clear()
}
}
} else {
val infoByVirtualFile = getModuleInfoByVirtualFile(project, vfile)
if (infoByVirtualFile == null || infoByVirtualFile !is ModuleSourceInfo) {
LOG.debugInTests("Skip $vfile as it has mismatched ModuleInfo=$infoByVirtualFile")
LOG.debugIfEnabled(project, "Skip $vfile as it has mismatched ModuleInfo=$infoByVirtualFile")
}
(infoByVirtualFile as? ModuleSourceInfo)?.let {
invalidateCacheForModuleSourceInfo(it)
@@ -282,13 +285,13 @@ class PerModulePackageCacheService(private val project: Project) {
pendingKtFileChanges.processPending { file ->
if (file.virtualFile != null && file.virtualFile !in projectScope) {
LOG.debugInTests("Skip $file without vFile, or not in scope: ${file.virtualFile?.let { it !in projectScope }}")
LOG.debugIfEnabled(project, "Skip $file without vFile, or not in scope: ${file.virtualFile?.let { it !in projectScope }}")
return@processPending
}
val nullableModuleInfo = file.getNullableModuleInfo()
(nullableModuleInfo as? ModuleSourceInfo)?.let { invalidateCacheForModuleSourceInfo(it) }
if (nullableModuleInfo == null || nullableModuleInfo !is ModuleSourceInfo) {
LOG.debugInTests("Skip $file as it has mismatched ModuleInfo=$nullableModuleInfo")
LOG.debugIfEnabled(project, "Skip $file as it has mismatched ModuleInfo=$nullableModuleInfo")
}
implicitPackagePrefixCache.update(file)
}
@@ -329,7 +332,7 @@ class PerModulePackageCacheService(private val project: Project) {
return cacheForCurrentModuleInfo.getOrPut(packageFqName) {
val packageExists = PackageIndexUtil.packageExists(packageFqName, moduleInfo.contentScope(), project)
LOG.debugInTests("Computed cache value for $packageFqName in $moduleInfo is $packageExists")
LOG.debugIfEnabled(project, "Computed cache value for $packageFqName in $moduleInfo is $packageExists")
packageExists
}
}
@@ -345,12 +348,17 @@ class PerModulePackageCacheService(private val project: Project) {
fun getInstance(project: Project): PerModulePackageCacheService =
ServiceManager.getService(project, PerModulePackageCacheService::class.java)
var Project.DEBUG_LOG_ENABLE_PerModulePackageCache: Boolean
by NotNullableUserDataProperty<Project, Boolean>(Key.create("debug.PerModulePackageCache"), false)
}
}
private fun Logger.debugInTests(message: String, withCurrentTrace: Boolean = false) {
if (ApplicationManager.getApplication().isUnitTestMode) {
private fun Logger.debugIfEnabled(project: Project, message: String, withCurrentTrace: Boolean = false) {
if (ApplicationManager.getApplication().isUnitTestMode && project.DEBUG_LOG_ENABLE_PerModulePackageCache) {
if (withCurrentTrace) {
val e = Exception().apply { fillInStackTrace() }
this.debug(message, e)
@@ -26,12 +26,15 @@ import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.invalidateLibraryCache
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.idea.caches.PerModulePackageCacheService.Companion.DEBUG_LOG_ENABLE_PerModulePackageCache
import java.io.File
abstract class AbstractJavaToKotlinConverterTest : LightCodeInsightFixtureTestCase() {
override fun setUp() {
super.setUp()
project.DEBUG_LOG_ENABLE_PerModulePackageCache = true
val testName = getTestName(false)
if (testName.contains("Java8") || testName.contains("java8")) {
LanguageLevelProjectExtension.getInstance(project).languageLevel = LanguageLevel.JDK_1_8
@@ -47,6 +50,8 @@ abstract class AbstractJavaToKotlinConverterTest : LightCodeInsightFixtureTestCa
override fun tearDown() {
VfsRootAccess.disallowRootAccess(KotlinTestUtils.getHomeDirectory())
project.DEBUG_LOG_ENABLE_PerModulePackageCache = false
super.tearDown()
}