8030091: Request to update error messages from javac for negative varargs test cases
Summary: Introducing a new error message for vararg parameter not being the last parameter, improving error message for unexpected character after a parameter.
Reviewed-by: jjg, sogoel
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu Jan 30 17:46:25 2014 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Sun Feb 02 12:12:01 2014 +0100
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, 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
@@ -3648,12 +3648,20 @@
params.append(lastParam);
}
this.allowThisIdent = false;
- while ((lastParam.mods.flags & Flags.VARARGS) == 0 && token.kind == COMMA) {
+ while (token.kind == COMMA) {
+ if ((lastParam.mods.flags & Flags.VARARGS) != 0) {
+ error(lastParam, "varargs.must.be.last");
+ }
nextToken();
params.append(lastParam = formalParameter(lambdaParameters));
}
}
- accept(RPAREN);
+ if (token.kind == RPAREN) {
+ nextToken();
+ } else {
+ setErrorEndPos(token.pos);
+ reportSyntaxError(S.prevToken().endPos, "expected3", COMMA, RPAREN, LBRACKET);
+ }
return params.toList();
}
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Thu Jan 30 17:46:25 2014 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Sun Feb 02 12:12:01 2014 +0100
@@ -1,5 +1,5 @@
#
-# Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 1999, 2014, 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
@@ -597,6 +597,9 @@
compiler.err.varargs.and.receiver =\
varargs notation not allowed on receiver parameter
+compiler.err.varargs.must.be.last =\
+ varargs parameter must be the last parameter
+
compiler.err.array.and.receiver =\
legacy array notation not allowed on receiver parameter
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/VarargsMustBeLast.java Sun Feb 02 12:12:01 2014 +0100
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.varargs.must.be.last
+
+class VarargMustBeFinal {
+ public void invalidVarArg(String... invalidVarArg, String extra) { }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/parser/ErroneousParameters.java Sun Feb 02 12:12:01 2014 +0100
@@ -0,0 +1,16 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8030091
+ * @summary Producing reasonable errors for unexpected tokens in method parameters
+ * @compile/fail/ref=ErroneousParameters.out -XDrawDiagnostics ErroneousParameters.java
+ */
+
+public class ErroneousParameters {
+
+ public static void test(int... extraVarArg, int additionalParam) { }
+ public static void test(byte param...) { }
+ public static void test(char param,) { }
+ public static void test(short param[) { }
+ public static void test(int param=) { }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/parser/ErroneousParameters.out Sun Feb 02 12:12:01 2014 +0100
@@ -0,0 +1,14 @@
+ErroneousParameters.java:10:36: compiler.err.varargs.must.be.last
+ErroneousParameters.java:11:39: compiler.err.expected3: ',', ')', '['
+ErroneousParameters.java:11:42: compiler.err.illegal.start.of.type
+ErroneousParameters.java:11:43: compiler.err.expected: token.identifier
+ErroneousParameters.java:11:45: compiler.err.expected: ';'
+ErroneousParameters.java:12:40: compiler.err.illegal.start.of.type
+ErroneousParameters.java:12:41: compiler.err.expected3: ',', ')', '['
+ErroneousParameters.java:12:43: compiler.err.expected: ';'
+ErroneousParameters.java:13:41: compiler.err.expected: ']'
+ErroneousParameters.java:14:38: compiler.err.expected3: ',', ')', '['
+ErroneousParameters.java:14:39: compiler.err.illegal.start.of.type
+ErroneousParameters.java:14:40: compiler.err.expected: token.identifier
+ErroneousParameters.java:14:42: compiler.err.expected: ';'
+13 errors
--- a/langtools/test/tools/javac/processing/errors/TestParseErrors/TestParseErrors.out Thu Jan 30 17:46:25 2014 -0800
+++ b/langtools/test/tools/javac/processing/errors/TestParseErrors/TestParseErrors.out Sun Feb 02 12:12:01 2014 +0100
@@ -1,6 +1,6 @@
ParseErrors.java:37:37: compiler.err.expected: token.identifier
ParseErrors.java:38:1: compiler.err.illegal.start.of.type
-ParseErrors.java:38:2: compiler.err.expected: ')'
+ParseErrors.java:38:2: compiler.err.expected3: ',', ')', '['
ParseErrors.java:40:6: compiler.err.expected: ';'
ParseErrors.java:40:20: compiler.err.illegal.start.of.type
ParseErrors.java:41:5: compiler.err.expected: '('