Use findLoadedClass instead of hand-written cache in ParentLastURLClassLoader
This commit is contained in:
+1
-14
@@ -20,23 +20,10 @@ import org.gradle.BuildAdapter
|
|||||||
import org.gradle.api.logging.Logging
|
import org.gradle.api.logging.Logging
|
||||||
import org.gradle.BuildResult
|
import org.gradle.BuildResult
|
||||||
import java.lang.ref.Reference
|
import java.lang.ref.Reference
|
||||||
import java.lang.reflect.Array
|
|
||||||
import kotlin.platform.platformStatic
|
|
||||||
|
|
||||||
class FinishBuildListener(var pluginClassLoader: ParentLastURLClassLoader?) : BuildAdapter() {
|
class FinishBuildListener(var pluginClassLoader: ParentLastURLClassLoader?) : BuildAdapter() {
|
||||||
val log = Logging.getLogger(this.javaClass)
|
val log = Logging.getLogger(this.javaClass)
|
||||||
|
|
||||||
class object {
|
|
||||||
platformStatic
|
|
||||||
fun isRequestedClass(fqName: String): Boolean {
|
|
||||||
return when (fqName) {
|
|
||||||
"com.intellij.openapi.util.io.ZipFileCache",
|
|
||||||
"com.intellij.openapi.util.LowMemoryWatcher" -> true
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun buildFinished(result: BuildResult?) {
|
override fun buildFinished(result: BuildResult?) {
|
||||||
log.debug("Build finished listener")
|
log.debug("Build finished listener")
|
||||||
|
|
||||||
@@ -66,7 +53,7 @@ class FinishBuildListener(var pluginClassLoader: ParentLastURLClassLoader?) : Bu
|
|||||||
val referentField = javaClass<Reference<Any>>().getDeclaredField("referent")
|
val referentField = javaClass<Reference<Any>>().getDeclaredField("referent")
|
||||||
referentField.setAccessible(true)
|
referentField.setAccessible(true)
|
||||||
|
|
||||||
val table = tableField[threadLocalsField[thread]] as kotlin.Array<*>
|
val table = tableField[threadLocalsField[thread]] as Array<*>
|
||||||
|
|
||||||
for (entry in table) {
|
for (entry in table) {
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
+5
-16
@@ -1,13 +1,10 @@
|
|||||||
package org.jetbrains.kotlin.gradle.plugin;
|
package org.jetbrains.kotlin.gradle.plugin;
|
||||||
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A parent-last classloader that will try the child classloader first and then the parent.
|
* A parent-last classloader that will try the child classloader first and then the parent.
|
||||||
@@ -57,8 +54,6 @@ public class ParentLastURLClassLoader extends ClassLoader {
|
|||||||
* We need this because findClass is protected in URLClassLoader
|
* We need this because findClass is protected in URLClassLoader
|
||||||
*/
|
*/
|
||||||
public static class ChildURLClassLoader extends URLClassLoader {
|
public static class ChildURLClassLoader extends URLClassLoader {
|
||||||
private final Map<String, Class<?>> cache = new HashMap<String, Class<?>>();
|
|
||||||
|
|
||||||
private FindClassClassLoader realParent;
|
private FindClassClassLoader realParent;
|
||||||
|
|
||||||
public ChildURLClassLoader(URL[] urls, FindClassClassLoader realParent) {
|
public ChildURLClassLoader(URL[] urls, FindClassClassLoader realParent) {
|
||||||
@@ -71,18 +66,12 @@ public class ParentLastURLClassLoader extends ClassLoader {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Class<?> findClass(@NotNull String name) throws ClassNotFoundException {
|
public Class<?> findClass(@NotNull String name) throws ClassNotFoundException {
|
||||||
|
Class<?> loaded = findLoadedClass(name);
|
||||||
|
if (loaded != null) {
|
||||||
|
return loaded;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Replace with FinishBuildListener.isRequestedClass(name) after rewriting this class on Kotlin
|
|
||||||
if (name.equals("com.intellij.openapi.util.io.ZipFileCache") ||
|
|
||||||
name.equals("com.intellij.openapi.util.LowMemoryWatcher")) {
|
|
||||||
if (cache.containsKey(name)) return cache.get(name);
|
|
||||||
|
|
||||||
Class<?> aClass = super.findClass(name);
|
|
||||||
cache.put(name, aClass);
|
|
||||||
|
|
||||||
return aClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.findClass(name);
|
return super.findClass(name);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
// if that fails, we ask our real parent classloader to load the class (we give up)
|
// if that fails, we ask our real parent classloader to load the class (we give up)
|
||||||
|
|||||||
Reference in New Issue
Block a user