Passing class loader instead of factory to compiler.

This commit is contained in:
Evgeny Gerashchenko
2014-08-12 21:24:34 +04:00
parent e419c1a604
commit 3233317316
6 changed files with 19 additions and 80 deletions
@@ -1,21 +0,0 @@
/*
* 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,33 +48,31 @@ public class ClassPreloadingUtils {
*
* @param jarFiles jars to load all classes from
* @param classCountEstimation an estimated number of classes in a the jars
* @param parentFactory (nullable) factory for creating parent class loader, passing preloader's as parameter
* @param parentClassLoader parent class loader
* @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, ClassLoaderFactory parentFactory, ClassHandler handler
Collection<File> jarFiles, int classCountEstimation, ClassLoader parentClassLoader, ClassHandler handler
) throws IOException {
Map<String, ResourceData> entries = loadAllClassesFromJars(jarFiles, classCountEstimation, handler);
return createMemoryBasedClassLoader(parentFactory, entries, handler);
return createMemoryBasedClassLoader(parentClassLoader, entries, handler);
}
public static ClassLoader preloadClasses(
Collection<File> jarFiles, int classCountEstimation, ClassLoaderFactory parentFactory
Collection<File> jarFiles, int classCountEstimation, ClassLoader parentClassLoader
) throws IOException {
return preloadClasses(jarFiles, classCountEstimation, parentFactory, null);
return preloadClasses(jarFiles, classCountEstimation, parentClassLoader, null);
}
private static ClassLoader createMemoryBasedClassLoader(
final ClassLoaderFactory parentFactory,
final ClassLoader parent,
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 {
// When compiler is invoked from JPS, we should use loaded incremental cache interface from its class loader,
@@ -57,15 +57,10 @@ public class Preloader {
ClassLoader parent = Preloader.class.getClassLoader();
final ClassLoader withInstrumenter = instrumentersClasspath.length > 0 ? new URLClassLoader(instrumentersClasspath, parent) : parent;
ClassLoader withInstrumenter = instrumentersClasspath.length > 0 ? new URLClassLoader(instrumentersClasspath, parent) : parent;
final Handler handler = getHandler(mode, withInstrumenter);
ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, new ClassLoaderFactory() {
@Override
public ClassLoader create(ClassLoader parameter) {
return withInstrumenter;
}
}, handler);
ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, withInstrumenter, handler);
Class<?> mainClass = preloaded.loadClass(mainClassCanonicalName);
Method mainMethod = mainClass.getMethod("main", String[].class);