1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8198552
* @summary Check that multiple plugins can be specified when starting javac
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.javap
* @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask
* @run main MultiplePlugins
*/
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import toolbox.JarTask;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.ToolBox;
public class MultiplePlugins {
public static void main(String... args) throws Exception {
new MultiplePlugins().run();
}
final File pluginClasses;
final File pluginModuleClasses;
final File pluginJar;
final JavaCompiler compiler;
final ToolBox tb = new ToolBox();
MultiplePlugins() throws Exception {
pluginClasses = new File("plugin");
tb.createDirectories(pluginClasses.toPath());
pluginModuleClasses = new File("plugin-modules");
pluginJar = new File("plugin.jar");
compiler = ToolProvider.getSystemJavaCompiler();
}
void run() throws Exception {
// compile the plugins:
new JavacTask(tb)
.options("-d", pluginClasses.getPath())
.sources(PLUGIN1, PLUGIN2)
.run();
File plugin = new File(pluginClasses.getPath(), "META-INF/services/com.sun.source.util.Plugin");
tb.writeFile(plugin.getPath(), "p1.Plugin1\np2.Plugin2\n");
new JarTask(tb)
.run("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
testCommandLine(EXPECTED, "--processor-path", pluginJar.toString(), "-Xplugin:plugin1", "-Xplugin:plugin2");
testAPI(EXPECTED, "--processor-path", pluginJar.toString(), "-Xplugin:plugin1", "-Xplugin:plugin2");
// compile the plugins as modules:
File m1 = new File(pluginModuleClasses, "m1");
tb.createDirectories(m1.toPath());
new JavacTask(tb)
.options("-d", m1.getPath())
.sources(MODULE1, PLUGIN1)
.run();
File m2 = new File(pluginModuleClasses, "m2");
tb.createDirectories(m2.toPath());
new JavacTask(tb)
.options("-d", m2.getPath())
.sources(MODULE2, PLUGIN2)
.run();
testCommandLine(EXPECTED, "--processor-module-path", pluginModuleClasses.toString(), "-Xplugin:plugin1", "-Xplugin:plugin2");
testAPI(EXPECTED, "--processor-module-path", pluginModuleClasses.toString(), "-Xplugin:plugin1", "-Xplugin:plugin2");
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
void testAPI(List<String> ref, String... opts) throws Exception {
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
System.err.println("test api: " + List.of(opts));
Task.Result result = new JavacTask(tb, Task.Mode.API)
.fileManager(fm)
.options(opts)
.sources(TEST)
.run(Task.Expect.SUCCESS)
.writeAll();
List<String> out = result.getOutputLines(Task.OutputKind.STDERR);
checkOutput(out, ref);
}
}
void testCommandLine(List<String> ref,String... opt) throws IOException {
Path testJavaFile = Paths.get("Test.java");
tb.writeFile(testJavaFile, TEST);
List<String> args = new ArrayList<>();
args.add("-d"); args.add(".");
args.addAll(List.of(opt));
System.err.println("test command line: " + Arrays.asList(args));
Task.Result result = new JavacTask(tb, Task.Mode.CMDLINE)
.options(args)
.files(testJavaFile)
.run(Task.Expect.SUCCESS)
.writeAll();
List<String> out = result.getOutputLines(Task.OutputKind.STDERR);
checkOutput(out, ref);
}
private void checkOutput(List<String> lines, List<String> ref) {
if (!lines.equals(ref)) {
error("unexpected output");
}
}
private void error(String msg) {
System.err.println(msg);
errors++;
}
int errors;
private static final String MODULE1 =
"module m1 {\n" +
" requires jdk.compiler;\n" +
" provides com.sun.source.util.Plugin with p1.Plugin1;\n" +
"}\n";
private static final String PLUGIN1 =
"package p1;\n" +
"import com.sun.source.util.*;\n" +
"public class Plugin1 implements Plugin {\n" +
" public String getName() {\n" +
" return \"plugin1\";\n" +
" }\n" +
" public void init(JavacTask task, String... args) {\n" +
" System.err.println(\"plugin1\");\n" +
" }\n" +
"}";
private static final String MODULE2 =
"module m2 {\n" +
" requires jdk.compiler;\n" +
" provides com.sun.source.util.Plugin with p2.Plugin2;\n" +
"}\n";
private static final String PLUGIN2 =
"package p2;\n" +
"import com.sun.source.util.*;\n" +
"public class Plugin2 implements Plugin {\n" +
" public String getName() {\n" +
" return \"plugin2\";\n" +
" }\n" +
" public void init(JavacTask task, String... args) {\n" +
" System.err.println(\"plugin2\");\n" +
" }\n" +
"}";
private static final String TEST =
"public class Test {}";
private static final List<String> EXPECTED = List.of(
"plugin1",
"plugin2"
);
}
|