aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Yun Peng <pcloudy@google.com>2017-01-25 09:18:38 +0000
committerGravatar Laszlo Csomor <laszlocsomor@google.com>2017-01-25 10:11:30 +0000
commit29e22949664b0c2a0a9cc8d48630f4712a89a067 (patch)
tree43c3100096cb89bcba6ab7e8ea984b00ababc710
parent41105346d2d7763979fb0889c9fe7cc07709a9b0 (diff)
Classpath.java: Support reading classpath value from classpath jar
Fixed: https://github.com/bazelbuild/bazel/issues/2242 -- Change-Id: I6aa7c97487b3add30df59c4483e409bb4d4c7532 Reviewed-on: https://cr.bazel.build/8399 PiperOrigin-RevId: 145520851 MOS_MIGRATED_REVID=145520851
-rw-r--r--src/test/java/com/google/devtools/build/lib/testutil/Classpath.java48
1 files changed, 43 insertions, 5 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/testutil/Classpath.java b/src/test/java/com/google/devtools/build/lib/testutil/Classpath.java
index 4c0a7970a1..812951396d 100644
--- a/src/test/java/com/google/devtools/build/lib/testutil/Classpath.java
+++ b/src/test/java/com/google/devtools/build/lib/testutil/Classpath.java
@@ -14,13 +14,18 @@
package com.google.devtools.build.lib.testutil;
import com.google.devtools.build.lib.util.Preconditions;
-
import java.io.File;
import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -120,13 +125,46 @@ final class Classpath {
return className.substring(0, classNameEnd).replace('/', '.');
}
+ private static void getClassPathsFromClasspathJar(File classpathJar, Set<String> classPaths)
+ throws IOException {
+ Manifest manifest = new JarFile(classpathJar).getManifest();
+ Attributes attributes = manifest.getMainAttributes();
+ for (String classPath : attributes.getValue("Class-Path").split(" ")) {
+ try {
+ classPaths.add(Paths.get(new URI(classPath)).toAbsolutePath().toString());
+ } catch (URISyntaxException e) {
+ throw new AssertionError(
+ "Error parsing classpath uri " + classPath + ": " + e.getMessage());
+ }
+ }
+ }
+
/**
- * Gets the class path from the System Property "java.class.path" and splits
- * it up into the individual elements.
+ * Gets the class path from the System Property "java.class.path" and splits it up into the
+ * individual elements.
+ *
+ * <p>Bazel creates a classpath jar when the class path length exceeds command line length limit,
+ * read the class path value from its manifest file if it's a classpath jar.
*/
- private static String[] getClassPath() {
+ private static Set<String> getClassPath() {
String classPath = System.getProperty("java.class.path");
String separator = System.getProperty("path.separator", ":");
- return classPath.split(Pattern.quote(separator));
+ String[] classPaths = classPath.split(Pattern.quote(separator));
+ Set<String> completeClassPaths = new TreeSet<>();
+ for (String entryName : classPaths) {
+ completeClassPaths.add(entryName);
+ if (entryName.endsWith("-classpath.jar")) {
+ File classPathEntry = new File(entryName);
+ if (classPathEntry.exists() && classPathEntry.isFile()) {
+ try {
+ getClassPathsFromClasspathJar(classPathEntry, completeClassPaths);
+ } catch (IOException e) {
+ throw new AssertionError(
+ "Can't read classpath entry " + entryName + ": " + e.getMessage());
+ }
+ }
+ }
+ }
+ return completeClassPaths;
}
}