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