Mon, 19 Nov 2012 11:38:49 -0800
8001098: Provide a simple light-weight "plug-in" mechanism for javac
Reviewed-by: mcimadamore
jjg@1416 | 1 | |
jjg@1416 | 2 | import java.io.File; |
jjg@1416 | 3 | import java.io.FileWriter; |
jjg@1416 | 4 | import java.io.IOException; |
jjg@1416 | 5 | import java.io.PrintWriter; |
jjg@1416 | 6 | import java.io.StringWriter; |
jjg@1416 | 7 | import java.nio.charset.Charset; |
jjg@1416 | 8 | import java.nio.file.Files; |
jjg@1416 | 9 | import java.util.Arrays; |
jjg@1416 | 10 | import java.util.List; |
jjg@1416 | 11 | import java.util.Locale; |
jjg@1416 | 12 | import java.util.Objects; |
jjg@1416 | 13 | import javax.tools.JavaCompiler; |
jjg@1416 | 14 | import javax.tools.JavaFileManager; |
jjg@1416 | 15 | import javax.tools.JavaFileObject; |
jjg@1416 | 16 | import javax.tools.StandardJavaFileManager; |
jjg@1416 | 17 | import javax.tools.StandardLocation; |
jjg@1416 | 18 | import javax.tools.ToolProvider; |
jjg@1416 | 19 | |
jjg@1416 | 20 | /* |
jjg@1416 | 21 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
jjg@1416 | 22 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1416 | 23 | * |
jjg@1416 | 24 | * This code is free software; you can redistribute it and/or modify it |
jjg@1416 | 25 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1416 | 26 | * published by the Free Software Foundation. |
jjg@1416 | 27 | * |
jjg@1416 | 28 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1416 | 29 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1416 | 30 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1416 | 31 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1416 | 32 | * accompanied this code). |
jjg@1416 | 33 | * |
jjg@1416 | 34 | * You should have received a copy of the GNU General Public License version |
jjg@1416 | 35 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1416 | 36 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1416 | 37 | * |
jjg@1416 | 38 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1416 | 39 | * or visit www.oracle.com if you need additional information or have any |
jjg@1416 | 40 | * questions. |
jjg@1416 | 41 | */ |
jjg@1416 | 42 | |
jjg@1416 | 43 | /** |
jjg@1416 | 44 | * @test |
jjg@1416 | 45 | * @bug 8001098 |
jjg@1416 | 46 | * @summary Provide a simple light-weight "plug-in" mechanism for javac |
jjg@1416 | 47 | */ |
jjg@1416 | 48 | |
jjg@1416 | 49 | public class Test { |
jjg@1416 | 50 | public static void main(String... args) throws Exception { |
jjg@1416 | 51 | new Test().run(); |
jjg@1416 | 52 | } |
jjg@1416 | 53 | |
jjg@1416 | 54 | final File testSrc; |
jjg@1416 | 55 | final File pluginSrc; |
jjg@1416 | 56 | final File pluginClasses ; |
jjg@1416 | 57 | final File pluginJar; |
jjg@1416 | 58 | final List<String> ref1; |
jjg@1416 | 59 | final List<String> ref2; |
jjg@1416 | 60 | final JavaCompiler compiler; |
jjg@1416 | 61 | final StandardJavaFileManager fm; |
jjg@1416 | 62 | |
jjg@1416 | 63 | Test() throws Exception { |
jjg@1416 | 64 | testSrc = new File(System.getProperty("test.src")); |
jjg@1416 | 65 | pluginSrc = new File(testSrc, "ShowTypePlugin.java"); |
jjg@1416 | 66 | pluginClasses = new File("plugin"); |
jjg@1416 | 67 | pluginJar = new File("plugin.jar"); |
jjg@1416 | 68 | ref1 = readFile(testSrc, "Identifiers.out"); |
jjg@1416 | 69 | ref2 = readFile(testSrc, "Identifiers_PI.out"); |
jjg@1416 | 70 | compiler = ToolProvider.getSystemJavaCompiler(); |
jjg@1416 | 71 | fm = compiler.getStandardFileManager(null, null, null); |
jjg@1416 | 72 | } |
jjg@1416 | 73 | |
jjg@1416 | 74 | void run() throws Exception { |
jjg@1416 | 75 | // compile the plugin explicitly, to a non-standard directory |
jjg@1416 | 76 | // so that we don't find it on the wrong path by accident |
jjg@1416 | 77 | pluginClasses.mkdirs(); |
jjg@1416 | 78 | compile("-d", pluginClasses.getPath(), pluginSrc.getPath()); |
jjg@1416 | 79 | writeFile(new File(pluginClasses, "META-INF/services/com.sun.source.util.Plugin"), |
jjg@1416 | 80 | "ShowTypePlugin\n"); |
jjg@1416 | 81 | jar("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), "."); |
jjg@1416 | 82 | |
jjg@1416 | 83 | testCommandLine("-Xplugin:showtype", ref1); |
jjg@1416 | 84 | testCommandLine("-Xplugin:showtype PI", ref2); |
jjg@1416 | 85 | testAPI("-Xplugin:showtype", ref1); |
jjg@1416 | 86 | testAPI("-Xplugin:showtype PI", ref2); |
jjg@1416 | 87 | |
jjg@1416 | 88 | if (errors > 0) |
jjg@1416 | 89 | throw new Exception(errors + " errors occurred"); |
jjg@1416 | 90 | } |
jjg@1416 | 91 | |
jjg@1416 | 92 | void testAPI(String opt, List<String> ref) throws Exception { |
jjg@1416 | 93 | File identifiers = new File(testSrc, "Identifiers.java"); |
jjg@1416 | 94 | fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH, Arrays.asList(pluginJar)); |
jjg@1416 | 95 | fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("."))); |
jjg@1416 | 96 | List<String> options = Arrays.asList(opt); |
jjg@1416 | 97 | Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers); |
jjg@1416 | 98 | |
jjg@1416 | 99 | System.err.println("test api: " + options + " " + files); |
jjg@1416 | 100 | |
jjg@1416 | 101 | StringWriter sw = new StringWriter(); |
jjg@1416 | 102 | PrintWriter pw = new PrintWriter(sw); |
jjg@1416 | 103 | boolean ok = compiler.getTask(pw, fm, null, options, null, files).call(); |
jjg@1416 | 104 | String out = sw.toString(); |
jjg@1416 | 105 | System.err.println(out); |
jjg@1416 | 106 | if (!ok) |
jjg@1416 | 107 | error("testCommandLine: compilation failed"); |
jjg@1416 | 108 | checkOutput(out, ref); |
jjg@1416 | 109 | } |
jjg@1416 | 110 | |
jjg@1416 | 111 | void testCommandLine(String opt, List<String> ref) { |
jjg@1416 | 112 | File identifiers = new File(testSrc, "Identifiers.java"); |
jjg@1416 | 113 | String[] args = { |
jjg@1416 | 114 | "-d", ".", |
jjg@1416 | 115 | "-processorpath", pluginJar.getPath(), |
jjg@1416 | 116 | opt, |
jjg@1416 | 117 | identifiers.getPath() }; |
jjg@1416 | 118 | |
jjg@1416 | 119 | System.err.println("test command line: " + Arrays.asList(args)); |
jjg@1416 | 120 | |
jjg@1416 | 121 | StringWriter sw = new StringWriter(); |
jjg@1416 | 122 | PrintWriter pw = new PrintWriter(sw); |
jjg@1416 | 123 | int rc = com.sun.tools.javac.Main.compile(args, pw); |
jjg@1416 | 124 | String out = sw.toString(); |
jjg@1416 | 125 | System.err.println(out); |
jjg@1416 | 126 | if (rc != 0) |
jjg@1416 | 127 | error("testCommandLine: compilation failed"); |
jjg@1416 | 128 | checkOutput(out, ref); |
jjg@1416 | 129 | } |
jjg@1416 | 130 | |
jjg@1416 | 131 | private void checkOutput(String out, List<String> ref) { |
jjg@1416 | 132 | List<String> lines = Arrays.asList(out |
jjg@1416 | 133 | .replaceAll(".*?([A-Za-z.]+:[0-9]+: .*)", "$1") // remove file directory |
jjg@1416 | 134 | .split("[\r\n]+")); // allow for newline formats |
jjg@1416 | 135 | if (!lines.equals(ref)) { |
jjg@1416 | 136 | error("unexpected output"); |
jjg@1416 | 137 | } |
jjg@1416 | 138 | } |
jjg@1416 | 139 | |
jjg@1416 | 140 | private void compile(String... args) throws Exception { |
jjg@1416 | 141 | System.err.println("compile: " + Arrays.asList(args)); |
jjg@1416 | 142 | int rc = com.sun.tools.javac.Main.compile(args); |
jjg@1416 | 143 | if (rc != 0) |
jjg@1416 | 144 | throw new Exception("compiled failed, rc=" + rc); |
jjg@1416 | 145 | } |
jjg@1416 | 146 | |
jjg@1416 | 147 | private void jar(String... args) throws Exception { |
jjg@1416 | 148 | System.err.println("jar: " + Arrays.asList(args)); |
jjg@1416 | 149 | boolean ok = new sun.tools.jar.Main(System.out, System.err, "jar").run(args); |
jjg@1416 | 150 | if (!ok) |
jjg@1416 | 151 | throw new Exception("jar failed"); |
jjg@1416 | 152 | } |
jjg@1416 | 153 | |
jjg@1416 | 154 | private List<String> readFile(File dir, String name) throws IOException { |
jjg@1416 | 155 | return Files.readAllLines(new File(dir, name).toPath(), Charset.defaultCharset()); |
jjg@1416 | 156 | } |
jjg@1416 | 157 | |
jjg@1416 | 158 | private void writeFile(File f, String body) throws IOException { |
jjg@1416 | 159 | f.getParentFile().mkdirs(); |
jjg@1416 | 160 | try (FileWriter out = new FileWriter(f)) { |
jjg@1416 | 161 | out.write(body); |
jjg@1416 | 162 | } |
jjg@1416 | 163 | } |
jjg@1416 | 164 | |
jjg@1416 | 165 | private void error(String msg) { |
jjg@1416 | 166 | System.err.println(msg); |
jjg@1416 | 167 | errors++; |
jjg@1416 | 168 | } |
jjg@1416 | 169 | |
jjg@1416 | 170 | int errors; |
jjg@1416 | 171 | } |