use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.
the class InProcessGroovyc method obtainParentLoader.
@Nullable
private static ClassLoader obtainParentLoader(Collection<String> compilationClassPath) throws MalformedURLException {
if (!"true".equals(System.getProperty("groovyc.reuse.compiler.classes", "true"))) {
return null;
}
List<String> groovyJars = ContainerUtil.findAll(compilationClassPath, s -> {
String fileName = StringUtil.getShortName(s, '/');
return GROOVY_ALL_JAR_PATTERN.matcher(fileName).matches() || GROOVY_JAR_PATTERN.matcher(fileName).matches();
});
LOG.debug("Groovy jars: " + groovyJars);
if (groovyJars.size() != 1 || !GROOVY_ALL_JAR_PATTERN.matcher(groovyJars.get(0)).matches()) {
// avoid complications caused by caching classes from several groovy versions in classpath
return null;
}
String groovyAll = groovyJars.get(0);
Pair<String, ClassLoader> pair = SoftReference.dereference(ourParentLoaderCache);
if (pair != null && pair.first.equals(groovyAll)) {
return pair.second;
}
final ClassDependencyLoader checkWellFormed = new ClassDependencyLoader() {
@Override
protected void loadClassDependencies(Class aClass) throws ClassNotFoundException {
if (!isCompilerCoreClass(aClass.getName()) || !(aClass.getClassLoader() instanceof UrlClassLoader)) {
super.loadClassDependencies(aClass);
}
}
private boolean isCompilerCoreClass(String name) {
if (name.startsWith("groovyjarjar")) {
return true;
}
if (name.startsWith("org.codehaus.groovy.")) {
String tail = name.substring("org.codehaus.groovy.".length());
if (tail.startsWith("ast") || tail.startsWith("classgen") || tail.startsWith("tools.javac") || tail.startsWith("antlr") || tail.startsWith("vmplugin") || tail.startsWith("reflection") || tail.startsWith("control")) {
return true;
}
if (tail.startsWith("runtime") && name.contains("GroovyMethods")) {
return true;
}
}
return false;
}
};
UrlClassLoader groovyAllLoader = UrlClassLoader.build().urls(toUrls(ContainerUtil.concat(GroovyBuilder.getGroovyRtRoots(), Collections.singletonList(groovyAll)))).allowLock().useCache(ourLoaderCachePool, new UrlClassLoader.CachingCondition() {
@Override
public boolean shouldCacheData(@NotNull URL url) {
return true;
}
}).get();
ClassLoader wrapper = new URLClassLoader(new URL[0], groovyAllLoader) {
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith("groovy.grape.")) {
// grape depends on Ivy which is not included in this class loader
throw new ClassNotFoundException(name);
}
try {
return checkWellFormed.loadDependencies(super.loadClass(name, resolve));
} catch (NoClassDefFoundError e) {
// For this to happen we should throw ClassNotFoundException
throw new ClassNotFoundException(name, e);
}
}
};
ourParentLoaderCache = new SoftReference<>(Pair.create(groovyAll, wrapper));
return wrapper;
}
use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.
the class BootstrapClassLoaderUtil method addParentClasspath.
private static void addParentClasspath(Collection<URL> classpath, boolean ext) throws MalformedURLException {
String[] extDirs = System.getProperty("java.ext.dirs", "").split(File.pathSeparator);
if (ext && extDirs.length == 0)
return;
List<URLClassLoader> loaders = new ArrayList<URLClassLoader>(2);
for (ClassLoader loader = BootstrapClassLoaderUtil.class.getClassLoader(); loader != null; loader = loader.getParent()) {
if (loader instanceof URLClassLoader) {
loaders.add(0, (URLClassLoader) loader);
} else {
getLogger().warn("Unknown class loader: " + loader.getClass().getName());
}
}
// todo[r.sh] drop after migration to Java 9
for (URLClassLoader loader : loaders) {
URL[] urls = loader.getURLs();
for (URL url : urls) {
String path = urlToPath(url);
boolean isExt = false;
for (String extDir : extDirs) {
if (path.startsWith(extDir) && path.length() > extDir.length() && path.charAt(extDir.length()) == File.separatorChar) {
isExt = true;
break;
}
}
if (isExt == ext) {
classpath.add(url);
}
}
}
}
use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.
the class BootstrapClassLoaderUtil method initClassLoader.
@NotNull
public static ClassLoader initClassLoader(boolean updatePlugins) throws MalformedURLException {
PathManager.loadProperties();
Collection<URL> classpath = new LinkedHashSet<URL>();
addParentClasspath(classpath, false);
addIDEALibraries(classpath);
addAdditionalClassPath(classpath);
addParentClasspath(classpath, true);
UrlClassLoader.Builder builder = UrlClassLoader.build().urls(filterClassPath(new ArrayList<URL>(classpath))).allowLock().usePersistentClasspathIndexForLocalClassDirectories().useCache();
if (Boolean.valueOf(System.getProperty(PROPERTY_ALLOW_BOOTSTRAP_RESOURCES, "true"))) {
builder.allowBootstrapResources();
}
UrlClassLoader newClassLoader = builder.get();
// prepare plugins
if (updatePlugins && !isLoadingOfExternalPluginsDisabled()) {
try {
StartupActionScriptManager.executeActionScript();
} catch (IOException e) {
Main.showMessage("Plugin Installation Error", e);
}
}
Thread.currentThread().setContextClassLoader(newClassLoader);
return newClassLoader;
}
use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.
the class ExternalSystemApiUtil method reloadIfNecessary.
/**
* There is a possible case that methods of particular object should be executed with classpath different from the one implied
* by the current class' class loader. External system offers {@link ParametersEnhancer#enhanceLocalProcessing(List)} method
* for defining that custom classpath.
* <p/>
* It's also possible that particular implementation of {@link ParametersEnhancer} is compiled using dependency to classes
* which are provided by the {@link ParametersEnhancer#enhanceLocalProcessing(List) expanded classpath}. E.g. a class
* <code>'A'</code> might use method of class <code>'B'</code> and 'A' is located at the current (system/plugin) classpath but
* <code>'B'</code> is not. We need to reload <code>'A'</code> using its expanded classpath then, i.e. create new class loaded
* with that expanded classpath and load <code>'A'</code> by it.
* <p/>
* This method allows to do that.
*
* @param clazz custom classpath-aware class which instance should be created (is assumed to have a no-args constructor)
* @param <T> target type
* @return newly created instance of the given class loaded by custom classpath-aware loader
* @throws IllegalAccessException as defined by reflection processing
* @throws InstantiationException as defined by reflection processing
* @throws NoSuchMethodException as defined by reflection processing
* @throws InvocationTargetException as defined by reflection processing
* @throws ClassNotFoundException as defined by reflection processing
*/
@NotNull
public static <T extends ParametersEnhancer> T reloadIfNecessary(@NotNull final Class<T> clazz) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
T instance = clazz.newInstance();
List<URL> urls = ContainerUtilRt.newArrayList();
instance.enhanceLocalProcessing(urls);
if (urls.isEmpty()) {
return instance;
}
final ClassLoader baseLoader = clazz.getClassLoader();
Method method = baseLoader.getClass().getMethod("getUrls");
if (method != null) {
//noinspection unchecked
urls.addAll((Collection<? extends URL>) method.invoke(baseLoader));
}
UrlClassLoader loader = new UrlClassLoader(UrlClassLoader.build().urls(urls).parent(baseLoader.getParent())) {
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.equals(clazz.getName())) {
return super.loadClass(name, resolve);
} else {
try {
return baseLoader.loadClass(name);
} catch (ClassNotFoundException e) {
return super.loadClass(name, resolve);
}
}
}
};
//noinspection unchecked
return (T) loader.loadClass(clazz.getName()).newInstance();
}
use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.
the class JdkUtil method setCommandLineWrapperParams.
private static boolean setCommandLineWrapperParams(SimpleJavaParameters javaParameters, GeneralCommandLine commandLine, ParametersList vmParametersList, Class commandLineWrapper) throws CantRunException {
boolean dynamicVMOptions = javaParameters.isDynamicVMOptions() && useDynamicVMOptions();
boolean dynamicParameters = javaParameters.isDynamicParameters() && useDynamicParameters();
File vmParamsFile = null;
if (dynamicVMOptions) {
try {
vmParamsFile = FileUtil.createTempFile("idea_vm_params", null);
try (PrintWriter writer = new PrintWriter(vmParamsFile)) {
for (String param : vmParametersList.getList()) {
if (isUserDefinedProperty(param)) {
writer.println(param);
} else {
commandLine.addParameter(param);
}
}
}
} catch (IOException e) {
throwUnableToCreateTempFile(e);
}
} else {
commandLine.addParameters(vmParametersList.getList());
}
File appParamsFile = null;
if (dynamicParameters) {
try {
appParamsFile = FileUtil.createTempFile("idea_app_params", null);
try (PrintWriter writer = new PrintWriter(appParamsFile)) {
for (String path : javaParameters.getProgramParametersList().getList()) {
writer.println(path);
}
}
} catch (IOException e) {
throwUnableToCreateTempFile(e);
}
}
File classpathFile = null;
try {
classpathFile = FileUtil.createTempFile("idea_classpath", null);
try (PrintWriter writer = new PrintWriter(classpathFile)) {
for (String path : javaParameters.getClassPath().getPathList()) {
writer.println(path);
}
}
String classpath = PathUtil.getJarPathForClass(commandLineWrapper);
final String utilRtPath = PathUtil.getJarPathForClass(StringUtilRt.class);
if (!classpath.equals(utilRtPath)) {
classpath += File.pathSeparator + utilRtPath;
}
final Class<UrlClassLoader> ourUrlClassLoader = UrlClassLoader.class;
if (ourUrlClassLoader.getName().equals(vmParametersList.getPropertyValue("java.system.class.loader"))) {
classpath += File.pathSeparator + PathUtil.getJarPathForClass(ourUrlClassLoader);
classpath += File.pathSeparator + PathUtil.getJarPathForClass(THashMap.class);
}
commandLine.addParameter("-classpath");
commandLine.addParameter(classpath);
} catch (IOException e) {
throwUnableToCreateTempFile(e);
}
appendEncoding(javaParameters, commandLine, vmParametersList);
commandLine.addParameter(commandLineWrapper.getName());
commandLine.addParameter(classpathFile.getAbsolutePath());
if (vmParamsFile != null) {
commandLine.addParameter("@vm_params");
commandLine.addParameter(vmParamsFile.getAbsolutePath());
}
if (appParamsFile != null) {
commandLine.addParameter("@app_params");
commandLine.addParameter(appParamsFile.getAbsolutePath());
}
Set<File> filesToDelete = getFilesToDeleteUserData(commandLine);
ContainerUtil.addIfNotNull(filesToDelete, classpathFile);
ContainerUtil.addIfNotNull(filesToDelete, vmParamsFile);
ContainerUtil.addIfNotNull(filesToDelete, appParamsFile);
return dynamicParameters;
}
Aggregations