Passing condition to prefer parent class loader as a parameter to preloader.

This commit is contained in:
Evgeny Gerashchenko
2014-08-19 21:03:21 +04:00
parent f3b7ae379f
commit fe27b2264a
6 changed files with 59 additions and 15 deletions
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.preloading;
public interface ClassCondition {
boolean accept(String className);
}
@@ -50,35 +50,38 @@ public class ClassPreloadingUtils {
* @param classCountEstimation an estimated number of classes in a the jars
* @param parentClassLoader parent class loader
* @param handler handler to be notified on class definitions done by this class loader, or null
* @param classesToLoadByParent condition to load some classes via parent class loader
* @return a class loader that reads classes from memory
* @throws IOException on from reading the jar
*/
public static ClassLoader preloadClasses(
Collection<File> jarFiles, int classCountEstimation, ClassLoader parentClassLoader, ClassHandler handler
Collection<File> jarFiles,
int classCountEstimation,
ClassLoader parentClassLoader,
ClassCondition classesToLoadByParent,
ClassHandler handler
) throws IOException {
Map<String, ResourceData> entries = loadAllClassesFromJars(jarFiles, classCountEstimation, handler);
return createMemoryBasedClassLoader(parentClassLoader, entries, handler);
return createMemoryBasedClassLoader(parentClassLoader, entries, handler, classesToLoadByParent);
}
public static ClassLoader preloadClasses(
Collection<File> jarFiles, int classCountEstimation, ClassLoader parentClassLoader
Collection<File> jarFiles, int classCountEstimation, ClassLoader parentClassLoader, ClassCondition classesToLoadByParent
) throws IOException {
return preloadClasses(jarFiles, classCountEstimation, parentClassLoader, null);
return preloadClasses(jarFiles, classCountEstimation, parentClassLoader, classesToLoadByParent, null);
}
private static ClassLoader createMemoryBasedClassLoader(
final ClassLoader parent,
final Map<String, ResourceData> preloadedResources,
final ClassHandler handler
final ClassHandler handler,
final ClassCondition classesToLoadByParent
) {
return new ClassLoader(null) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
// When compiler is invoked from JPS, we should use loaded incremental cache interface from its class loader,
// because implementation is loaded from it, as well
if (name.startsWith("org.jetbrains.jet.lang.resolve.kotlin.incremental.cache.") ||
name.equals("org.jetbrains.jet.config.CompilerServices")) {
if (classesToLoadByParent != null && classesToLoadByParent.accept(name)) {
if (parent == null) {
return super.loadClass(name);
}
@@ -60,7 +60,7 @@ public class Preloader {
ClassLoader withInstrumenter = instrumentersClasspath.length > 0 ? new URLClassLoader(instrumentersClasspath, parent) : parent;
final Handler handler = getHandler(mode, withInstrumenter);
ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, withInstrumenter, handler);
ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, withInstrumenter, null, handler);
Class<?> mainClass = preloaded.loadClass(mainClassCanonicalName);
Method mainMethod = mainClass.getMethod("main", String[].class);