8172668: NPE in jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.importNamed(
authorjlahoda
Thu, 12 Jan 2017 15:37:46 +0100
changeset 43141 7ea6578a9110
parent 43140 1e40241dc2d4
child 43142 e72c3d9b0a0b
8172668: NPE in jdk.compiler/com.sun.tools.javac.comp.TypeEnter$ImportsPhase.importNamed( Summary: Guarding against noType returned from ErrorType.getOriginalType. Reviewed-by: mcimadamore
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java
langtools/test/tools/javac/modules/ConvenientAccessErrorsTest.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Wed Jan 11 11:09:04 2017 -0800
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Jan 12 15:37:46 2017 +0100
@@ -410,7 +410,9 @@
                     importNamedStatic(tree, p, name, localEnv);
                     chk.checkCanonical(imp.selected);
                 } else {
-                    TypeSymbol c = attribImportType(imp, localEnv).getOriginalType().tsym;
+                    Type importedType = attribImportType(imp, localEnv);
+                    Type originalType = importedType.getOriginalType();
+                    TypeSymbol c = originalType.hasTag(CLASS) ? originalType.tsym : importedType.tsym;
                     chk.checkCanonical(imp);
                     importNamed(tree.pos(), c, env, tree);
                 }
--- a/langtools/test/tools/javac/modules/ConvenientAccessErrorsTest.java	Wed Jan 11 11:09:04 2017 -0800
+++ b/langtools/test/tools/javac/modules/ConvenientAccessErrorsTest.java	Thu Jan 12 15:37:46 2017 +0100
@@ -23,6 +23,7 @@
 
 /*
  * @test
+ * @bug 8169197 8172668
  * @summary Check convenient errors are produced for inaccessible classes.
  * @library /tools/lib
  * @modules jdk.compiler/com.sun.tools.javac.api
@@ -512,4 +513,84 @@
                 .writeAll();
     }
 
+    @Test
+    public void testUnresolvableInImport(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path src_m1 = src.resolve("m1x");
+        tb.writeJavaFiles(src_m1,
+                          "module m1x { }",
+                          "package api; import can.not.resolve; public class Api { }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        List<String> log = new JavacTask(tb)
+                .options("-XDrawDiagnostics",
+                         "--module-source-path", src.toString())
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList(
+                "Api.java:1:28: compiler.err.doesnt.exist: can.not",
+                "1 error");
+
+        if (!expected.equals(log))
+            throw new Exception("expected output not found; actual: " + log);
+    }
+
+    @Test
+    public void testMissingKnownClass(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path src_m1 = src.resolve("m1x");
+        tb.writeJavaFiles(src_m1,
+                          "module m1x { exports api; }",
+                          "package api; public class Base { }",
+                          "package api; public class Sub extends Base { }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+        Path m1xClasses = classes.resolve("m1x");
+        tb.createDirectories(m1xClasses);
+
+        new JavacTask(tb)
+                .options("-XDrawDiagnostics")
+                .outdir(m1xClasses)
+                .files(findJavaFiles(src_m1))
+                .run(Task.Expect.SUCCESS)
+                .writeAll();
+
+        Files.delete(m1xClasses.resolve("api").resolve("Base.class"));
+
+        Path src_m2 = src.resolve("m2x");
+        tb.writeJavaFiles(src_m2,
+                          "module m2x { requires m1x; }",
+                          "package test;\n" +
+                          "import api.Sub;\n" +
+                          "import api.Base;\n" +
+                          "public class Test {\n" +
+                          "    Sub a2;\n" +
+                          "    Base a;\n" +
+                          "}\n");
+        Path m2xClasses = classes.resolve("m2x");
+        tb.createDirectories(m2xClasses);
+        List<String> log = new JavacTask(tb)
+                .options("-XDrawDiagnostics",
+                         "--module-path", classes.toString(),
+                         "-XDdev")
+                .outdir(m2xClasses)
+                .files(findJavaFiles(src_m2))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList(
+                "Test.java:3:11: compiler.err.cant.resolve.location: kindname.class, Base, , , (compiler.misc.location: kindname.package, api, null)",
+                "Test.java:6:5: compiler.err.cant.resolve.location: kindname.class, Base, , , (compiler.misc.location: kindname.class, test.Test, null)",
+                "2 errors");
+
+        if (!expected.equals(log))
+            throw new Exception("expected output not found; actual: " + log);
+    }
+
 }