321 return Stream.of(result.getMessage().split("\r?\n"))
322 .collect(Collectors.toSet());
323 }
324
325 public static void checkModule(Path module, Set<String> expected) throws IOException {
326 Set<String> actual = getModuleContent(module);
327 if (!Objects.equals(actual, expected)) {
328 Set<String> unexpected = new HashSet<>(actual);
329 unexpected.removeAll(expected);
330 Set<String> notFound = new HashSet<>(expected);
331 notFound.removeAll(actual);
332 System.err.println("Unexpected files:");
333 unexpected.forEach(s -> System.err.println("\t" + s));
334 System.err.println("Not found files:");
335 notFound.forEach(s -> System.err.println("\t" + s));
336 throw new AssertionError("Module check failed.");
337 }
338 }
339
340 public static class JModTask {
341
342 private final List<Path> classpath = new ArrayList<>();
343 private final List<Path> libs = new ArrayList<>();
344 private final List<Path> cmds = new ArrayList<>();
345 private final List<Path> config = new ArrayList<>();
346 private final List<Path> jars = new ArrayList<>();
347 private final List<Path> jmods = new ArrayList<>();
348 private final List<String> options = new ArrayList<>();
349 private Path output;
350 private String hashModules;
351 private String mainClass;
352 private String moduleVersion;
353
354 public JModTask addNativeLibraries(Path cp) {
355 this.libs.add(cp);
356 return this;
357 }
358
359 public JModTask hashModules(String hash) {
360 this.hashModules = hash;
460 }
461 return options.toArray(new String[options.size()]);
462 }
463
464 public Result create() {
465 return cmd("create");
466 }
467
468 public Result list() {
469 return cmd("list");
470 }
471
472 public Result call() {
473 return cmd("");
474 }
475
476 private Result cmd(String cmd) {
477 String[] args = optionsJMod(cmd);
478 System.err.println("jmod options: " + optionsPrettyPrint(args));
479 ByteArrayOutputStream baos = new ByteArrayOutputStream();
480 int exitCode = jdk.tools.jmod.Main.run(args, new PrintStream(baos));
481 String msg = new String(baos.toByteArray());
482 return new Result(exitCode, msg, output);
483 }
484 }
485
486 public static String getPackageName(String canonicalName) {
487 int index = canonicalName.lastIndexOf('.');
488 return index > 0 ? canonicalName.substring(0, index) : "";
489 }
490
491 public static String getSimpleName(String canonicalName) {
492 int index = canonicalName.lastIndexOf('.');
493 return canonicalName.substring(index + 1);
494 }
495
496 public static class JImageTask {
497
498 private final List<Path> pluginModulePath = new ArrayList<>();
499 private final List<String> options = new ArrayList<>();
500 private Path dir;
539 }
540 options.addAll(this.options);
541 options.add(image.toString());
542 return options.toArray(new String[options.size()]);
543 }
544
545 private Result cmd(String cmd, Path returnPath) {
546 String[] args = optionsJImage(cmd);
547 System.err.println("jimage options: " + optionsPrettyPrint(args));
548 StringWriter writer = new StringWriter();
549 int exitCode = jdk.tools.jimage.Main.run(args, new PrintWriter(writer));
550 return new Result(exitCode, writer.toString(), returnPath);
551 }
552
553 public Result extract() {
554 return cmd("extract", dir);
555 }
556 }
557
558 public static class JLinkTask {
559
560 private final List<Path> jars = new ArrayList<>();
561 private final List<Path> jmods = new ArrayList<>();
562 private final List<Path> pluginModulePath = new ArrayList<>();
563 private final List<String> addMods = new ArrayList<>();
564 private final List<String> limitMods = new ArrayList<>();
565 private final List<String> options = new ArrayList<>();
566 private String modulePath;
567 // if you want to specifiy repeated --module-path option
568 private String repeatedModulePath;
569 // if you want to specifiy repeated --limit-modules option
570 private String repeatedLimitMods;
571 private Path output;
572 private Path existing;
573
574 public JLinkTask modulePath(String modulePath) {
575 this.modulePath = modulePath;
576 return this;
577 }
578
674 options.add(toPath(pluginModulePath));
675 }
676 options.addAll(this.options);
677 return options.toArray(new String[options.size()]);
678 }
679
680 private String[] optionsPostProcessJLink() {
681 List<String> options = new ArrayList<>();
682 if (existing != null) {
683 options.add(POST_PROCESS_OPTION);
684 options.add(existing.toString());
685 }
686 options.addAll(this.options);
687 return options.toArray(new String[options.size()]);
688 }
689
690 public Result call() {
691 String[] args = optionsJLink();
692 System.err.println("jlink options: " + optionsPrettyPrint(args));
693 StringWriter writer = new StringWriter();
694 int exitCode = jdk.tools.jlink.internal.Main.run(args, new PrintWriter(writer));
695 return new Result(exitCode, writer.toString(), output);
696 }
697
698 public Result callPostProcess() {
699 String[] args = optionsPostProcessJLink();
700 System.err.println("jlink options: " + optionsPrettyPrint(args));
701 StringWriter writer = new StringWriter();
702 int exitCode = jdk.tools.jlink.internal.Main.run(args, new PrintWriter(writer));
703 return new Result(exitCode, writer.toString(), output);
704 }
705 }
706
707 public static class InMemorySourceFile {
708 public final String packageName;
709 public final String className;
710 public final String source;
711
712 public InMemorySourceFile(String packageName, String simpleName, String source) {
713 this.packageName = packageName;
714 this.className = simpleName;
715 this.source = source;
716 }
717 }
718
719 public static class InMemoryFile {
720 private final String path;
721 private final byte[] bytes;
722
|
321 return Stream.of(result.getMessage().split("\r?\n"))
322 .collect(Collectors.toSet());
323 }
324
325 public static void checkModule(Path module, Set<String> expected) throws IOException {
326 Set<String> actual = getModuleContent(module);
327 if (!Objects.equals(actual, expected)) {
328 Set<String> unexpected = new HashSet<>(actual);
329 unexpected.removeAll(expected);
330 Set<String> notFound = new HashSet<>(expected);
331 notFound.removeAll(actual);
332 System.err.println("Unexpected files:");
333 unexpected.forEach(s -> System.err.println("\t" + s));
334 System.err.println("Not found files:");
335 notFound.forEach(s -> System.err.println("\t" + s));
336 throw new AssertionError("Module check failed.");
337 }
338 }
339
340 public static class JModTask {
341 static final java.util.spi.ToolProvider JMOD_TOOL =
342 java.util.spi.ToolProvider.findFirst("jmod").get();
343
344 private final List<Path> classpath = new ArrayList<>();
345 private final List<Path> libs = new ArrayList<>();
346 private final List<Path> cmds = new ArrayList<>();
347 private final List<Path> config = new ArrayList<>();
348 private final List<Path> jars = new ArrayList<>();
349 private final List<Path> jmods = new ArrayList<>();
350 private final List<String> options = new ArrayList<>();
351 private Path output;
352 private String hashModules;
353 private String mainClass;
354 private String moduleVersion;
355
356 public JModTask addNativeLibraries(Path cp) {
357 this.libs.add(cp);
358 return this;
359 }
360
361 public JModTask hashModules(String hash) {
362 this.hashModules = hash;
462 }
463 return options.toArray(new String[options.size()]);
464 }
465
466 public Result create() {
467 return cmd("create");
468 }
469
470 public Result list() {
471 return cmd("list");
472 }
473
474 public Result call() {
475 return cmd("");
476 }
477
478 private Result cmd(String cmd) {
479 String[] args = optionsJMod(cmd);
480 System.err.println("jmod options: " + optionsPrettyPrint(args));
481 ByteArrayOutputStream baos = new ByteArrayOutputStream();
482 PrintStream ps = new PrintStream(baos);
483 int exitCode = JMOD_TOOL.run(ps, ps, args);
484 String msg = new String(baos.toByteArray());
485 return new Result(exitCode, msg, output);
486 }
487 }
488
489 public static String getPackageName(String canonicalName) {
490 int index = canonicalName.lastIndexOf('.');
491 return index > 0 ? canonicalName.substring(0, index) : "";
492 }
493
494 public static String getSimpleName(String canonicalName) {
495 int index = canonicalName.lastIndexOf('.');
496 return canonicalName.substring(index + 1);
497 }
498
499 public static class JImageTask {
500
501 private final List<Path> pluginModulePath = new ArrayList<>();
502 private final List<String> options = new ArrayList<>();
503 private Path dir;
542 }
543 options.addAll(this.options);
544 options.add(image.toString());
545 return options.toArray(new String[options.size()]);
546 }
547
548 private Result cmd(String cmd, Path returnPath) {
549 String[] args = optionsJImage(cmd);
550 System.err.println("jimage options: " + optionsPrettyPrint(args));
551 StringWriter writer = new StringWriter();
552 int exitCode = jdk.tools.jimage.Main.run(args, new PrintWriter(writer));
553 return new Result(exitCode, writer.toString(), returnPath);
554 }
555
556 public Result extract() {
557 return cmd("extract", dir);
558 }
559 }
560
561 public static class JLinkTask {
562 static final java.util.spi.ToolProvider JLINK_TOOL =
563 java.util.spi.ToolProvider.findFirst("jlink").get();
564
565 private final List<Path> jars = new ArrayList<>();
566 private final List<Path> jmods = new ArrayList<>();
567 private final List<Path> pluginModulePath = new ArrayList<>();
568 private final List<String> addMods = new ArrayList<>();
569 private final List<String> limitMods = new ArrayList<>();
570 private final List<String> options = new ArrayList<>();
571 private String modulePath;
572 // if you want to specifiy repeated --module-path option
573 private String repeatedModulePath;
574 // if you want to specifiy repeated --limit-modules option
575 private String repeatedLimitMods;
576 private Path output;
577 private Path existing;
578
579 public JLinkTask modulePath(String modulePath) {
580 this.modulePath = modulePath;
581 return this;
582 }
583
679 options.add(toPath(pluginModulePath));
680 }
681 options.addAll(this.options);
682 return options.toArray(new String[options.size()]);
683 }
684
685 private String[] optionsPostProcessJLink() {
686 List<String> options = new ArrayList<>();
687 if (existing != null) {
688 options.add(POST_PROCESS_OPTION);
689 options.add(existing.toString());
690 }
691 options.addAll(this.options);
692 return options.toArray(new String[options.size()]);
693 }
694
695 public Result call() {
696 String[] args = optionsJLink();
697 System.err.println("jlink options: " + optionsPrettyPrint(args));
698 StringWriter writer = new StringWriter();
699 PrintWriter pw = new PrintWriter(writer);
700 int exitCode = JLINK_TOOL.run(pw, pw, args);
701 return new Result(exitCode, writer.toString(), output);
702 }
703
704 public Result callPostProcess() {
705 String[] args = optionsPostProcessJLink();
706 System.err.println("jlink options: " + optionsPrettyPrint(args));
707 StringWriter writer = new StringWriter();
708 PrintWriter pw = new PrintWriter(writer);
709 int exitCode = JLINK_TOOL.run(pw, pw, args);
710 return new Result(exitCode, writer.toString(), output);
711 }
712 }
713
714 public static class InMemorySourceFile {
715 public final String packageName;
716 public final String className;
717 public final String source;
718
719 public InMemorySourceFile(String packageName, String simpleName, String source) {
720 this.packageName = packageName;
721 this.className = simpleName;
722 this.source = source;
723 }
724 }
725
726 public static class InMemoryFile {
727 private final String path;
728 private final byte[] bytes;
729
|