8080353: JShell: Better error message on attempting to add default method

Special handling for errors with "default" modifier

Reviewed-by: jlahoda
This commit is contained in:
Robert Field 2019-06-01 13:41:01 -07:00
parent 1813ce706a
commit 50971d6ea7
4 changed files with 52 additions and 32 deletions

View File

@ -98,6 +98,7 @@ import static jdk.jshell.Snippet.SubKind.STATIC_IMPORT_ON_DEMAND_SUBKIND;
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 @@ class Eval {
}
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 @@ class Eval {
};
}
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 @@ class Eval {
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 @@ class Eval {
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) {

View File

@ -37,7 +37,7 @@ jdk/javadoc/doclet/testIOException/TestIOException.java
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
###########################################################################
#

View File

@ -332,7 +332,8 @@ public class ExceptionsTest extends KullaTesting {
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);
}
}
}

View File

@ -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 @@ public class MethodsTest extends KullaTesting {
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);