1 /*
   2  * Copyright (c) 2015, 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 org.openjdk.buildtools.symbolgenerator;
  27 
  28 import com.sun.source.util.JavacTask;
  29 import com.sun.tools.javac.api.JavacTool;
  30 import com.sun.tools.javac.util.Context;
  31 import java.io.BufferedInputStream;
  32 import java.io.ByteArrayOutputStream;
  33 import java.io.File;
  34 import java.io.FileOutputStream;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.io.OutputStream;
  38 import java.nio.charset.Charset;
  39 import java.util.Arrays;
  40 import java.util.EnumSet;
  41 import java.util.List;
  42 import java.util.Set;
  43 
  44 import javax.tools.JavaFileManager;
  45 import javax.tools.JavaFileManager.Location;
  46 import javax.tools.JavaFileObject;
  47 import javax.tools.JavaFileObject.Kind;
  48 import javax.tools.StandardLocation;
  49 
  50 /**A tool to dump the content of the default javac's bootclasspath. This tool should not use any
  51  * features not available on the oldest supported target JDK, or not available in the boot JDK.
  52  *
  53  * For more information on use of this site, please see CreateSymbols.
  54  */
  55 public class ProbeModular {
  56 
  57     public static void main(String... args) throws IOException {
  58         if (args.length != 1) {
  59             System.err.println("Not enough arguments.");
  60             System.err.println("Usage:");
  61             System.err.println("    java " +
  62                                ProbeModular.class.getName() +
  63                                " <output-file>");
  64             return ;
  65         }
  66 
  67         File outFile = new File(args[0]);
  68         Charset cs = Charset.forName("UTF-8");
  69         JavacTool tool = JavacTool.create();
  70         Context ctx = new Context();
  71         String version = System.getProperty("java.specification.version");
  72         JavacTask task = tool.getTask(null, null, null,
  73                                       Arrays.asList("--release", version),
  74                                       null, null, ctx);
  75         task.getElements().getTypeElement("java.lang.Object");
  76         JavaFileManager fm = ctx.get(JavaFileManager.class);
  77 
  78         try (OutputStream out = new FileOutputStream(outFile)) {
  79             for (Location modLoc : LOCATIONS) {
  80                 for (Set<JavaFileManager.Location> module :
  81                         fm.listLocationsForModules(modLoc)) {
  82                     for (JavaFileManager.Location loc : module) {
  83                         Iterable<JavaFileObject> files =
  84                                 fm.list(loc,
  85                                         "",
  86                                         EnumSet.of(Kind.CLASS),
  87                                         true);
  88 
  89                         for (JavaFileObject jfo : files) {
  90                             try (InputStream is = jfo.openInputStream();
  91                                  InputStream in =
  92                                          new BufferedInputStream(is)) {
  93                                 ByteArrayOutputStream baos =
  94                                         new ByteArrayOutputStream();
  95                                 StringBuilder textual = new StringBuilder();
  96                                 int read;
  97 
  98                                 while ((read = in.read()) != (-1)) {
  99                                     baos.write(read);
 100                                     String hex = String.format("%02x", read);
 101                                     textual.append(hex);
 102                                 }
 103 
 104                                 textual.append("\n");
 105                                 out.write(textual.toString().getBytes(cs));
 106                             }
 107                         }
 108                     }
 109                 }
 110             }
 111         }
 112     }
 113     //where:
 114         private static final List<StandardLocation> LOCATIONS =
 115                 Arrays.asList(StandardLocation.SYSTEM_MODULES,
 116                               StandardLocation.UPGRADE_MODULE_PATH);
 117 
 118 }