< prev index next >
src/java.base/share/classes/jdk/internal/module/ModuleInfoWriter.java
Print this page
*** 47,84 ****
/**
* Writes the given module descriptor to a module-info.class file,
* returning it in a byte array.
*/
private static byte[] toModuleInfo(ModuleDescriptor md) {
-
ClassWriter cw = new ClassWriter(0);
! cw.visit(Opcodes.V1_9, ACC_MODULE, null, null, null, null);
cw.visitAttribute(new ModuleAttribute(md));
! // for tests: write the Packages attribute when there are packages that
! // aren't exported or open
Stream<String> exported = md.exports().stream()
.map(ModuleDescriptor.Exports::source);
Stream<String> open = md.opens().stream()
.map(ModuleDescriptor.Opens::source);
long exportedOrOpen = Stream.concat(exported, open).distinct().count();
if (md.packages().size() > exportedOrOpen)
cw.visitAttribute(new ModulePackagesAttribute(md.packages()));
! md.version().ifPresent(v -> cw.visitAttribute(new ModuleVersionAttribute(v)));
md.mainClass().ifPresent(mc -> cw.visitAttribute(new ModuleMainClassAttribute(mc)));
! // write the TargetPlatform attribute if have any of OS name/arch/version
String osName = md.osName().orElse(null);
String osArch = md.osArch().orElse(null);
String osVersion = md.osVersion().orElse(null);
if (osName != null || osArch != null || osVersion != null) {
cw.visitAttribute(new ModuleTargetAttribute(osName, osArch, osVersion));
}
cw.visitEnd();
-
return cw.toByteArray();
}
/**
* Writes a module descriptor to the given output stream as a
--- 47,82 ----
/**
* Writes the given module descriptor to a module-info.class file,
* returning it in a byte array.
*/
private static byte[] toModuleInfo(ModuleDescriptor md) {
ClassWriter cw = new ClassWriter(0);
! cw.visit(Opcodes.V1_9, ACC_MODULE, "module-info", null, null, null);
cw.visitAttribute(new ModuleAttribute(md));
! // for tests: write the ModulePackages attribute when there are packages
! // that aren't exported or open
Stream<String> exported = md.exports().stream()
.map(ModuleDescriptor.Exports::source);
Stream<String> open = md.opens().stream()
.map(ModuleDescriptor.Opens::source);
long exportedOrOpen = Stream.concat(exported, open).distinct().count();
if (md.packages().size() > exportedOrOpen)
cw.visitAttribute(new ModulePackagesAttribute(md.packages()));
! // write ModuleMainClass if the module has a main class
md.mainClass().ifPresent(mc -> cw.visitAttribute(new ModuleMainClassAttribute(mc)));
! // write ModuleTarget attribute if have any of OS name/arch/version
String osName = md.osName().orElse(null);
String osArch = md.osArch().orElse(null);
String osVersion = md.osVersion().orElse(null);
if (osName != null || osArch != null || osVersion != null) {
cw.visitAttribute(new ModuleTargetAttribute(osName, osArch, osVersion));
}
cw.visitEnd();
return cw.toByteArray();
}
/**
* Writes a module descriptor to the given output stream as a
< prev index next >