8209963: source file mode for JVM should provide a hook to locate the source file
authorjjg
Wed, 26 Sep 2018 16:23:31 -0700
changeset 51892 904c4e07cc22
parent 51891 9d9ab30af97d
child 51893 c2b02e3af8ed
8209963: source file mode for JVM should provide a hook to locate the source file Reviewed-by: darcy
src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java
test/langtools/tools/javac/launcher/SourceLauncherTest.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java	Wed Sep 26 15:14:17 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/Main.java	Wed Sep 26 16:23:31 2018 -0700
@@ -399,6 +399,7 @@
      */
     private void execute(String mainClassName, String[] appArgs, Context context)
             throws Fault, InvocationTargetException {
+        System.setProperty("jdk.launcher.sourcefile", context.file.toString());
         ClassLoader cl = context.getClassLoader(ClassLoader.getSystemClassLoader());
         try {
             Class<?> appClass = Class.forName(mainClassName, true, cl);
--- a/test/langtools/tools/javac/launcher/SourceLauncherTest.java	Wed Sep 26 15:14:17 2018 -0700
+++ b/test/langtools/tools/javac/launcher/SourceLauncherTest.java	Wed Sep 26 16:23:31 2018 -0700
@@ -250,6 +250,22 @@
                     "access denied (\"java.util.PropertyPermission\" \"user.dir\" \"write\")");
     }
 
+    public void testSystemProperty(Path base) throws IOException {
+        tb.writeJavaFiles(base,
+            "class ShowProperty {\n" +
+            "    public static void main(String... args) {\n" +
+            "        System.out.println(System.getProperty(\"jdk.launcher.sourcefile\"));\n" +
+            "    }\n" +
+            "}");
+
+        Path file = base.resolve("ShowProperty.java");
+        String log = new JavaTask(tb)
+                .className(file.toString())
+                .run(Task.Expect.SUCCESS)
+                .getOutput(Task.OutputKind.STDOUT);
+        checkEqual("stdout", log.trim(), file.toAbsolutePath().toString());
+    }
+
     void testSuccess(Path file, String expect) throws IOException {
         Result r = run(file, Collections.emptyList(), List.of("1", "2", "3"));
         checkEqual("stdout", r.stdOut, expect);