Compiler Daemon: Use clearHandlersCache() method instead of Reflection API

(old code may fail in IDEA Ultimate due to field obfuscation)
This commit is contained in:
Alexey Sedunov
2016-03-25 13:56:19 +03:00
parent 2d0b06dc2d
commit c4eb8ceb53
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.daemon
import com.intellij.openapi.vfs.impl.ZipHandler
import com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem
import org.jetbrains.kotlin.cli.common.CLICompiler
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY
@@ -39,7 +40,8 @@ import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.locks.ReentrantReadWriteLock
import java.util.logging.Level
import java.util.logging.Logger
import kotlin.comparisons.*
import kotlin.comparisons.compareBy
import kotlin.comparisons.reversed
import kotlin.concurrent.read
import kotlin.concurrent.schedule
import kotlin.concurrent.write
@@ -486,28 +488,7 @@ class CompileServiceImpl(
private fun clearJarCache() {
ZipHandler.clearFileAccessorCache()
val classloader = javaClass.classLoader
// TODO: replace the following code with direct call to CoreJarFileSystem.<clearCache> as soon as it will be available (hopefully in 15.02)
try {
KotlinCoreEnvironment.applicationEnvironment?.jarFileSystem.let { jarfs ->
val jarfsClass = classloader.loadClass("com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem")
val privateHandlersField = jarfsClass.getDeclaredField("myHandlers")
privateHandlersField.isAccessible = true
privateHandlersField.get(jarfs)?.let {
val clearMethod = privateHandlersField.type.getMethod("clear")
if (clearMethod != null) {
clearMethod.invoke(it)
log.info("successfully cleared com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.myHandlers")
}
else {
log.info("unable to access com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.myHandlers.clear")
}
} ?: log.info("unable to access CoreJarFileSystem.myHandlers (${privateHandlersField.get(jarfs)})")
}
}
catch (e: Exception) {
log.log(Level.SEVERE, "error clearing CoreJarFileSystem", e)
}
(KotlinCoreEnvironment.applicationEnvironment?.jarFileSystem as? CoreJarFileSystem)?.clearHandlersCache()
}
// copied (with edit) from gradle plugin