Passing class loader instead of factory to compiler.
This commit is contained in:
@@ -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 jarFiles jars to load all classes from
|
||||||
* @param classCountEstimation an estimated number of classes in a the jars
|
* @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
|
* @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
|
* @return a class loader that reads classes from memory
|
||||||
* @throws IOException on from reading the jar
|
* @throws IOException on from reading the jar
|
||||||
*/
|
*/
|
||||||
public static ClassLoader preloadClasses(
|
public static ClassLoader preloadClasses(
|
||||||
Collection<File> jarFiles, int classCountEstimation, ClassLoaderFactory parentFactory, ClassHandler handler
|
Collection<File> jarFiles, int classCountEstimation, ClassLoader parentClassLoader, ClassHandler handler
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
Map<String, ResourceData> entries = loadAllClassesFromJars(jarFiles, classCountEstimation, handler);
|
Map<String, ResourceData> entries = loadAllClassesFromJars(jarFiles, classCountEstimation, handler);
|
||||||
|
|
||||||
return createMemoryBasedClassLoader(parentFactory, entries, handler);
|
return createMemoryBasedClassLoader(parentClassLoader, entries, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ClassLoader preloadClasses(
|
public static ClassLoader preloadClasses(
|
||||||
Collection<File> jarFiles, int classCountEstimation, ClassLoaderFactory parentFactory
|
Collection<File> jarFiles, int classCountEstimation, ClassLoader parentClassLoader
|
||||||
) throws IOException {
|
) throws IOException {
|
||||||
return preloadClasses(jarFiles, classCountEstimation, parentFactory, null);
|
return preloadClasses(jarFiles, classCountEstimation, parentClassLoader, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ClassLoader createMemoryBasedClassLoader(
|
private static ClassLoader createMemoryBasedClassLoader(
|
||||||
final ClassLoaderFactory parentFactory,
|
final ClassLoader parent,
|
||||||
final Map<String, ResourceData> preloadedResources,
|
final Map<String, ResourceData> preloadedResources,
|
||||||
final ClassHandler handler
|
final ClassHandler handler
|
||||||
) {
|
) {
|
||||||
return new ClassLoader(null) {
|
return new ClassLoader(null) {
|
||||||
private final ClassLoader parent = parentFactory == null ? null : parentFactory.create(this);
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
||||||
// When compiler is invoked from JPS, we should use loaded incremental cache interface from its class loader,
|
// 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();
|
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);
|
final Handler handler = getHandler(mode, withInstrumenter);
|
||||||
ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, new ClassLoaderFactory() {
|
ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, withInstrumenter, handler);
|
||||||
@Override
|
|
||||||
public ClassLoader create(ClassLoader parameter) {
|
|
||||||
return withInstrumenter;
|
|
||||||
}
|
|
||||||
}, handler);
|
|
||||||
|
|
||||||
Class<?> mainClass = preloaded.loadClass(mainClassCanonicalName);
|
Class<?> mainClass = preloaded.loadClass(mainClassCanonicalName);
|
||||||
Method mainMethod = mainClass.getMethod("main", String[].class);
|
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.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.cli.common.messages.MessageCollector;
|
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.KotlinPaths;
|
||||||
import org.jetbrains.jet.utils.PathUtil;
|
import org.jetbrains.jet.utils.PathUtil;
|
||||||
|
|
||||||
@@ -35,7 +34,7 @@ public final class CompilerEnvironment {
|
|||||||
public static CompilerEnvironment getEnvironmentFor(
|
public static CompilerEnvironment getEnvironmentFor(
|
||||||
@NotNull KotlinPaths kotlinPaths,
|
@NotNull KotlinPaths kotlinPaths,
|
||||||
@Nullable File outputDir,
|
@Nullable File outputDir,
|
||||||
@Nullable ClassLoaderFactory parentFactory,
|
@Nullable ClassLoader parentClassLoader,
|
||||||
@NotNull Object... serviceImplementations
|
@NotNull Object... serviceImplementations
|
||||||
) {
|
) {
|
||||||
Map<Class, Object> servicesMap = new HashMap<Class, Object>();
|
Map<Class, Object> servicesMap = new HashMap<Class, Object>();
|
||||||
@@ -44,7 +43,7 @@ public final class CompilerEnvironment {
|
|||||||
servicesMap.put(serviceInterface, serviceImplementation);
|
servicesMap.put(serviceInterface, serviceImplementation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new CompilerEnvironment(kotlinPaths, outputDir, parentFactory, servicesMap);
|
return new CompilerEnvironment(kotlinPaths, outputDir, parentClassLoader, servicesMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -52,19 +51,19 @@ public final class CompilerEnvironment {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private final File output;
|
private final File output;
|
||||||
@Nullable
|
@Nullable
|
||||||
private final ClassLoaderFactory parentFactory;
|
private final ClassLoader parentClassLoader;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Map<Class, Object> services;
|
private final Map<Class, Object> services;
|
||||||
|
|
||||||
private CompilerEnvironment(
|
private CompilerEnvironment(
|
||||||
@NotNull KotlinPaths kotlinPaths,
|
@NotNull KotlinPaths kotlinPaths,
|
||||||
@Nullable File output,
|
@Nullable File output,
|
||||||
@Nullable ClassLoaderFactory parentFactory,
|
@Nullable ClassLoader parentClassLoader,
|
||||||
@NotNull Map<Class, Object> services
|
@NotNull Map<Class, Object> services
|
||||||
) {
|
) {
|
||||||
this.kotlinPaths = kotlinPaths;
|
this.kotlinPaths = kotlinPaths;
|
||||||
this.output = output;
|
this.output = output;
|
||||||
this.parentFactory = parentFactory;
|
this.parentClassLoader = parentClassLoader;
|
||||||
this.services = services;
|
this.services = services;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,8 +83,8 @@ public final class CompilerEnvironment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public ClassLoaderFactory getParentFactory() {
|
public ClassLoader getParentClassLoader() {
|
||||||
return parentFactory;
|
return parentClassLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reportErrorsTo(@NotNull MessageCollector messageCollector) {
|
public void reportErrorsTo(@NotNull MessageCollector messageCollector) {
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import com.intellij.util.Function;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.cli.common.messages.MessageCollector;
|
import org.jetbrains.jet.cli.common.messages.MessageCollector;
|
||||||
import org.jetbrains.jet.preloading.ClassLoaderFactory;
|
|
||||||
import org.jetbrains.jet.preloading.ClassPreloadingUtils;
|
import org.jetbrains.jet.preloading.ClassPreloadingUtils;
|
||||||
import org.jetbrains.jet.utils.KotlinPaths;
|
import org.jetbrains.jet.utils.KotlinPaths;
|
||||||
|
|
||||||
@@ -60,14 +59,14 @@ public class CompilerRunnerUtil {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static ClassLoader getOrCreatePreloader(
|
public static ClassLoader getOrCreatePreloader(
|
||||||
@NotNull KotlinPaths paths,
|
@NotNull KotlinPaths paths,
|
||||||
@Nullable ClassLoaderFactory parentFactory,
|
@Nullable ClassLoader parentClassLoader,
|
||||||
@NotNull MessageCollector messageCollector
|
@NotNull MessageCollector messageCollector
|
||||||
) {
|
) {
|
||||||
ClassLoader answer = ourClassLoaderRef.get();
|
ClassLoader answer = ourClassLoaderRef.get();
|
||||||
if (answer == null) {
|
if (answer == null) {
|
||||||
try {
|
try {
|
||||||
int estimatedClassNumber = 4096;
|
int estimatedClassNumber = 4096;
|
||||||
answer = ClassPreloadingUtils.preloadClasses(kompilerClasspath(paths, messageCollector), estimatedClassNumber, parentFactory);
|
answer = ClassPreloadingUtils.preloadClasses(kompilerClasspath(paths, messageCollector), estimatedClassNumber, parentClassLoader);
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@@ -120,7 +119,7 @@ public class CompilerRunnerUtil {
|
|||||||
MessageCollector messageCollector, PrintStream out, boolean usePreloader
|
MessageCollector messageCollector, PrintStream out, boolean usePreloader
|
||||||
) throws Exception {
|
) throws Exception {
|
||||||
ClassLoader loader = usePreloader
|
ClassLoader loader = usePreloader
|
||||||
? getOrCreatePreloader(environment.getKotlinPaths(), environment.getParentFactory(), messageCollector)
|
? getOrCreatePreloader(environment.getKotlinPaths(), environment.getParentClassLoader(), messageCollector)
|
||||||
: getOrCreateClassLoader(environment.getKotlinPaths(), messageCollector);
|
: getOrCreateClassLoader(environment.getKotlinPaths(), messageCollector);
|
||||||
|
|
||||||
Class<?> kompiler = Class.forName(compilerClassName, true, loader);
|
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.google.common.collect.Maps;
|
||||||
import com.intellij.openapi.util.Key;
|
import com.intellij.openapi.util.Key;
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil;
|
||||||
import com.intellij.openapi.util.io.StreamUtil;
|
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
import com.intellij.util.Function;
|
import com.intellij.util.Function;
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
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.config.IncrementalCompilation;
|
||||||
import org.jetbrains.jet.jps.JpsKotlinCompilerSettings;
|
import org.jetbrains.jet.jps.JpsKotlinCompilerSettings;
|
||||||
import org.jetbrains.jet.jps.incremental.*;
|
import org.jetbrains.jet.jps.incremental.*;
|
||||||
import org.jetbrains.jet.preloading.ClassLoaderFactory;
|
|
||||||
import org.jetbrains.jet.utils.PathUtil;
|
import org.jetbrains.jet.utils.PathUtil;
|
||||||
import org.jetbrains.jps.ModuleChunk;
|
import org.jetbrains.jps.ModuleChunk;
|
||||||
import org.jetbrains.jps.builders.DirtyFilesHolder;
|
import org.jetbrains.jps.builders.DirtyFilesHolder;
|
||||||
@@ -57,10 +55,8 @@ import org.jetbrains.jps.model.module.JpsModule;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileFilter;
|
import java.io.FileFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.net.URL;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
|
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);
|
IncrementalCacheProviderImpl cacheProvider = new IncrementalCacheProviderImpl(incrementalCaches);
|
||||||
|
|
||||||
CompilerEnvironment environment = CompilerEnvironment.getEnvironmentFor(
|
CompilerEnvironment environment = CompilerEnvironment.getEnvironmentFor(
|
||||||
PathUtil.getKotlinPathsForJpsPluginOrJpsTests(), outputDir, new ClassLoaderFactory() {
|
PathUtil.getKotlinPathsForJpsPluginOrJpsTests(), outputDir, getClass().getClassLoader(), cacheProvider);
|
||||||
@Override
|
|
||||||
public ClassLoader create(ClassLoader compilerClassLoader) {
|
|
||||||
return new MyClassLoader(compilerClassLoader);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
cacheProvider
|
|
||||||
);
|
|
||||||
if (!environment.success()) {
|
if (!environment.success()) {
|
||||||
environment.reportErrorsTo(messageCollector);
|
environment.reportErrorsTo(messageCollector);
|
||||||
return ExitCode.ABORT;
|
return ExitCode.ABORT;
|
||||||
@@ -407,24 +396,4 @@ public class KotlinBuilder extends ModuleLevelBuilder {
|
|||||||
public List<String> getCompilableFileExtensions() {
|
public List<String> getCompilableFileExtensions() {
|
||||||
return COMPILABLE_FILE_EXTENSIONS;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user