src/java.base/share/classes/jdk/internal/module/ModuleInfoWriter.java
changeset 59133 580fb715b29d
parent 48203 4fd79561f38f
equal deleted inserted replaced
59132:189f47d990b5 59133:580fb715b29d
    32 import java.util.stream.Stream;
    32 import java.util.stream.Stream;
    33 
    33 
    34 import jdk.internal.org.objectweb.asm.ClassWriter;
    34 import jdk.internal.org.objectweb.asm.ClassWriter;
    35 import jdk.internal.org.objectweb.asm.ModuleVisitor;
    35 import jdk.internal.org.objectweb.asm.ModuleVisitor;
    36 import jdk.internal.org.objectweb.asm.Opcodes;
    36 import jdk.internal.org.objectweb.asm.Opcodes;
       
    37 import jdk.internal.org.objectweb.asm.commons.ModuleResolutionAttribute;
    37 import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
    38 import jdk.internal.org.objectweb.asm.commons.ModuleTargetAttribute;
    38 import static jdk.internal.org.objectweb.asm.Opcodes.*;
    39 import static jdk.internal.org.objectweb.asm.Opcodes.*;
    39 
    40 
    40 /**
    41 /**
    41  * Utility class to write a ModuleDescriptor as a module-info.class.
    42  * Utility class to write a ModuleDescriptor as a module-info.class.
    76 
    77 
    77     /**
    78     /**
    78      * Writes the given module descriptor to a module-info.class file,
    79      * Writes the given module descriptor to a module-info.class file,
    79      * returning it in a byte array.
    80      * returning it in a byte array.
    80      */
    81      */
    81     private static byte[] toModuleInfo(ModuleDescriptor md, ModuleTarget target) {
    82     private static byte[] toModuleInfo(ModuleDescriptor md,
       
    83                                        ModuleResolution mres,
       
    84                                        ModuleTarget target) {
    82         ClassWriter cw = new ClassWriter(0);
    85         ClassWriter cw = new ClassWriter(0);
    83         cw.visit(Opcodes.V10, ACC_MODULE, "module-info", null, null, null);
    86         cw.visit(Opcodes.V10, ACC_MODULE, "module-info", null, null, null);
    84 
    87 
    85         int moduleFlags = md.modifiers().stream()
    88         int moduleFlags = md.modifiers().stream()
    86                 .map(MODULE_MODS_TO_FLAGS::get)
    89                 .map(MODULE_MODS_TO_FLAGS::get)
   145             .map(mc -> mc.replace('.', '/'))
   148             .map(mc -> mc.replace('.', '/'))
   146             .ifPresent(mv::visitMainClass);
   149             .ifPresent(mv::visitMainClass);
   147 
   150 
   148         mv.visitEnd();
   151         mv.visitEnd();
   149 
   152 
       
   153         // write ModuleResolution attribute if specified
       
   154         if (mres != null) {
       
   155             cw.visitAttribute(new ModuleResolutionAttribute(mres.value()));
       
   156         }
       
   157 
   150         // write ModuleTarget attribute if there is a target platform
   158         // write ModuleTarget attribute if there is a target platform
   151         if (target != null && target.targetPlatform().length() > 0) {
   159         if (target != null && target.targetPlatform().length() > 0) {
   152             cw.visitAttribute(new ModuleTargetAttribute(target.targetPlatform()));
   160             cw.visitAttribute(new ModuleTargetAttribute(target.targetPlatform()));
   153         }
   161         }
   154 
   162 
   159     /**
   167     /**
   160      * Writes a module descriptor to the given output stream as a
   168      * Writes a module descriptor to the given output stream as a
   161      * module-info.class.
   169      * module-info.class.
   162      */
   170      */
   163     public static void write(ModuleDescriptor descriptor,
   171     public static void write(ModuleDescriptor descriptor,
       
   172                              ModuleResolution mres,
   164                              ModuleTarget target,
   173                              ModuleTarget target,
   165                              OutputStream out)
   174                              OutputStream out)
   166         throws IOException
   175         throws IOException
   167     {
   176     {
   168         byte[] bytes = toModuleInfo(descriptor, target);
   177         byte[] bytes = toModuleInfo(descriptor, mres, target);
   169         out.write(bytes);
   178         out.write(bytes);
   170     }
   179     }
   171 
   180 
   172     /**
   181     /**
   173      * Writes a module descriptor to the given output stream as a
   182      * Writes a module descriptor to the given output stream as a
   174      * module-info.class.
   183      * module-info.class.
   175      */
   184      */
       
   185     public static void write(ModuleDescriptor descriptor,
       
   186                              ModuleResolution mres,
       
   187                              OutputStream out)
       
   188         throws IOException
       
   189     {
       
   190         write(descriptor, mres, null, out);
       
   191     }
       
   192 
       
   193     /**
       
   194      * Writes a module descriptor to the given output stream as a
       
   195      * module-info.class.
       
   196      */
       
   197     public static void write(ModuleDescriptor descriptor,
       
   198                              ModuleTarget target,
       
   199                              OutputStream out)
       
   200         throws IOException
       
   201     {
       
   202         write(descriptor, null, target, out);
       
   203     }
       
   204 
       
   205     /**
       
   206      * Writes a module descriptor to the given output stream as a
       
   207      * module-info.class.
       
   208      */
   176     public static void write(ModuleDescriptor descriptor, OutputStream out)
   209     public static void write(ModuleDescriptor descriptor, OutputStream out)
   177         throws IOException
   210         throws IOException
   178     {
   211     {
   179         write(descriptor, null, out);
   212         write(descriptor, null, null, out);
   180     }
   213     }
   181 
   214 
   182     /**
   215     /**
   183      * Returns a {@code ByteBuffer} containing the given module descriptor
   216      * Returns a {@code ByteBuffer} containing the given module descriptor
   184      * in module-info.class format.
   217      * in module-info.class format.
   185      */
   218      */
   186     public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
   219     public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
   187         byte[] bytes = toModuleInfo(descriptor, null);
   220         byte[] bytes = toModuleInfo(descriptor, null, null);
   188         return ByteBuffer.wrap(bytes);
   221         return ByteBuffer.wrap(bytes);
   189     }
   222     }
   190 }
   223 }