public static void main(String... args) { Tool compiler = ToolProvider.getSystemJavaCompiler(); Set<SourceVersion> expected = EnumSet.noneOf(SourceVersion.class); for (String arg : args) expected.add(SourceVersion.valueOf(arg)); Set<SourceVersion> found = compiler.getSourceVersions(); Set<SourceVersion> notExpected = EnumSet.copyOf(found); for (SourceVersion version : expected) { if (!found.contains(version)) throw new AssertionError("Expected source version not found: " + version); else notExpected.remove(version); } if (!notExpected.isEmpty()) throw new AssertionError("Unexpected source versions: " + notExpected); }
public void run() throws Exception { Optional<Tool> opt = findFirst("javadoc"); if (!opt.isPresent()) { throw new Exception("tool not found"); } if (!(opt.get() instanceof JavadocTool)) { throw new Exception("unexpected tool found"); } }
Optional<Tool> findFirst(String name) { getClass().getModule().addUses(Tool.class); for (Tool p : ServiceLoader.load(Tool.class, ClassLoader.getSystemClassLoader())) { if (p.name().equals(name)) { return Optional.of(p); } } return Optional.empty(); }
private int runShellServiceLoader(String... args) { ServiceLoader<Tool> sl = ServiceLoader.load(Tool.class); for (Tool provider : sl) { if (provider.name().equals("jshell")) { return provider.run(cmdInStream, cmdout, cmderr, args); } } throw new AssertionError("Repl tool not found by ServiceLoader: " + sl); }
static void compile(String... args) { StringBuffer sb = new StringBuffer("compile:"); for (String a: args) sb.append(' ').append(a); System.err.println(sb); Tool t = ToolProvider.getSystemJavaCompiler(); int rc = t.run(System.in, System.out, System.err, args); System.out.flush(); System.err.flush(); if (rc != 0) throw new Error("compilation failed"); }
public void run() throws Exception { Optional<Tool> opt = findFirst("javac"); if (!opt.isPresent()) { throw new Exception("tool not found"); } if (!(opt.get() instanceof JavacTool)) { throw new Exception("unexpected tool found"); } }
public void compileJava(List<JavacOption> options, List<File> sourceFiles) { List<String> args = ImmutableList.<String>builder() .addAll(options.stream().map(JavacOption::getArgs).flatMap(List::stream).iterator()) .addAll(sourceFiles.stream().map(File::getPath).iterator()) .build(); Tool javac = ToolProvider.getSystemJavaCompiler(); javac.run(null, null, null, args.toArray(new String[args.size()])); }
static int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { ServiceLoader<Tool> loader = ServiceLoader.load(Tool.class, JShellConfigRunner.class.getClassLoader()); Tool jshell = loader.stream() .map(provider -> provider.get()) .filter(tool -> tool.name().equals("jshell")) .findFirst() .orElseThrow(() -> new IllegalStateException("can not find jshell")); return jshell.run(in, out, err, arguments); }
protected static int doCompile(String dir, String classpath, List<String> files, OutputStream out, OutputStream err) { List<String> args = new ArrayList<String>(); args.add("-target"); args.add("1.5"); args.add("-d"); args.add(dir); args.add("-cp"); args.add(classpath); Tool compiler = null; for (String f : files) { Tool ftool; if (f.endsWith(".f3")) ftool = f3c; else if (f.endsWith(".java")) ftool = javac; else ftool = null; if (compiler != null && ftool != null && ftool != compiler) { throw new IllegalArgumentException("cannot compile both .java and .f3 with same compiler"); } compiler = ftool; args.add(f); } if (compiler == null) compiler = f3c; return compiler.run(null, out, err, args.toArray(new String[args.size()])); }