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").orElseThrow(() ->
343 new RuntimeException("jmod tool not found")
344 );
345
346 private final List<Path> classpath = new ArrayList<>();
347 private final List<Path> libs = new ArrayList<>();
348 private final List<Path> cmds = new ArrayList<>();
349 private final List<Path> config = new ArrayList<>();
350 private final List<Path> jars = new ArrayList<>();
351 private final List<Path> jmods = new ArrayList<>();
352 private final List<String> options = new ArrayList<>();
353 private Path output;
354 private String hashModules;
355 private String mainClass;
356 private String moduleVersion;
357
358 public JModTask addNativeLibraries(Path cp) {
359 this.libs.add(cp);
360 return this;
361 }
362
363 public JModTask hashModules(String hash) {
364 this.hashModules = hash;
464 }
465 return options.toArray(new String[options.size()]);
466 }
467
468 public Result create() {
469 return cmd("create");
470 }
471
472 public Result list() {
473 return cmd("list");
474 }
475
476 public Result call() {
477 return cmd("");
478 }
479
480 private Result cmd(String cmd) {
481 String[] args = optionsJMod(cmd);
482 System.err.println("jmod options: " + optionsPrettyPrint(args));
483 ByteArrayOutputStream baos = new ByteArrayOutputStream();
484 PrintStream ps = new PrintStream(baos);
485 int exitCode = JMOD_TOOL.run(ps, ps, args);
486 String msg = new String(baos.toByteArray());
487 return new Result(exitCode, msg, output);
488 }
489 }
490
491 public static String getPackageName(String canonicalName) {
492 int index = canonicalName.lastIndexOf('.');
493 return index > 0 ? canonicalName.substring(0, index) : "";
494 }
495
496 public static String getSimpleName(String canonicalName) {
497 int index = canonicalName.lastIndexOf('.');
498 return canonicalName.substring(index + 1);
499 }
500
501 public static class JImageTask {
502
503 private final List<Path> pluginModulePath = new ArrayList<>();
504 private final List<String> options = new ArrayList<>();
505 private Path dir;
544 }
545 options.addAll(this.options);
546 options.add(image.toString());
547 return options.toArray(new String[options.size()]);
548 }
549
550 private Result cmd(String cmd, Path returnPath) {
551 String[] args = optionsJImage(cmd);
552 System.err.println("jimage options: " + optionsPrettyPrint(args));
553 StringWriter writer = new StringWriter();
554 int exitCode = jdk.tools.jimage.Main.run(args, new PrintWriter(writer));
555 return new Result(exitCode, writer.toString(), returnPath);
556 }
557
558 public Result extract() {
559 return cmd("extract", dir);
560 }
561 }
562
563 public static class JLinkTask {
564 static final java.util.spi.ToolProvider JLINK_TOOL =
565 java.util.spi.ToolProvider.findFirst("jlink").orElseThrow(() ->
566 new RuntimeException("jlink tool not found")
567 );
568
569 private final List<Path> jars = new ArrayList<>();
570 private final List<Path> jmods = new ArrayList<>();
571 private final List<Path> pluginModulePath = new ArrayList<>();
572 private final List<String> addMods = new ArrayList<>();
573 private final List<String> limitMods = new ArrayList<>();
574 private final List<String> options = new ArrayList<>();
575 private String modulePath;
576 // if you want to specifiy repeated --module-path option
577 private String repeatedModulePath;
578 // if you want to specifiy repeated --limit-modules option
579 private String repeatedLimitMods;
580 private Path output;
581 private Path existing;
582
583 public JLinkTask modulePath(String modulePath) {
584 this.modulePath = modulePath;
585 return this;
586 }
587
683 options.add(toPath(pluginModulePath));
684 }
685 options.addAll(this.options);
686 return options.toArray(new String[options.size()]);
687 }
688
689 private String[] optionsPostProcessJLink() {
690 List<String> options = new ArrayList<>();
691 if (existing != null) {
692 options.add(POST_PROCESS_OPTION);
693 options.add(existing.toString());
694 }
695 options.addAll(this.options);
696 return options.toArray(new String[options.size()]);
697 }
698
699 public Result call() {
700 String[] args = optionsJLink();
701 System.err.println("jlink options: " + optionsPrettyPrint(args));
702 StringWriter writer = new StringWriter();
703 PrintWriter pw = new PrintWriter(writer);
704 int exitCode = JLINK_TOOL.run(pw, pw, args);
705 return new Result(exitCode, writer.toString(), output);
706 }
707
708 public Result callPostProcess() {
709 String[] args = optionsPostProcessJLink();
710 System.err.println("jlink options: " + optionsPrettyPrint(args));
711 StringWriter writer = new StringWriter();
712 PrintWriter pw = new PrintWriter(writer);
713 int exitCode = JLINK_TOOL.run(pw, pw, args);
714 return new Result(exitCode, writer.toString(), output);
715 }
716 }
717
718 public static class InMemorySourceFile {
719 public final String packageName;
720 public final String className;
721 public final String source;
722
723 public InMemorySourceFile(String packageName, String simpleName, String source) {
724 this.packageName = packageName;
725 this.className = simpleName;
726 this.source = source;
727 }
728 }
729
730 public static class InMemoryFile {
731 private final String path;
732 private final byte[] bytes;
733
|