8080353: JShell: Better error message on attempting to add default method
Summary: Special handling for errors with "default" modifier
Reviewed-by: jlahoda
--- a/src/jdk.jshell/share/classes/jdk/jshell/Eval.java Sat Jun 01 03:18:23 2019 +0200
+++ b/src/jdk.jshell/share/classes/jdk/jshell/Eval.java Sat Jun 01 13:41:01 2019 -0700
@@ -98,6 +98,7 @@
class Eval {
private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\p{javaWhitespace}+(?<static>static\\p{javaWhitespace}+)?(?<fullname>[\\p{L}\\p{N}_\\$\\.]+\\.(?<name>[\\p{L}\\p{N}_\\$]+|\\*))");
+ private static final Pattern DEFAULT_PREFIX = Pattern.compile("\\p{javaWhitespace}*(default)\\p{javaWhitespace}+");
// for uses that should not change state -- non-evaluations
private boolean preserveState = false;
@@ -201,7 +202,13 @@
}
Tree unitTree = units.get(0);
if (pt.getDiagnostics().hasOtherThanNotStatementErrors()) {
- return compileFailResult(pt, userSource, kindOfTree(unitTree));
+ Matcher matcher = DEFAULT_PREFIX.matcher(compileSource);
+ DiagList dlist = matcher.lookingAt()
+ ? new DiagList(new ModifierDiagnostic(true,
+ state.messageFormat("jshell.diag.modifier.single.fatal", "'default'"),
+ matcher.start(1), matcher.end(1)))
+ : pt.getDiagnostics();
+ return compileFailResult(dlist, userSource, kindOfTree(unitTree));
}
// Erase illegal/ignored modifiers
@@ -1154,34 +1161,21 @@
};
}
- private DiagList modifierDiagnostics(ModifiersTree modtree,
- final TreeDissector dis, boolean isAbstractProhibited) {
-
- class ModifierDiagnostic extends Diag {
+ private class ModifierDiagnostic extends Diag {
final boolean fatal;
final String message;
- long start;
- long end;
+ final long start;
+ final long end;
- ModifierDiagnostic(List<Modifier> list, boolean fatal) {
+ ModifierDiagnostic(boolean fatal,
+ final String message,
+ long start,
+ long end) {
this.fatal = fatal;
- StringBuilder sb = new StringBuilder();
- for (Modifier mod : list) {
- sb.append("'");
- sb.append(mod.toString());
- sb.append("' ");
- }
- String key = (list.size() > 1)
- ? fatal
- ? "jshell.diag.modifier.plural.fatal"
- : "jshell.diag.modifier.plural.ignore"
- : fatal
- ? "jshell.diag.modifier.single.fatal"
- : "jshell.diag.modifier.single.ignore";
- this.message = state.messageFormat(key, sb.toString());
- start = dis.getStartPosition(modtree);
- end = dis.getEndPosition(modtree);
+ this.message = message;
+ this.start = start;
+ this.end = end;
}
@Override
@@ -1215,7 +1209,10 @@
public String getMessage(Locale locale) {
return message;
}
- }
+ }
+
+ private DiagList modifierDiagnostics(ModifiersTree modtree,
+ final TreeDissector dis, boolean isAbstractProhibited) {
List<Modifier> list = new ArrayList<>();
boolean fatal = false;
@@ -1243,9 +1240,26 @@
break;
}
}
- return list.isEmpty()
- ? new DiagList()
- : new DiagList(new ModifierDiagnostic(list, fatal));
+ if (list.isEmpty()) {
+ return new DiagList();
+ } else {
+ StringBuilder sb = new StringBuilder();
+ for (Modifier mod : list) {
+ sb.append("'");
+ sb.append(mod.toString());
+ sb.append("' ");
+ }
+ String key = (list.size() > 1)
+ ? fatal
+ ? "jshell.diag.modifier.plural.fatal"
+ : "jshell.diag.modifier.plural.ignore"
+ : fatal
+ ? "jshell.diag.modifier.single.fatal"
+ : "jshell.diag.modifier.single.ignore";
+ String message = state.messageFormat(key, sb.toString().trim());
+ return new DiagList(new ModifierDiagnostic(fatal, message,
+ dis.getStartPosition(modtree), dis.getEndPosition(modtree)));
+ }
}
String computeDeclareName(TypeSymbol ts) {
--- a/test/langtools/ProblemList.txt Sat Jun 01 03:18:23 2019 +0200
+++ b/test/langtools/ProblemList.txt Sat Jun 01 13:41:01 2019 -0700
@@ -37,7 +37,7 @@
jdk/jshell/UserJdiUserRemoteTest.java 8173079 linux-all
jdk/jshell/UserInputTest.java 8169536 generic-all
-jdk/jshell/ExceptionsTest.java 8200701 windows-all
+## jdk/jshell/ExceptionsTest.java 8200701 windows-all
###########################################################################
#
--- a/test/langtools/jdk/jshell/ExceptionsTest.java Sat Jun 01 03:18:23 2019 +0200
+++ b/test/langtools/jdk/jshell/ExceptionsTest.java Sat Jun 01 13:41:01 2019 -0700
@@ -332,7 +332,8 @@
assertEquals(actualElement.getMethodName(), expectedElement.getMethodName(), message + " : method names");
}
assertEquals(actualElement.getFileName(), expectedElement.getFileName(), message + " : file names");
- assertEquals(actualElement.getLineNumber(), expectedElement.getLineNumber(), message + " : line numbers");
+ assertEquals(actualElement.getLineNumber(), expectedElement.getLineNumber(), message + " : line numbers"
+ + " -- actual: " + actual + ", expected: " + expected);
}
}
}
--- a/test/langtools/jdk/jshell/MethodsTest.java Sat Jun 01 03:18:23 2019 +0200
+++ b/test/langtools/jdk/jshell/MethodsTest.java Sat Jun 01 13:41:01 2019 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 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
@@ -23,7 +23,7 @@
/*
* @test
- * @bug 8080357 8167643 8187359 8199762
+ * @bug 8080357 8167643 8187359 8199762 8080353
* @summary Tests for EvaluationState.methods
* @build KullaTesting TestingInputStream ExpectedDiagnostic
* @run testng MethodsTest
@@ -237,6 +237,11 @@
assertNumberOfActiveMethods(0);
assertActiveKeys();
+ assertDeclareFail("default void f() { }",
+ new ExpectedDiagnostic("jdk.eval.error.illegal.modifiers", 0, 7, 0, -1, -1, Diagnostic.Kind.ERROR));
+ assertNumberOfActiveMethods(0);
+ assertActiveKeys();
+
assertDeclareFail("int f() {}", "compiler.err.missing.ret.stmt",
added(REJECTED));
assertNumberOfActiveMethods(0);