Passing custom class loader for loading incremental cache implementation.

This commit is contained in:
Evgeny Gerashchenko
2014-06-09 20:50:51 +04:00
parent 04f7ad450f
commit d3e5790674
8 changed files with 141 additions and 18 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 ClassLoaderFactory {
ClassLoader create(ClassLoader parameter);
}
@@ -48,31 +48,33 @@ public class ClassPreloadingUtils {
*
* @param jarFiles jars to load all classes from
* @param classCountEstimation an estimated number of classes in a the jars
* @param parent (nullable) parent class loader
* @param parentFactory (nullable) factory for creating parent class loader, passing preloader's as parameter
* @param handler handler to be notified on class definitions done by this class loader, or null
* @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 parent, ClassHandler handler
Collection<File> jarFiles, int classCountEstimation, ClassLoaderFactory parentFactory, ClassHandler handler
) throws IOException {
Map<String, ResourceData> entries = loadAllClassesFromJars(jarFiles, classCountEstimation, handler);
return createMemoryBasedClassLoader(parent, entries, handler);
return createMemoryBasedClassLoader(parentFactory, entries, handler);
}
public static ClassLoader preloadClasses(
Collection<File> jarFiles, int classCountEstimation, ClassLoader parent
Collection<File> jarFiles, int classCountEstimation, ClassLoaderFactory parentFactory
) throws IOException {
return preloadClasses(jarFiles, classCountEstimation, parent, null);
return preloadClasses(jarFiles, classCountEstimation, parentFactory, null);
}
private static ClassLoader createMemoryBasedClassLoader(
final ClassLoader parent,
final ClassLoaderFactory parentFactory,
final Map<String, ResourceData> preloadedResources,
final ClassHandler handler
) {
return new ClassLoader(null) {
private final ClassLoader parent = parentFactory == null ? null : parentFactory.create(this);
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
// Look in this class loader and then in the parent one
@@ -106,6 +108,15 @@ public class ClassPreloadingUtils {
return definedClass;
}
@Override
public URL getResource(String name) {
URL resource = super.getResource(name);
if (resource == null && parent != null) {
return parent.getResource(name);
}
return resource;
}
@Override
protected URL findResource(String name) {
ResourceData resourceData = preloadedResources.get(name);
@@ -113,6 +124,15 @@ public class ClassPreloadingUtils {
return resourceData.getURL();
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
Enumeration<URL> resources = super.getResources(name);
if (!resources.hasMoreElements() && parent != null) {
return parent.getResources(name);
}
return resources;
}
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
URL resource = findResource(name);
@@ -57,10 +57,15 @@ public class Preloader {
ClassLoader parent = Preloader.class.getClassLoader();
ClassLoader withInstrumenter = instrumentersClasspath.length > 0 ? new URLClassLoader(instrumentersClasspath, parent) : parent;
final 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, new ClassLoaderFactory() {
@Override
public ClassLoader create(ClassLoader parameter) {
return withInstrumenter;
}
}, handler);
Class<?> mainClass = preloaded.loadClass(mainClassCanonicalName);
Method mainMethod = mainClass.getMethod("main", String[].class);