jdk/make/non-build-utils/src/build/tools/makeclasslist/MakeClasslist.java
changeset 31556 aad9d45ea14e
parent 31555 10a8368b188b
parent 31551 a2c0cc28d801
child 31558 b84b84aac58a
equal deleted inserted replaced
31555:10a8368b188b 31556:aad9d45ea14e
     1 /*
       
     2  * Copyright (c) 2003, 2013, 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.makeclasslist;
       
    27 
       
    28 import java.io.*;
       
    29 import java.util.*;
       
    30 import java.util.jar.*;
       
    31 
       
    32 /** Reads a set of files containing the output of java
       
    33     -XX:+TraceClassLoadingPreorder runs. Finds all classes that were
       
    34     loaded from the bootstrap class path by comparing the prefix of
       
    35     the load path to the current JRE's java.home system property.
       
    36     Prints the names of these classes to stdout.
       
    37 */
       
    38 
       
    39 public class MakeClasslist {
       
    40   public static void main(String[] args) throws IOException {
       
    41     List<String> classes = new ArrayList<>();
       
    42     String origJavaHome = System.getProperty("java.home");
       
    43     String javaHome     = origJavaHome.toLowerCase();
       
    44     if (javaHome.endsWith("jre")) {
       
    45       origJavaHome = origJavaHome.substring(0, origJavaHome.length() - 4);
       
    46       javaHome     = javaHome.substring(0, javaHome.length() - 4);
       
    47     }
       
    48     for (int i = 0; i < args.length; i++) {
       
    49       try {
       
    50         File file = new File(args[i]);
       
    51         BufferedReader reader = new BufferedReader(new FileReader(file));
       
    52         String line = null;
       
    53         while ((line = reader.readLine()) != null) {
       
    54           StringTokenizer tok = new StringTokenizer(line, "[ \t\n\r\f");
       
    55           if (tok.hasMoreTokens()) {
       
    56             String t = tok.nextToken();
       
    57             // Understand only "Loading" from -XX:+TraceClassLoadingPreorder.
       
    58             // This ignores old "Loaded" from -verbose:class to force correct
       
    59             // classlist generation on Mustang.
       
    60             if (t.equals("Loading")) {
       
    61               t = tok.nextToken();
       
    62               t = t.replace('.', '/');
       
    63 
       
    64               // Check to make sure it came from the boot class path
       
    65               if (tok.hasMoreTokens()) {
       
    66                 String tmp = tok.nextToken();
       
    67                 if (tmp.equals("from")) {
       
    68                   if (tok.hasMoreTokens()) {
       
    69                     tmp = tok.nextToken().toLowerCase();
       
    70                     // System.err.println("Loaded " + t + " from " + tmp);
       
    71                     if (tmp.startsWith(javaHome)) {
       
    72                       // OK, remember this class for later
       
    73                       classes.add(t);
       
    74                     }
       
    75                   }
       
    76                 }
       
    77               }
       
    78             }
       
    79           }
       
    80         }
       
    81       } catch (IOException e) {
       
    82         System.err.println("Error reading file " + args[i]);
       
    83         throw(e);
       
    84       }
       
    85     }
       
    86 
       
    87     Set<String> seenClasses = new HashSet<>();
       
    88 
       
    89     for (String str : classes) {
       
    90       if (seenClasses.add(str)) {
       
    91         System.out.println(str);
       
    92       }
       
    93     }
       
    94 
       
    95     // Try to complete certain packages
       
    96     // Note: not using this new code yet; need to consider whether the
       
    97     // footprint increase is worth any startup gains
       
    98     // Note also that the packages considered below for completion are
       
    99     // (obviously) platform-specific
       
   100     // JarFile rtJar = new JarFile(origJavaHome + File.separator +
       
   101     //                             "jre" + File.separator +
       
   102     //                             "lib" + File.separator +
       
   103     //                             "rt.jar");
       
   104     // completePackage(seenClasses, rtJar, "java/awt");
       
   105     // completePackage(seenClasses, rtJar, "sun/awt");
       
   106     // completePackage(seenClasses, rtJar, "sun/awt/X11");
       
   107     // completePackage(seenClasses, rtJar, "java/awt/im/spi");
       
   108     // completePackage(seenClasses, rtJar, "java/lang");
       
   109   }
       
   110 
       
   111   private static void completePackage(Set<String> seenClasses,
       
   112                                       JarFile jar,
       
   113                                       String packageName) {
       
   114     int len = packageName.length();
       
   115     Enumeration<JarEntry> entries = jar.entries();
       
   116     while (entries.hasMoreElements()) {
       
   117       JarEntry entry = entries.nextElement();
       
   118       String name = entry.getName();
       
   119       if (name.startsWith(packageName) &&
       
   120           name.endsWith(".class") &&
       
   121           name.lastIndexOf('/') == len) {
       
   122         // Trim ".class" from end
       
   123         name = name.substring(0, name.length() - 6);
       
   124         if (seenClasses.add(name)) {
       
   125           System.out.println(name);
       
   126         }
       
   127       }
       
   128     }
       
   129   }
       
   130 }