|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8292755 |
| 27 | + * @summary Verify error recovery related to method modifiers. |
| 28 | + * @library /tools/lib |
| 29 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 30 | + * jdk.compiler/com.sun.tools.javac.main |
| 31 | + * jdk.jdeps/com.sun.tools.classfile |
| 32 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 33 | + * @run main MethodModifiers |
| 34 | + */ |
| 35 | + |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Objects; |
| 39 | + |
| 40 | +import toolbox.JavacTask; |
| 41 | +import toolbox.Task.Expect; |
| 42 | +import toolbox.Task.OutputKind; |
| 43 | +import toolbox.TestRunner; |
| 44 | +import toolbox.ToolBox; |
| 45 | + |
| 46 | +public class MethodModifiers extends TestRunner { |
| 47 | + |
| 48 | + ToolBox tb; |
| 49 | + |
| 50 | + public MethodModifiers() { |
| 51 | + super(System.err); |
| 52 | + tb = new ToolBox(); |
| 53 | + } |
| 54 | + |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + MethodModifiers t = new MethodModifiers(); |
| 57 | + t.runTests(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testNonDefaultMethodInterface() throws Exception { |
| 62 | + String code = """ |
| 63 | + interface Test { |
| 64 | + void test() { |
| 65 | + try { |
| 66 | + unresolvable(); |
| 67 | + } catch (Throwable t) { |
| 68 | + throw new RuntimeException(t); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + """; |
| 73 | + Path curPath = Path.of("."); |
| 74 | + List<String> actual = new JavacTask(tb) |
| 75 | + .options("-XDrawDiagnostics", |
| 76 | + "-XDshould-stop.at=FLOW", |
| 77 | + "-XDdev") |
| 78 | + .sources(code) |
| 79 | + .outdir(curPath) |
| 80 | + .run(Expect.FAIL) |
| 81 | + .getOutputLines(OutputKind.DIRECT); |
| 82 | + |
| 83 | + List<String> expected = List.of( |
| 84 | + "Test.java:2:17: compiler.err.intf.meth.cant.have.body", |
| 85 | + "Test.java:4:13: compiler.err.cant.resolve.location.args: kindname.method, unresolvable, , , (compiler.misc.location: kindname.interface, Test, null)", |
| 86 | + "2 errors" |
| 87 | + ); |
| 88 | + |
| 89 | + if (!Objects.equals(actual, expected)) { |
| 90 | + error("Expected: " + expected + ", but got: " + actual); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testAbstractMethodWithBody() throws Exception { |
| 96 | + String code = """ |
| 97 | + abstract class Test { |
| 98 | + abstract void test() { |
| 99 | + try { |
| 100 | + unresolvable(); |
| 101 | + } catch (Throwable t) { |
| 102 | + throw new RuntimeException(t); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + """; |
| 107 | + Path curPath = Path.of("."); |
| 108 | + List<String> actual = new JavacTask(tb) |
| 109 | + .options("-XDrawDiagnostics", |
| 110 | + "-XDshould-stop.at=FLOW", |
| 111 | + "-XDdev") |
| 112 | + .sources(code) |
| 113 | + .outdir(curPath) |
| 114 | + .run(Expect.FAIL) |
| 115 | + .getOutputLines(OutputKind.DIRECT); |
| 116 | + |
| 117 | + List<String> expected = List.of( |
| 118 | + "Test.java:2:19: compiler.err.abstract.meth.cant.have.body", |
| 119 | + "Test.java:4:13: compiler.err.cant.resolve.location.args: kindname.method, unresolvable, , , (compiler.misc.location: kindname.class, Test, null)", |
| 120 | + "2 errors" |
| 121 | + ); |
| 122 | + |
| 123 | + if (!Objects.equals(actual, expected)) { |
| 124 | + error("Expected: " + expected + ", but got: " + actual); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testNativeMethodWithBody() throws Exception { |
| 130 | + String code = """ |
| 131 | + class Test { |
| 132 | + native void test() { |
| 133 | + try { |
| 134 | + unresolvable(); |
| 135 | + } catch (Throwable t) { |
| 136 | + throw new RuntimeException(t); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + """; |
| 141 | + Path curPath = Path.of("."); |
| 142 | + List<String> actual = new JavacTask(tb) |
| 143 | + .options("-XDrawDiagnostics", |
| 144 | + "-XDshould-stop.at=FLOW", |
| 145 | + "-XDdev") |
| 146 | + .sources(code) |
| 147 | + .outdir(curPath) |
| 148 | + .run(Expect.FAIL) |
| 149 | + .getOutputLines(OutputKind.DIRECT); |
| 150 | + |
| 151 | + List<String> expected = List.of( |
| 152 | + "Test.java:2:17: compiler.err.native.meth.cant.have.body", |
| 153 | + "Test.java:4:13: compiler.err.cant.resolve.location.args: kindname.method, unresolvable, , , (compiler.misc.location: kindname.class, Test, null)", |
| 154 | + "2 errors" |
| 155 | + ); |
| 156 | + |
| 157 | + if (!Objects.equals(actual, expected)) { |
| 158 | + error("Expected: " + expected + ", but got: " + actual); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments