jdk/src/java.base/share/classes/jdk/internal/module/ModuleInfoWriter.java
author alanb
Fri, 28 Oct 2016 10:18:07 +0100
changeset 41817 b90ad1de93ea
parent 38425 3191fb2435f8
child 42338 a60f280f803c
permissions -rw-r--r--
8168789: ModuleReader.list and ModuleFinder.of update Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.internal.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.OutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.ModuleDescriptor.Version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.ByteBuffer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import jdk.internal.org.objectweb.asm.ClassWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import jdk.internal.org.objectweb.asm.Opcodes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import static jdk.internal.module.ClassFileAttributes.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import static jdk.internal.module.ClassFileConstants.ACC_MODULE;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * Utility class to write a ModuleDescriptor as a module-info.class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
public final class ModuleInfoWriter {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    private ModuleInfoWriter() { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
     * Writes the given module descriptor to a module-info.class file,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
     * returning it in a byte array.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    52
    private static byte[] toModuleInfo(ModuleDescriptor md) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        ClassWriter cw = new ClassWriter(0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    56
        String name = md.name().replace('.', '/') + "/module-info";
38425
3191fb2435f8 8148834: Update module-info reader/writer to 53.0
alanb
parents: 37779
diff changeset
    57
        cw.visit(Opcodes.V1_9, ACC_MODULE, name, null, null, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    59
        cw.visitAttribute(new ModuleAttribute(md));
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    60
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    61
        // for tests: write the ConcealedPackages attribute when there are non-exported packages
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    62
        long nExportedPackages = md.exports().stream()
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    63
                .map(ModuleDescriptor.Exports::source)
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    64
                .distinct()
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    65
                .count();
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    66
        if (md.packages().size() > nExportedPackages)
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    67
            cw.visitAttribute(new ConcealedPackagesAttribute(md.packages()));
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 38425
diff changeset
    68
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    69
        md.version().ifPresent(v -> cw.visitAttribute(new VersionAttribute(v)));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    70
        md.mainClass().ifPresent(mc -> cw.visitAttribute(new MainClassAttribute(mc)));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        // write the TargetPlatform attribute if have any of OS name/arch/version
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    73
        String osName = md.osName().orElse(null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    74
        String osArch = md.osArch().orElse(null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    75
        String osVersion = md.osVersion().orElse(null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        if (osName != null || osArch != null || osVersion != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            cw.visitAttribute(new TargetPlatformAttribute(osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                                                          osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                                                          osVersion));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        cw.visitEnd();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        return cw.toByteArray();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     * Writes a module descriptor to the given output stream as a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
     * module-info.class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    public static void write(ModuleDescriptor descriptor, OutputStream out)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        byte[] bytes = toModuleInfo(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        out.write(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     * Returns a {@code ByteBuffer} containing the given module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * in module-info.class format.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
    public static ByteBuffer toByteBuffer(ModuleDescriptor descriptor) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        byte[] bytes = toModuleInfo(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        return ByteBuffer.wrap(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
}