1 /*
   2  * Copyright (c) 2017, 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 package vm.runtime.defmeth.shared;
  25 
  26 import jdk.test.lib.JDKToolLauncher;
  27 import jdk.test.lib.Utils;
  28 import jdk.test.lib.process.ProcessTools;
  29 import jdk.test.lib.util.JarUtils;
  30 
  31 import java.io.File;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 import java.util.Arrays;
  36 
  37 /**
  38  * Build {@code retransform.jar} in current directory using
  39  * {@code vm/runtime/defmeth/shared/retransform.mf} and classes from
  40  * {@code vm.runtime.defmeth.shared} package.
  41  */
  42 public class BuildJar {
  43     public static void main(String[] args) {
  44         Path manifest = testRoot().resolve("vmTestbase")
  45                                   .resolve("vm")
  46                                   .resolve("runtime")
  47                                   .resolve("defmeth")
  48                                   .resolve("shared")
  49                                   .resolve("retransform.mf")
  50                                   .toAbsolutePath();
  51         if (Files.notExists(manifest)) {
  52             throw new Error("can't find manifest file: " + manifest);
  53         }
  54 
  55         Path file = foundInClassPath(Util.Transformer.class).toAbsolutePath();
  56         // Util$Transformer.class is in vm/runtime/defmeth/shared
  57         Path dir = file.getParent()
  58                        .getParent()
  59                        .getParent()
  60                        .getParent()
  61                        .getParent()
  62                        .toAbsolutePath();
  63 
  64         JDKToolLauncher jar = JDKToolLauncher.create("jar")
  65                                              .addToolArg("cmf")
  66                                              .addToolArg(manifest.toString())
  67                                              .addToolArg("retransform.jar")
  68                                              .addToolArg("-C")
  69                                              .addToolArg(dir.toString())
  70                                              .addToolArg(dir.relativize(file).toString());
  71         String[] command = jar.getCommand();
  72         try {
  73             ProcessTools.executeCommand(command)
  74                         .shouldHaveExitValue(0);
  75         } catch (Error | RuntimeException e) {
  76             throw e;
  77         } catch (Throwable e) {
  78             throw new Error("execution of jar [" + Arrays.toString(command) + "] failed", e);
  79         }
  80     }
  81 
  82     private static Path foundInClassPath(Class<?> aClass) {
  83         Path file = Paths.get(aClass.getName()
  84                                     .replace(".", File.separator) + ".class");
  85         for (String dir : Utils.TEST_CLASS_PATH.split(File.pathSeparator)) {
  86             Path result = Paths.get(dir).resolve(file);
  87             if (Files.exists(result)) {
  88                 return result;
  89             }
  90         }
  91         throw new Error("can't find " + file + " in " + Utils.TEST_CLASS_PATH);
  92     }
  93 
  94     private static Path testRoot() {
  95         Path p = Paths.get(Utils.TEST_SRC);
  96         while (!Files.exists(p.resolve("TEST.ROOT"))) {
  97             p = p.getParent();
  98         }
  99         return p;
 100     }
 101 }
 102