author | bobv |
Tue, 07 Nov 2017 10:30:53 -0500 | |
changeset 47801 | c7b50c23ea71 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
1 |
/* |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
4 |
* |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
32337 | 7 |
* published by the Free Software Foundation. |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
8 |
* |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
13 |
* accompanied this code). |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
14 |
* |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
18 |
* |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
21 |
* questions. |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
22 |
*/ |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
23 |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
24 |
import java.io.IOException; |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
25 |
import java.lang.reflect.Method; |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
26 |
import java.nio.file.Files; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
27 |
import java.nio.file.Path; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
28 |
import java.nio.file.Paths; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
29 |
import java.util.ArrayList; |
25603
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
30 |
import java.util.Arrays; |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
31 |
import java.util.List; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
32 |
import java.util.stream.Collectors; |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
33 |
|
25603
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
34 |
public class Wrapper { |
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
35 |
public static void main(String... args) throws Exception { |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
36 |
if (!isSJavacOnClassPath()) { |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
37 |
System.out.println("sjavac not available: pass by default"); |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
38 |
return; |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
39 |
} |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
40 |
|
25603
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
41 |
String testClassName = args[0]; |
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
42 |
String[] testArgs = Arrays.copyOfRange(args, 1, args.length); |
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
43 |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
44 |
Path srcDir = Paths.get(System.getProperty("test.src")); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
45 |
Path clsDir = Paths.get(System.getProperty("test.classes")); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
46 |
String clsPath = System.getProperty("test.class.path"); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
47 |
String tstMdls = System.getProperty("test.modules"); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
48 |
|
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
49 |
Path src = srcDir.resolve(testClassName + ".java"); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
50 |
Path cls = clsDir.resolve(testClassName + ".class"); |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
51 |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
52 |
if (isNewer(src, cls)) { |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
53 |
System.err.println("Recompiling test class..."); |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
54 |
List<String> javacArgs = new ArrayList<>(); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
55 |
javacArgs.addAll(Arrays.asList("-d", clsDir.toString())); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
56 |
javacArgs.addAll(Arrays.asList("-sourcepath", srcDir.toString())); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
57 |
javacArgs.addAll(Arrays.asList("-classpath", clsPath)); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
58 |
Arrays.stream(tstMdls.split("\\s+")) |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
59 |
.filter(s -> s.contains("/")) |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
60 |
.map(s -> "--add-exports=" + s + "=ALL-UNNAMED") |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
61 |
.collect(Collectors.toCollection(() -> javacArgs)); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
62 |
javacArgs.add(src.toString()); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
63 |
System.out.println("javac: " + javacArgs); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
64 |
int rc = com.sun.tools.javac.Main.compile( |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
65 |
javacArgs.toArray(new String[javacArgs.size()])); |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
66 |
if (rc != 0) |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
67 |
throw new Exception("compilation failed"); |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
68 |
} |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
69 |
|
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
70 |
Class<?> sjavac = Class.forName(testClassName); |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
71 |
Method main = sjavac.getMethod("main", String[].class); |
25603
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
72 |
main.invoke(null, new Object[] { testArgs }); |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
73 |
} |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
74 |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
75 |
private static boolean isNewer(Path a, Path b) throws IOException { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
76 |
if (Files.notExists(b)) |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
77 |
return true; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
78 |
return Files.getLastModifiedTime(a).compareTo(Files.getLastModifiedTime(b)) > 0; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
79 |
} |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
32337
diff
changeset
|
80 |
|
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
81 |
private static boolean isSJavacOnClassPath() { |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
82 |
String cls = "com/sun/tools/sjavac/Main.class"; |
25603
d5fa4eab2d26
8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents:
24067
diff
changeset
|
83 |
return Wrapper.class.getClassLoader().getResource(cls) != null; |
24067
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
84 |
} |
76e7b6bbbd85
8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff
changeset
|
85 |
} |