8235597: Extend support for -XDaccessInternalAPI to plugin modules

Reviewed-by: mcimadamore, jlahoda
This commit is contained in:
Jonathan Gibbons 2019-12-11 16:32:49 -08:00
parent 9fc688875d
commit d8d4cd6508
3 changed files with 219 additions and 7 deletions

View File

@ -61,6 +61,8 @@ import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.ModuleHelper;
import com.sun.tools.javac.util.Options;
import com.sun.tools.javac.util.PropagatedException;
/**
@ -73,6 +75,7 @@ import com.sun.tools.javac.util.PropagatedException;
*/
public class BasicJavacTask extends JavacTask {
protected Context context;
protected Options options;
private TaskListener taskListener;
public static JavacTask instance(Context context) {
@ -84,6 +87,7 @@ public class BasicJavacTask extends JavacTask {
public BasicJavacTask(Context c, boolean register) {
context = c;
options = Options.instance(c);
if (register)
context.put(JavacTask.class, this);
}
@ -198,10 +202,10 @@ public class BasicJavacTask extends JavacTask {
for (PluginInfo<Plugin> pluginDesc : platformProvider.getPlugins()) {
java.util.List<String> options =
pluginDesc.getOptions().entrySet().stream()
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.toList());
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.toList());
try {
pluginDesc.getPlugin().init(this, options.toArray(new String[options.size()]));
initPlugin(pluginDesc.getPlugin(), options.toArray(new String[options.size()]));
} catch (RuntimeException ex) {
throw new PropagatedException(ex);
}
@ -221,7 +225,7 @@ public class BasicJavacTask extends JavacTask {
pluginsToCall.remove(p);
autoStart.remove(plugin);
try {
plugin.init(this, p.tail.toArray(new String[p.tail.size()]));
initPlugin(plugin, p.tail.toArray(new String[p.tail.size()]));
} catch (RuntimeException ex) {
throw new PropagatedException(ex);
}
@ -229,12 +233,12 @@ public class BasicJavacTask extends JavacTask {
}
}
}
for (List<String> p: pluginsToCall) {
for (List<String> p : pluginsToCall) {
Log.instance(context).error(Errors.PluginNotFound(p.head));
}
for (Plugin plugin : autoStart) {
try {
plugin.init(this, new String[0]);
initPlugin(plugin, new String[0]);
} catch (RuntimeException ex) {
throw new PropagatedException(ex);
}
@ -242,6 +246,14 @@ public class BasicJavacTask extends JavacTask {
}
}
private void initPlugin(Plugin p, String... args) {
Module m = p.getClass().getModule();
if (m.isNamed() && options.isSet("accessInternalAPI")) {
ModuleHelper.addExports(getClass().getModule(), m);
}
p.init(this, args);
}
public void initDocLint(List<String> docLintOpts) {
if (docLintOpts.isEmpty())
return;

View File

@ -1670,7 +1670,7 @@ public class ClassWriter extends ClassFile {
protected int writeExtraAttributes(Symbol sym) {
int i = 0;
for (ToIntFunction<Symbol> hook : extraAttributeHooks) {
i += hook.apply(sym);
i += hook.applyAsInt(sym);
}
return i;
}

View File

@ -0,0 +1,200 @@
/*
* Copyright (c) 2019, 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 8235597
* @summary Extend support for -XDaccessInternalAPI to plugin modules
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jlink
* @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask
* @run main InternalAPI
*/
import java.io.IOException;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.spi.ToolProvider;
import toolbox.ExecTask;
import toolbox.JarTask;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.TestRunner;
import toolbox.ToolBox;
public class InternalAPI extends TestRunner {
public static void main(String... args) throws Exception {
new InternalAPI().run();
}
InternalAPI() {
super(System.out);
}
ToolBox tb = new ToolBox();
Path pluginJar;
Path mclasses;
void run() throws Exception {
Path src = Path.of("src");
tb.writeJavaFiles(src,
"package p;\n"
+ "import javax.lang.model.element.*;\n"
+ "import com.sun.source.util.*;\n"
+ "import com.sun.tools.javac.code.Symbol.ClassSymbol;\n"
+ "public class C implements Plugin, TaskListener {\n"
+ " public String getName() { return \"TestPlugin\"; }\n"
+ " public boolean autoStart() { return true; }\n"
+ " public void init(JavacTask task, String... args) {\n"
+ " System.out.println(\"C.init \" + java.util.Arrays.toString(args));\n"
+ " task.addTaskListener(this);\n"
+ " }\n"
+ " public void started(TaskEvent e) {\n"
+ " TypeElement te = e.getTypeElement();\n"
+ " if (te != null) {\n"
+ " ClassSymbol sym = (ClassSymbol) te;\n"
+ " System.out.println(e.getKind() + \" \" + te + \" \" + sym.kind);\n"
+ " }\n"
+ " }\n"
+ "}\n");
Path msrc = Path.of("msrc");
tb.writeJavaFiles(msrc,
"module m {\n"
+ " requires jdk.compiler;\n"
+ " provides com.sun.source.util.Plugin with p.C;\n"
+ "}\n");
Path classes = Files.createDirectories(Path.of("classes"));
new JavacTask(tb)
.outdir(classes)
.options("--add-exports", "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED")
.files(tb.findJavaFiles(src))
.run()
.writeAll();
tb.writeFile(classes.resolve("META-INF").resolve("services").resolve("com.sun.source.util.Plugin"),
"p.C\n");
pluginJar = Path.of("plugin.jar");
new JarTask(tb, pluginJar)
.baseDir(classes)
.files(".")
.run();
mclasses = Files.createDirectories(Path.of("mclasses"));
new JavacTask(tb)
.outdir(mclasses)
.sourcepath(msrc, src)
.options("--add-exports", "jdk.compiler/com.sun.tools.javac.code=m")
.files(tb.findJavaFiles(msrc))
.run()
.writeAll();
Path hw = Path.of("hw");
tb.writeJavaFiles(hw,
"public class HelloWorld {\n"
+ " public static void main(String... args) {\n"
+ " System.out.println(\"Hello World!\");\n"
+ " }\n"
+ "}\n");
runTests(m -> new Object[] { Path.of(m.getName()) });
}
// -XDaccessInternalAPI has always been supported for classpath plugins
@Test
public void testClassPath(Path base) throws Exception {
List<String> stdout = new JavacTask(tb)
.classpath(pluginJar)
.outdir(Files.createDirectories(base.resolve("out")))
.files(tb.findJavaFiles(Path.of("hw")))
.run()
.writeAll()
.getOutputLines(Task.OutputKind.STDOUT);
tb.checkEqual(stdout, List.of("C.init []", "ANALYZE HelloWorld TYP", "GENERATE HelloWorld TYP"));
}
// negative control test: access fails without the new option
@Test
public void testModuleControl(Path base) throws Exception {
try {
new JavacTask(tb)
.options("--processor-module-path", mclasses.toString())
.outdir(Files.createDirectories(base.resolve("out")))
.files(tb.findJavaFiles(Path.of("hw")))
.run()
.writeAll();
throw new Exception("expected exception not thrown");
} catch (Exception e) {
System.out.println("*** " + e);
String msg = e.getMessage();
if (!msg.contains("java.lang.IllegalAccessError: class p.C (in module m) "
+ "cannot access class com.sun.tools.javac.code.Symbol$ClassSymbol (in module jdk.compiler)")) {
throw new Exception("expected exception not found");
}
}
}
// positive test: access succeeds with option for modules on modulepath
@Test
public void testModulePath(Path base) throws IOException {
List<String> stdout = new JavacTask(tb)
.options("--processor-module-path", mclasses.toString(),
"-XDaccessInternalAPI")
.outdir(Files.createDirectories(base.resolve("out")))
.files(tb.findJavaFiles(Path.of("hw")))
.run()
.writeAll()
.getOutputLines(Task.OutputKind.STDOUT);
tb.checkEqual(stdout, List.of("C.init []", "ANALYZE HelloWorld TYP", "GENERATE HelloWorld TYP"));
}
// positive test: access succeeds with option for modules in image
@Test
public void testImage(Path base) throws Exception {
Path tmpJDK = base.resolve("tmpJDK");
ToolProvider jlink = ToolProvider.findFirst("jlink")
.orElseThrow(() -> new Exception("cannot find jlink"));
jlink.run(System.out, System.err,
"--module-path", mclasses.toString(),
"--add-modules", "jdk.compiler,jdk.zipfs,m",
"--output", tmpJDK.toString());
String suffix = tb.isWindows() ? ".exe" : "";
List<String> stdout = new ExecTask(tb, tmpJDK.resolve("bin").resolve("javac" + suffix))
.args("-d", Files.createDirectories(base.resolve("out")).toString(),
"-XDaccessInternalAPI",
Path.of("hw").resolve("HelloWorld.java").toString())
.run()
.writeAll()
.getOutputLines(Task.OutputKind.STDOUT);
tb.checkEqual(stdout, List.of("C.init []", "ANALYZE HelloWorld TYP", "GENERATE HelloWorld TYP"));
}
}