< prev index next >
src/java.base/share/classes/jdk/internal/module/ModuleInfoWriter.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 46,56 ****
/**
* 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
--- 46,56 ----
/**
* Writes the given module descriptor to a module-info.class file,
* returning it in a byte array.
*/
! private static byte[] toModuleInfo(ModuleDescriptor md, ModuleTarget target) {
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
*** 64,101 ****
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
* module-info.class.
*/
! public static void write(ModuleDescriptor descriptor, OutputStream out)
throws IOException
{
! byte[] bytes = toModuleInfo(descriptor);
out.write(bytes);
}
/**
* Returns a {@code ByteBuffer} containing the given module descriptor
* in module-info.class format.
*/
public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
! byte[] bytes = toModuleInfo(descriptor);
return ByteBuffer.wrap(bytes);
}
-
}
--- 64,110 ----
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 if there is a platform OS/arch
! if (target != null) {
! cw.visitAttribute(new ModuleTargetAttribute(target.osName(),
! target.osArch()));
}
cw.visitEnd();
return cw.toByteArray();
}
/**
* Writes a module descriptor to the given output stream as a
* module-info.class.
*/
! public static void write(ModuleDescriptor descriptor,
! ModuleTarget target,
! OutputStream out)
throws IOException
{
! byte[] bytes = toModuleInfo(descriptor, target);
out.write(bytes);
}
/**
+ * Writes a module descriptor to the given output stream as a
+ * module-info.class.
+ */
+ public static void write(ModuleDescriptor descriptor, OutputStream out)
+ throws IOException
+ {
+ write(descriptor, null, out);
+ }
+
+ /**
* Returns a {@code ByteBuffer} containing the given module descriptor
* in module-info.class format.
*/
public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
! byte[] bytes = toModuleInfo(descriptor, null);
return ByteBuffer.wrap(bytes);
}
}
< prev index next >