Skip to content

Commit

Permalink
Expand wildcards on classpath, as javac does (#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst authored and smillst committed Aug 1, 2018
1 parent b5eeed5 commit 8b0b5e2
Showing 1 changed file with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarInputStream;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -407,17 +408,59 @@ public List<String> getExecArguments() {

if (!argsListHasClassPath(argListFiles)) {
args.add("-classpath");
args.add(quote(String.join(File.pathSeparator, cpOpts)));
args.add(quote(concatenatePaths(cpOpts)));
}
if (!argsListHasProcessorPath(argListFiles)) {
args.add("-processorpath");
args.add(quote(String.join(File.pathSeparator, ppOpts)));
args.add(quote(concatenatePaths(ppOpts)));
}

args.addAll(toolOpts);
return args;
}

/** Given a list of paths, concatenate them to form a single path. Also expand wildcards. */
private String concatenatePaths(List<String> paths) {
List<String> elements = new ArrayList<>();
for (String path : paths) {
for (String element : path.split(File.pathSeparator)) {
elements.addAll(expandWildcards(element));
}
}
return String.join(File.pathSeparator, elements);
}

/** The string "/*" (on Unix). */
private static final String FILESEP_STAR = File.separator + "*";

/**
* Given a path element that might be a wildcard, return a list of the elements it expands to.
* If the element isn't a wildcard, return a singleton list containing the argument.
*/
private List<String> expandWildcards(String pathElement) {
if (pathElement.equals("*")) {
return jarFiles(".");
} else if (pathElement.endsWith(FILESEP_STAR)) {
return jarFiles(pathElement.substring(0, pathElement.length() - 1));
} else if (pathElement.equals("")) {
return Collections.<String>emptyList();
} else {
return Collections.singletonList(pathElement);
}
}

/** Return all the .jar and .JAR files in the given directory. */
private List<String> jarFiles(String directory) {
File dir = new File(directory);
File[] jarFiles =
dir.listFiles((d, name) -> name.endsWith(".jar") || name.endsWith(".JAR"));
List<String> result = new ArrayList<>(jarFiles.length);
for (File jarFile : jarFiles) {
result.add(jarFile.toString());
}
return result;
}

/**
* Invoke the JSR308 Type Annotations Compiler with all relevant jars on its classpath or boot
* classpath.
Expand Down

0 comments on commit 8b0b5e2

Please sign in to comment.