1 /* |
|
2 * Copyright (c) 2016, 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 import java.io.BufferedWriter; |
|
25 import java.io.IOException; |
|
26 import java.nio.file.Files; |
|
27 import java.nio.file.Path; |
|
28 import java.util.Arrays; |
|
29 import java.util.regex.Matcher; |
|
30 import java.util.regex.Pattern; |
|
31 import java.util.stream.Stream; |
|
32 |
|
33 import static org.testng.Assert.assertTrue; |
|
34 |
|
35 /** |
|
36 * Utility class for creating test modules. |
|
37 */ |
|
38 public class ModuleSourceBuilder { |
|
39 private static String MODULE_INFO_JAVA = "module-info.java"; |
|
40 private static Pattern MODULE_PATTERN = |
|
41 Pattern.compile("module\\s+((?:\\w+\\.)*)"); |
|
42 private static Pattern PACKAGE_PATTERN = |
|
43 Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))"); |
|
44 private static Pattern CLASS_PATTERN = |
|
45 Pattern.compile("(?:public\\s+)?(?:class|enum|interface)\\s+(\\w+)"); |
|
46 |
|
47 private final Path dir; |
|
48 public ModuleSourceBuilder(Path dir) { |
|
49 this.dir = dir; |
|
50 } |
|
51 |
|
52 /** |
|
53 * Create java source files of the given module |
|
54 */ |
|
55 public void writeJavaFiles(String module, String moduleInfoJava, String... contents) |
|
56 throws IOException |
|
57 { |
|
58 Path msrc = dir.resolve(module); |
|
59 new JavaSource(moduleInfoJava).write(msrc); |
|
60 for (String c : contents) { |
|
61 new JavaSource(c).write(msrc); |
|
62 } |
|
63 } |
|
64 |
|
65 /** |
|
66 * Compile the module to the given destination. |
|
67 */ |
|
68 public void compile(String module, Path dest, String... options) |
|
69 throws IOException |
|
70 { |
|
71 Path msrc = dir.resolve(module); |
|
72 Stream<String> args = |
|
73 Stream.concat(Arrays.stream(options), |
|
74 Stream.of("--module-source-path", |
|
75 dir.toString())); |
|
76 assertTrue(CompilerUtils.compile(msrc, dest, args.toArray(String[]::new)), |
|
77 "Fail to compile " + module); |
|
78 } |
|
79 |
|
80 static class JavaSource { |
|
81 final String source; |
|
82 JavaSource(String source) { |
|
83 this.source = source; |
|
84 } |
|
85 |
|
86 /** |
|
87 * Writes the source code to a file in a specified directory. |
|
88 * @param dir the directory |
|
89 * @throws IOException if there is a problem writing the file |
|
90 */ |
|
91 public void write(Path dir) throws IOException { |
|
92 Path file = dir.resolve(getJavaFileNameFromSource(source)); |
|
93 Files.createDirectories(file.getParent()); |
|
94 try (BufferedWriter out = Files.newBufferedWriter(file)) { |
|
95 out.write(source.replace("\n", System.lineSeparator())); |
|
96 } |
|
97 } |
|
98 |
|
99 /** |
|
100 * Extracts the Java file name from the class declaration. |
|
101 * This method is intended for simple files and uses regular expressions, |
|
102 * so comments matching the pattern can make the method fail. |
|
103 */ |
|
104 static String getJavaFileNameFromSource(String source) { |
|
105 String packageName = null; |
|
106 |
|
107 Matcher matcher = MODULE_PATTERN.matcher(source); |
|
108 if (matcher.find()) |
|
109 return MODULE_INFO_JAVA; |
|
110 |
|
111 matcher = PACKAGE_PATTERN.matcher(source); |
|
112 if (matcher.find()) |
|
113 packageName = matcher.group(1).replace(".", "/"); |
|
114 |
|
115 matcher = CLASS_PATTERN.matcher(source); |
|
116 if (matcher.find()) { |
|
117 String className = matcher.group(1) + ".java"; |
|
118 return (packageName == null) ? className : packageName + "/" + className; |
|
119 } else if (packageName != null) { |
|
120 return packageName + "/package-info.java"; |
|
121 } else { |
|
122 throw new Error("Could not extract the java class " + |
|
123 "name from the provided source"); |
|
124 } |
|
125 } |
|
126 } |
|
127 } |
|