44 public final class ModuleInfoWriter {
45
46 private ModuleInfoWriter() { }
47
48 /**
49 * Writes the given module descriptor to a module-info.class file,
50 * returning it in a byte array.
51 */
52 private static byte[] toModuleInfo(ModuleDescriptor descriptor) {
53
54 ClassWriter cw = new ClassWriter(0);
55
56 String name = descriptor.name().replace('.', '/') + "/module-info";
57 cw.visit(Opcodes.V1_8, ACC_MODULE, name, null, null, null);
58
59 cw.visitAttribute(new ModuleAttribute(descriptor));
60 cw.visitAttribute(new ConcealedPackagesAttribute(descriptor.conceals()));
61
62 Optional<Version> oversion = descriptor.version();
63 if (oversion.isPresent())
64 cw.visitAttribute(new VersionAttribute(oversion.get()));
65
66 Optional<String> omain = descriptor.mainClass();
67 if (omain.isPresent())
68 cw.visitAttribute(new MainClassAttribute(omain.get()));
69
70 // write the TargetPlatform attribute if have any of OS name/arch/version
71 String osName = descriptor.osName().orElse(null);
72 String osArch = descriptor.osArch().orElse(null);
73 String osVersion = descriptor.osVersion().orElse(null);
74 if (osName != null || osArch != null || osVersion != null) {
75 cw.visitAttribute(new TargetPlatformAttribute(osName,
76 osArch,
77 osVersion));
78 }
79
80 cw.visitEnd();
81
82 return cw.toByteArray();
83 }
84
85 /**
86 * Writes a module descriptor to the given output stream as a
87 * module-info.class.
88 */
|
44 public final class ModuleInfoWriter {
45
46 private ModuleInfoWriter() { }
47
48 /**
49 * Writes the given module descriptor to a module-info.class file,
50 * returning it in a byte array.
51 */
52 private static byte[] toModuleInfo(ModuleDescriptor descriptor) {
53
54 ClassWriter cw = new ClassWriter(0);
55
56 String name = descriptor.name().replace('.', '/') + "/module-info";
57 cw.visit(Opcodes.V1_8, ACC_MODULE, name, null, null, null);
58
59 cw.visitAttribute(new ModuleAttribute(descriptor));
60 cw.visitAttribute(new ConcealedPackagesAttribute(descriptor.conceals()));
61
62 Optional<Version> oversion = descriptor.version();
63 if (oversion.isPresent())
64 cw.visitAttribute(new VersionAttribute(oversion.getWhenPresent()));
65
66 Optional<String> omain = descriptor.mainClass();
67 if (omain.isPresent())
68 cw.visitAttribute(new MainClassAttribute(omain.getWhenPresent()));
69
70 // write the TargetPlatform attribute if have any of OS name/arch/version
71 String osName = descriptor.osName().orElse(null);
72 String osArch = descriptor.osArch().orElse(null);
73 String osVersion = descriptor.osVersion().orElse(null);
74 if (osName != null || osArch != null || osVersion != null) {
75 cw.visitAttribute(new TargetPlatformAttribute(osName,
76 osArch,
77 osVersion));
78 }
79
80 cw.visitEnd();
81
82 return cw.toByteArray();
83 }
84
85 /**
86 * Writes a module descriptor to the given output stream as a
87 * module-info.class.
88 */
|