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. With JDK 12
  42  *          this will produce an archive with over 30,000 classes.
  43  * @requires vm.cds
  44  * @library /test/lib
  45  * @run driver LotsOfClasses
  46  */
  47 
  48 public class LotsOfClasses {
  49     static Pattern pattern;
  50 
  51     public static void main(String[] args) throws Throwable {
  52         ArrayList<String> list = new ArrayList<>();
  53         findAllClasses(list);
  54 
  55         CDSOptions opts = new CDSOptions();
  56         opts.setClassList(list);
  57         opts.addSuffix("--add-modules"); 
  58         opts.addSuffix("ALL-SYSTEM");
  59         opts.addSuffix("-Xlog:hashtables");
  60 
  61         OutputAnalyzer out = CDSTestUtils.createArchive(opts);
  62         CDSTestUtils.checkDump(out);
  63     }
  64 
  65     static void findAllClasses(ArrayList<String> list) throws Throwable {
  66         // Find all the classes in the jrt file system
  67         pattern = Pattern.compile("/modules/[a-z.]*[a-z]+/([^-]*)[.]class");
  68         FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
  69         Path base = fs.getPath("/modules/");
  70         find(base, list);
  71     }
  72 
  73     static void find(Path p, ArrayList<String> list) throws Throwable {
  74         try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
  75                 for (Path entry: stream) {
  76                     Matcher matcher = pattern.matcher(entry.toString());
  77                     if (matcher.find()) {
  78                         String className = matcher.group(1);
  79                         list.add(className);
  80                         //System.out.println(className);
  81                     }
  82                     try {
  83                         find(entry, list);
  84                     } catch (Throwable t) {}
  85                 }
  86             }
  87     }
  88 }