Path: blob/master/test/langtools/tools/sjavac/Wrapper.java
41144 views
/*1* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.lang.reflect.Method;25import java.nio.file.Files;26import java.nio.file.Path;27import java.nio.file.Paths;28import java.util.ArrayList;29import java.util.Arrays;30import java.util.List;31import java.util.stream.Collectors;3233public class Wrapper {34public static void main(String... args) throws Exception {35if (!isSJavacOnClassPath()) {36System.out.println("sjavac not available: pass by default");37return;38}3940String testClassName = args[0];41String[] testArgs = Arrays.copyOfRange(args, 1, args.length);4243Path srcDir = Paths.get(System.getProperty("test.src"));44Path clsDir = Paths.get(System.getProperty("test.classes"));45String clsPath = System.getProperty("test.class.path");46String tstMdls = System.getProperty("test.modules");4748Path src = srcDir.resolve(testClassName + ".java");49Path cls = clsDir.resolve(testClassName + ".class");5051if (isNewer(src, cls)) {52System.err.println("Recompiling test class...");53List<String> javacArgs = new ArrayList<>();54javacArgs.addAll(Arrays.asList("-d", clsDir.toString()));55javacArgs.addAll(Arrays.asList("-sourcepath", srcDir.toString()));56javacArgs.addAll(Arrays.asList("-classpath", clsPath));57Arrays.stream(tstMdls.split("\\s+"))58.filter(s -> s.contains("/"))59.map(s -> "--add-exports=" + s + "=ALL-UNNAMED")60.collect(Collectors.toCollection(() -> javacArgs));61javacArgs.add(src.toString());62System.out.println("javac: " + javacArgs);63int rc = com.sun.tools.javac.Main.compile(64javacArgs.toArray(new String[javacArgs.size()]));65if (rc != 0)66throw new Exception("compilation failed");67}6869Class<?> sjavac = Class.forName(testClassName);70Method main = sjavac.getMethod("main", String[].class);71main.invoke(null, new Object[] { testArgs });72}7374private static boolean isNewer(Path a, Path b) throws IOException {75if (Files.notExists(b))76return true;77return Files.getLastModifiedTime(a).compareTo(Files.getLastModifiedTime(b)) > 0;78}7980private static boolean isSJavacOnClassPath() {81String cls = "com/sun/tools/sjavac/Main.class";82return Wrapper.class.getClassLoader().getResource(cls) != null;83}84}858687