Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.caliper.platform.jvm; |
| 18 | |
| 19 | import com.google.common.base.Joiner; |
| 20 | import com.google.common.base.Splitter; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.net.URISyntaxException; |
| 25 | import java.net.URL; |
| 26 | import java.net.URLClassLoader; |
| 27 | import java.util.LinkedHashSet; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import javax.annotation.Nullable; |
| 31 | |
| 32 | /** |
| 33 | * Provides a class path containing all of the jars present on the local machine that are referenced |
| 34 | * by a given {@link ClassLoader}. |
| 35 | */ |
| 36 | final class EffectiveClassPath { |
| 37 | private EffectiveClassPath() {} |
| 38 | |
| 39 | private static final String PATH_SEPARATOR = System.getProperty("path.separator"); |
| 40 | private static final String JAVA_CLASS_PATH = System.getProperty("java.class.path"); |
| 41 | |
| 42 | static String getClassPathForClassLoader(ClassLoader classLoader) { |
| 43 | // Order of JAR files may have some significance. Try to preserve it. |
| 44 | LinkedHashSet<File> files = new LinkedHashSet<File>(); |
| 45 | for (String entry : Splitter.on(PATH_SEPARATOR).split(JAVA_CLASS_PATH)) { |
| 46 | files.add(new File(entry)); |
| 47 | } |
| 48 | files.addAll(getClassPathFiles(classLoader)); |
| 49 | |
| 50 | return Joiner.on(PATH_SEPARATOR).join(files); |
| 51 | } |
| 52 | |
| 53 | private static Set<File> getClassPathFiles(ClassLoader classLoader) { |
| 54 | ImmutableSet.Builder<File> files = ImmutableSet.builder(); |
| 55 | @Nullable ClassLoader parent = classLoader.getParent(); |
| 56 | if (parent != null) { |
| 57 | files.addAll(getClassPathFiles(parent)); |
| 58 | } |
| 59 | if (classLoader instanceof URLClassLoader) { |
| 60 | URLClassLoader urlClassLoader = (URLClassLoader) classLoader; |
| 61 | for (URL url : urlClassLoader.getURLs()) { |
| 62 | try { |
| 63 | files.add(new File(url.toURI())); |
| 64 | } catch (URISyntaxException e) { |
| 65 | // skip it then |
| 66 | } catch (IllegalArgumentException e) { |
| 67 | // skip it then |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return files.build(); |
| 72 | } |
| 73 | } |