8196133: JShell crashes when attempting to use bad source file in class path
authorrfield
Fri, 16 Feb 2018 16:18:55 -0800
changeset 49092 6dc5e0cdb44c
parent 49091 0fa50be70f7a
child 49093 ca29679eafe5
8196133: JShell crashes when attempting to use bad source file in class path Reviewed-by: jlahoda
src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java
test/langtools/jdk/jshell/ToolBasicTest.java
--- a/src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java	Fri Feb 16 13:49:07 2018 -0800
+++ b/src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java	Fri Feb 16 16:18:55 2018 -0800
@@ -311,15 +311,18 @@
             return fm.createSourceFileObject(w, w.classFullName(), w.wrapped());
         }
 
+        /**
+         * Get the source information from the wrap.  If this is external, or
+         * otherwise does not have wrap info, just use source code.
+         * @param d the Diagnostic from the compiler
+         * @return the corresponding Diag
+         */
         @Override
         public Diag diag(Diagnostic<? extends JavaFileObject> d) {
-            SourceMemoryJavaFileObject smjfo = (SourceMemoryJavaFileObject) d.getSource();
-            if (smjfo == null) {
-                // Handle failure that doesn't preserve mapping
-                return new StringSourceHandler().diag(d);
-            }
-            OuterWrap w = (OuterWrap) smjfo.getOrigin();
-            return w.wrapDiag(d);
+            JavaFileObject jfo = d.getSource();
+            return jfo instanceof SourceMemoryJavaFileObject
+                    ? ((OuterWrap) ((SourceMemoryJavaFileObject) jfo).getOrigin()).wrapDiag(d)
+                    : new StringSourceHandler().diag(d);
         }
     }
 
--- a/test/langtools/jdk/jshell/ToolBasicTest.java	Fri Feb 16 13:49:07 2018 -0800
+++ b/test/langtools/jdk/jshell/ToolBasicTest.java	Fri Feb 16 16:18:55 2018 -0800
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508
+ * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8196133
  * @summary Tests for Basic tests for REPL tool
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -350,6 +350,34 @@
         );
     }
 
+    private String makeBadSourceJar() {
+        Compiler compiler = new Compiler();
+        Path outDir = Paths.get("testClasspathJar");
+        Path src = compiler.getPath(outDir.resolve("pkg/A.java"));
+        compiler.writeToFile(src, "package pkg; /** \u0086 */public class A { public String toString() { return \"A\"; } }");
+        String jarName = "test.jar";
+        compiler.jar(outDir, jarName, "pkg/A.java");
+        return compiler.getPath(outDir).resolve(jarName).toString();
+    }
+
+    public void testBadSourceJarClasspath() {
+        String jarPath = makeBadSourceJar();
+        test(
+                (a) -> assertCommand(a, "/env --class-path " + jarPath,
+                        "|  Setting new options and restoring state."),
+                (a) -> assertCommandOutputStartsWith(a, "new pkg.A();",
+                        "|  Error:\n"
+                        + "|  cannot find symbol\n"
+                        + "|    symbol:   class A")
+        );
+        test(new String[]{"--class-path", jarPath},
+                (a) -> assertCommandOutputStartsWith(a, "new pkg.A();",
+                        "|  Error:\n"
+                        + "|  cannot find symbol\n"
+                        + "|    symbol:   class A")
+        );
+    }
+
     public void testModulePath() {
         Compiler compiler = new Compiler();
         Path modsDir = Paths.get("mods");