From 8b0b5e2b420d7dcdd1c47b27d6f2cea64ae9fa8f Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Wed, 1 Aug 2018 08:29:37 -0700 Subject: [PATCH] Expand wildcards on classpath, as javac does (#2085) --- .../framework/util/CheckerMain.java | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java b/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java index 7edbc4991d9..996209b832a 100644 --- a/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java +++ b/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java @@ -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; @@ -407,17 +408,59 @@ public List 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 paths) { + List 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 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.emptyList(); + } else { + return Collections.singletonList(pathElement); + } + } + + /** Return all the .jar and .JAR files in the given directory. */ + private List jarFiles(String directory) { + File dir = new File(directory); + File[] jarFiles = + dir.listFiles((d, name) -> name.endsWith(".jar") || name.endsWith(".JAR")); + List 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.