From 020877da038a9bbac0928bae2d405f6d46f2fb12 Mon Sep 17 00:00:00 2001 From: Robert Field Date: Thu, 29 Nov 2018 17:45:29 -0800 Subject: [PATCH] 8213725: JShell NullPointerException due to class file with unexpected package Reviewed-by: jlahoda --- .../share/classes/jdk/jshell/Eval.java | 2 +- test/langtools/jdk/jshell/KullaTesting.java | 2 +- test/langtools/jdk/jshell/VariablesTest.java | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/jdk.jshell/share/classes/jdk/jshell/Eval.java b/src/jdk.jshell/share/classes/jdk/jshell/Eval.java index e14eccf00d..dbdb389a31 100644 --- a/src/jdk.jshell/share/classes/jdk/jshell/Eval.java +++ b/src/jdk.jshell/share/classes/jdk/jshell/Eval.java @@ -327,7 +327,7 @@ class Eval { String initCode = rinit.part(compileSource); ExpressionInfo ei = ExpressionToTypeInfo.localVariableTypeForInitializer(initCode, state, false); - if (ei != null) { + if (ei != null && ei.declareTypeName != null) { typeName = ei.declareTypeName; fullTypeName = ei.fullTypeName; displayType = ei.displayTypeName; diff --git a/test/langtools/jdk/jshell/KullaTesting.java b/test/langtools/jdk/jshell/KullaTesting.java index 9cf8a55346..802ed3f677 100644 --- a/test/langtools/jdk/jshell/KullaTesting.java +++ b/test/langtools/jdk/jshell/KullaTesting.java @@ -737,7 +737,7 @@ public class KullaTesting { expectedSubKind, unressz, othersz); String signature = var.typeName(); assertEquals(signature, expectedTypeName, - "Expected " + var.source() + " to have the name: " + + "Expected " + var.source() + " to have the type name: " + expectedTypeName + ", got: " + signature); } diff --git a/test/langtools/jdk/jshell/VariablesTest.java b/test/langtools/jdk/jshell/VariablesTest.java index 51d8c79000..695aca7ce5 100644 --- a/test/langtools/jdk/jshell/VariablesTest.java +++ b/test/langtools/jdk/jshell/VariablesTest.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8144903 8177466 8191842 8211694 + * @bug 8144903 8177466 8191842 8211694 8213725 * @summary Tests for EvaluationState.variables * @library /tools/lib * @modules jdk.compiler/com.sun.tools.javac.api @@ -492,6 +492,21 @@ public class VariablesTest extends KullaTesting { assertVarDeclRedefNoInit("String", "s", "\"hi\"", "null"); } + public void badPkgVarDecl() { + Compiler compiler = new Compiler(); + Path nopkgdirpath = Paths.get("cp", "xyz"); + compiler.compile(nopkgdirpath, + "public class TestZ { public static int V = 0; }\n"); + assertDeclareFail("import static xyz.TestZ.V;", + "compiler.err.cant.access"); + + + VarSnippet v1 = varKey(assertEval("var v = xyz.TestZ.V;", IGNORE_VALUE, null, + DiagCheck.DIAG_ERROR, DiagCheck.DIAG_OK, added(RECOVERABLE_NOT_DEFINED))); + assertVariableDeclSnippet(v1, "v", "java.lang.Object", RECOVERABLE_NOT_DEFINED, SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND, 0, 1); + assertEval("1+1", "2"); + } + private void assertVarDeclRedefNoInit(String typeName, String name, String value, String dvalue) { assertVarDeclRedefNoInit(typeName, name, value, value, dvalue); }