1 /*
   2  * Copyright (c) 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 import java.net.URI;
  26 import java.nio.file.DirectoryStream;
  27 import java.nio.file.Files;
  28 import java.nio.file.FileSystem;
  29 import java.nio.file.FileSystems;
  30 import java.nio.file.Path;
  31 import java.util.ArrayList;
  32 import java.util.regex.Matcher;
  33 import java.util.regex.Pattern;
  34 
  35 import jdk.test.lib.cds.CDSTestUtils;
  36 import jdk.test.lib.cds.CDSOptions;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 
  39 /*
  40  * @test
  41  * @summary Try to archive lots of classes by searching for classes from the jrt:/ file system
  42  * @requires vm.cds
  43  * @library /test/lib
  44  * @run driver LotsOfClasses
  45  */
  46 
  47 public class LotsOfClasses {
  48     static Pattern pattern;
  49 
  50     public static void main(String[] args) throws Throwable {
  51         ArrayList<String> list = new ArrayList<>();
  52         findAllClasses(list);
  53 
  54         CDSOptions opts = new CDSOptions();
  55         opts.setClassList(list);
  56         opts.addSuffix("--add-modules"); 
  57         opts.addSuffix("ALL-SYSTEM");
  58         opts.addSuffix("-Xlog:hashtables");
  59 
  60         OutputAnalyzer out = CDSTestUtils.createArchive(opts);
  61         CDSTestUtils.checkDump(out);
  62     }
  63 
  64     static void findAllClasses(ArrayList<String> list) throws Throwable {
  65         // Find all the classes in the jrt file system
  66         pattern = Pattern.compile("/modules/[a-z.]*[a-z]+/([^-]*)[.]class");
  67         FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
  68         Path base = fs.getPath("/modules/");
  69         find(base, list);
  70     }
  71 
  72     static void find(Path p, ArrayList<String> list) throws Throwable {
  73         try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
  74                 for (Path entry: stream) {
  75                     Matcher matcher = pattern.matcher(entry.toString());
  76                     if (matcher.find()) {
  77                         String className = matcher.group(1);
  78                         list.add(className);
  79                         //System.out.println(className);
  80                     }
  81                     try {
  82                         find(entry, list);
  83                     } catch (Throwable t) {}
  84                 }
  85             }
  86     }
  87 }