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);
@@ -19,7 +19,6 @@ package org.jetbrains.jet.compiler.runner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.preloading.ClassLoaderFactory;
import org.jetbrains.jet.utils.KotlinPaths;
import org.jetbrains.jet.utils.PathUtil;
@@ -35,7 +34,7 @@ public final class CompilerEnvironment {
public static CompilerEnvironment getEnvironmentFor(
@NotNull KotlinPaths kotlinPaths,
@Nullable File outputDir,
@Nullable ClassLoaderFactory parentFactory,
@Nullable ClassLoader parentClassLoader,
@NotNull Object... serviceImplementations
) {
Map<Class, Object> servicesMap = new HashMap<Class, Object>();
@@ -44,7 +43,7 @@ public final class CompilerEnvironment {
servicesMap.put(serviceInterface, serviceImplementation);
}
}
return new CompilerEnvironment(kotlinPaths, outputDir, parentFactory, servicesMap);
return new CompilerEnvironment(kotlinPaths, outputDir, parentClassLoader, servicesMap);
}
@NotNull
@@ -52,19 +51,19 @@ public final class CompilerEnvironment {
@Nullable
private final File output;
@Nullable
private final ClassLoaderFactory parentFactory;
private final ClassLoader parentClassLoader;
@NotNull
private final Map<Class, Object> services;
private CompilerEnvironment(
@NotNull KotlinPaths kotlinPaths,
@Nullable File output,
@Nullable ClassLoaderFactory parentFactory,
@Nullable ClassLoader parentClassLoader,
@NotNull Map<Class, Object> services
) {
this.kotlinPaths = kotlinPaths;
this.output = output;
this.parentFactory = parentFactory;
this.parentClassLoader = parentClassLoader;
this.services = services;
}
@@ -84,8 +83,8 @@ public final class CompilerEnvironment {
}
@Nullable
public ClassLoaderFactory getParentFactory() {
return parentFactory;
public ClassLoader getParentClassLoader() {
return parentClassLoader;
}
public void reportErrorsTo(@NotNull MessageCollector messageCollector) {
@@ -20,7 +20,6 @@ import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.common.messages.MessageCollector;
import org.jetbrains.jet.preloading.ClassLoaderFactory;
import org.jetbrains.jet.preloading.ClassPreloadingUtils;
import org.jetbrains.jet.utils.KotlinPaths;
@@ -60,14 +59,14 @@ public class CompilerRunnerUtil {
@NotNull
public static ClassLoader getOrCreatePreloader(
@NotNull KotlinPaths paths,
@Nullable ClassLoaderFactory parentFactory,
@Nullable ClassLoader parentClassLoader,
@NotNull MessageCollector messageCollector
) {
ClassLoader answer = ourClassLoaderRef.get();
if (answer == null) {
try {
int estimatedClassNumber = 4096;
answer = ClassPreloadingUtils.preloadClasses(kompilerClasspath(paths, messageCollector), estimatedClassNumber, parentFactory);
answer = ClassPreloadingUtils.preloadClasses(kompilerClasspath(paths, messageCollector), estimatedClassNumber, parentClassLoader);
}
catch (IOException e) {
throw new RuntimeException(e);
@@ -120,7 +119,7 @@ public class CompilerRunnerUtil {
MessageCollector messageCollector, PrintStream out, boolean usePreloader
) throws Exception {
ClassLoader loader = usePreloader
? getOrCreatePreloader(environment.getKotlinPaths(), environment.getParentFactory(), messageCollector)
? getOrCreatePreloader(environment.getKotlinPaths(), environment.getParentClassLoader(), messageCollector)
: getOrCreateClassLoader(environment.getKotlinPaths(), messageCollector);
Class<?> kompiler = Class.forName(compilerClassName, true, loader);
@@ -19,7 +19,6 @@ package org.jetbrains.jet.jps.build;
import com.google.common.collect.Maps;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
@@ -41,7 +40,6 @@ import org.jetbrains.jet.compiler.runner.SimpleOutputItem;
import org.jetbrains.jet.config.IncrementalCompilation;
import org.jetbrains.jet.jps.JpsKotlinCompilerSettings;
import org.jetbrains.jet.jps.incremental.*;
import org.jetbrains.jet.preloading.ClassLoaderFactory;
import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.jps.ModuleChunk;
import org.jetbrains.jps.builders.DirtyFilesHolder;
@@ -57,10 +55,8 @@ import org.jetbrains.jps.model.module.JpsModule;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.*;
import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
@@ -132,14 +128,7 @@ public class KotlinBuilder extends ModuleLevelBuilder {
IncrementalCacheProviderImpl cacheProvider = new IncrementalCacheProviderImpl(incrementalCaches);
CompilerEnvironment environment = CompilerEnvironment.getEnvironmentFor(
PathUtil.getKotlinPathsForJpsPluginOrJpsTests(), outputDir, new ClassLoaderFactory() {
@Override
public ClassLoader create(ClassLoader compilerClassLoader) {
return new MyClassLoader(compilerClassLoader);
}
},
cacheProvider
);
PathUtil.getKotlinPathsForJpsPluginOrJpsTests(), outputDir, getClass().getClassLoader(), cacheProvider);
if (!environment.success()) {
environment.reportErrorsTo(messageCollector);
return ExitCode.ABORT;
@@ -407,24 +396,4 @@ public class KotlinBuilder extends ModuleLevelBuilder {
public List<String> getCompilableFileExtensions() {
return COMPILABLE_FILE_EXTENSIONS;
}
private class MyClassLoader extends ClassLoader {
private final ClassLoader compilerClassLoader;
private final ClassLoader jpsPluginClassLoader = KotlinBuilder.this.getClass().getClassLoader();
private MyClassLoader(ClassLoader compilerClassLoader) {
this.compilerClassLoader = compilerClassLoader;
}
@NotNull
@Override
public Enumeration<URL> getResources(String name) throws IOException {
return jpsPluginClassLoader.getResources(name);
}
@Override
public Class<?> loadClass(@NotNull String name) throws ClassNotFoundException {
return jpsPluginClassLoader.loadClass(name);
}
}
}