jdk/make/src/classes/build/tools/jigsaw/AddPackagesAttribute.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 41224 aa0e64b5711c
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41224
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     1
/*
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     4
 *
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    10
 *
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    15
 * accompanied this code).
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    16
 *
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    20
 *
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    23
 * questions.
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    24
 */
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    25
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    26
package build.tools.jigsaw;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    27
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    29
import java.io.IOException;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    30
import java.io.InputStream;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    31
import java.lang.module.ModuleFinder;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    32
import java.lang.module.ModuleReference;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    33
import java.nio.file.DirectoryStream;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    34
import java.nio.file.Files;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    35
import java.nio.file.Path;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    36
import java.nio.file.Paths;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    37
import java.util.Optional;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    38
import java.util.Set;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    39
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    40
import jdk.internal.module.ModuleInfoExtender;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    41
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    42
/**
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    43
 * Adds the Packages class file attribute to each module-info.class in an
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    44
 * exploded build.
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    45
 */
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    46
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    47
public class AddPackagesAttribute {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    48
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    49
    public static void main(String[] args) throws IOException {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    50
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    51
        if (args.length != 1) {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    52
            System.err.println("Usage AddPackagesAttribute exploded-java-home");
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    53
            System.exit(-1);
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    54
        }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    55
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    56
        String home = args[0];
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    57
        Path dir = Paths.get(home, "modules");
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    58
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    59
        ModuleFinder finder = ModuleFinder.of(dir);
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    60
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    61
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    62
            for (Path entry : stream) {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    63
                Path mi = entry.resolve("module-info.class");
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    64
                if (Files.isRegularFile(mi)) {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    65
                    String mn = entry.getFileName().toString();
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    66
                    Optional<ModuleReference> omref = finder.find(mn);
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    67
                    if (omref.isPresent()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41224
diff changeset
    68
                        Set<String> packages = omref.get().descriptor().packages();
41224
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    69
                        addPackagesAttribute(mi, packages);
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    70
                    }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    71
                }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    72
            }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    73
        }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    74
    }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    75
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    76
    static void addPackagesAttribute(Path mi, Set<String> packages) throws IOException {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    77
        byte[] bytes;
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    78
        try (InputStream in = Files.newInputStream(mi)) {
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    79
            ModuleInfoExtender extender = ModuleInfoExtender.newExtender(in);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41224
diff changeset
    80
            extender.packages(packages);
41224
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    81
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    82
            extender.write(baos);
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    83
            bytes = baos.toByteArray();
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    84
        }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    85
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    86
        Files.write(mi, bytes);
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    87
    }
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    88
aa0e64b5711c 8166948: Exploded image too slow to be usable
erikj
parents:
diff changeset
    89
}