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  * @test
  26  * @bug 8207954
  27  * @summary Verify that CreateSymbols can handle classfiles from the current release.
  28  * @library /tools/lib /tools/javac/lib
  29  * @modules jdk.compiler/com.sun.tools.javac.api
  30  *          jdk.compiler/com.sun.tools.javac.main
  31  * @build toolbox.ToolBox toolbox.JavacTask toolbox.Task
  32  * @run main CanHandleClassFilesTest
  33  */
  34 
  35 import java.io.Writer;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 
  40 import javax.tools.StandardLocation;
  41 
  42 import toolbox.JavacTask;
  43 import toolbox.ToolBox;
  44 
  45 public class CanHandleClassFilesTest {
  46 
  47     public static void main(String... args) throws Exception {
  48         new CanHandleClassFilesTest().run();
  49     }
  50 
  51     void run() throws Exception {
  52         var testSrc = Paths.get(System.getProperty("test.src", "."));
  53         var targetDir = Paths.get(".");
  54         Path createSymbols = null;
  55         Path includeList = null;
  56         for (Path d = testSrc; d != null; d = d.getParent()) {
  57             if (Files.exists(d.resolve("TEST.ROOT"))) {
  58                 d = d.getParent().getParent();
  59                 Path test = d.resolve("make/langtools/src/classes/build/tools/symbolgenerator/CreateSymbols.java");
  60                 if (Files.exists(test)) {
  61                     createSymbols = test;
  62                     includeList = d.resolve("make/data/symbols/include.list");
  63                     break;
  64                 }
  65             }
  66         }
  67         if (createSymbols == null || includeList == null || !Files.isReadable(includeList)) {
  68             System.err.println("Warning: sources not found, test skipped.");
  69             return ;
  70         }
  71         try (ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager()) {
  72             ToolBox tb = new ToolBox();
  73             new JavacTask(tb)
  74               .options("--add-exports", "jdk.jdeps/com.sun.tools.classfile=ALL-UNNAMED",
  75                        "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
  76                        "--add-exports", "jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
  77                        "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
  78                        "--add-modules", "jdk.jdeps")
  79               .files(createSymbols)
  80               .fileManager(mfm)
  81               .run()
  82               .writeAll();
  83 
  84             ClassLoader cl = new ClassLoader() {
  85                 @Override
  86                 protected Class<?> findClass(String name) throws ClassNotFoundException {
  87                     byte[] data = mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, name);
  88                     if (data != null) {
  89                         return defineClass(name, data, 0, data.length);
  90                     } else {
  91                         throw new ClassNotFoundException(name);
  92                     }
  93                 }
  94             };
  95 
  96             var createSymbolsClass = Class.forName("build.tools.symbolgenerator.CreateSymbols", false, cl);
  97             var main = createSymbolsClass.getDeclaredMethod("main", String[].class);
  98             var symbols = targetDir.resolve("symbols");
  99 
 100             try (Writer w = Files.newBufferedWriter(symbols)) {}
 101 
 102             main.invoke(null,
 103                         (Object) new String[] {"build-description-incremental",
 104                                                symbols.toAbsolutePath().toString(),
 105                                                includeList.toAbsolutePath().toString()});
 106 
 107             main.invoke(null,
 108                         (Object) new String[] {"build-ctsym",
 109                                                "does-not-exist",
 110                                                symbols.toAbsolutePath().toString(),
 111                                                targetDir.resolve("ct.sym").toAbsolutePath().toString()});
 112         }
 113     }
 114 
 115 }