MethodsTest.java revision 3731:6bb6785c2329
1/*
2 * Copyright (c) 2015, 2016, 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 8080357 8167643
27 * @summary Tests for EvaluationState.methods
28 * @build KullaTesting TestingInputStream ExpectedDiagnostic
29 * @run testng MethodsTest
30 */
31
32import javax.tools.Diagnostic;
33
34import jdk.jshell.Snippet;
35import jdk.jshell.MethodSnippet;
36import jdk.jshell.Snippet.Status;
37import org.testng.annotations.Test;
38
39import static jdk.jshell.Snippet.Status.*;
40
41@Test
42public class MethodsTest extends KullaTesting {
43
44    public void noMethods() {
45        assertNumberOfActiveMethods(0);
46    }
47
48    public void testSignature1() {
49        MethodSnippet m1 = methodKey(assertEval("void f() { g(); }", added(RECOVERABLE_DEFINED)));
50        assertMethodDeclSnippet(m1, "f", "()void", RECOVERABLE_DEFINED, 1, 0);
51        MethodSnippet m2 = methodKey(assertEval("void g() { }",
52                added(VALID),
53                ste(m1, RECOVERABLE_DEFINED, VALID, false, null)));
54        assertMethodDeclSnippet(m2, "g", "()void", VALID, 0, 0);
55    }
56
57    public void testSignature2() {
58        MethodSnippet m1 = (MethodSnippet) assertDeclareFail("void f() { return g(); }", "compiler.err.prob.found.req");
59        assertMethodDeclSnippet(m1, "f", "()void", REJECTED, 0, 2);
60        MethodSnippet m2 = methodKey(assertEval("int f() { return g(); }",
61                added(RECOVERABLE_DEFINED)));
62        assertMethodDeclSnippet(m1, "f", "()void", REJECTED, 0, 2);
63        assertMethodDeclSnippet(m2, "f", "()int", RECOVERABLE_DEFINED, 1, 0);
64    }
65
66    @Test(enabled = false) // TODO 8081690
67    public void testSignature3() {
68        MethodSnippet m1 = methodKey(assertEval("void f(Bar b) { }", added(RECOVERABLE_NOT_DEFINED)));
69        assertMethodDeclSnippet(m1, "f", "(Bar)void", RECOVERABLE_NOT_DEFINED, 1, 0);
70        MethodSnippet m2 = methodKey(assertEval("void f(A.Bar b) { }", added(RECOVERABLE_NOT_DEFINED)));
71        assertMethodDeclSnippet(m1, "f", "(Bar)void", RECOVERABLE_NOT_DEFINED, 1, 0);
72        assertMethodDeclSnippet(m2, "f", "(A.Bar)void", RECOVERABLE_NOT_DEFINED, 1, 0);
73        assertDrop(m1, ste(m1, RECOVERABLE_NOT_DEFINED, DROPPED, false, null));
74        assertMethodDeclSnippet(m1, "f", "(Bar)void", DROPPED, 1, 0);
75    }
76
77    // 8080357
78    public void testNonReplUnresolved() {
79        // internal case
80        assertEval("class CCC {}", added(VALID));
81        assertEval("void f1() { CCC.xxxx(); }", added(RECOVERABLE_DEFINED));
82        // external case, not recoverable
83        assertDeclareFail("void f2() { System.xxxx(); }", "compiler.err.cant.resolve.location.args");
84    }
85
86    public void methods() {
87        assertEval("int x() { return 10; }");
88        assertEval("String y() { return null; }");
89        assertEval("long z() { return 0; }");
90        assertMethods(method("()int", "x"), method("()String", "y"), method("()long", "z"));
91        assertActiveKeys();
92    }
93
94    public void methodOverload() {
95        assertEval("int m() { return 1; }");
96        assertEval("int m(int x) { return 2; }");
97        assertEval("int m(String s) { return 3; }");
98        assertEval("int m(int x, int y) { return 4; }");
99        assertEval("int m(int x, String z) { return 5; }");
100        assertEval("int m(int x, String z, long g) { return 6; }");
101        assertMethods(
102                method("()int", "m"),
103                method("(int)int", "m"),
104                method("(String)int", "m"),
105                method("(int,int)int", "m"),
106                method("(int,String)int", "m"),
107                method("(int,String,long)int", "m")
108        );
109        assertEval("m();", "1");
110        assertEval("m(3);", "2");
111        assertEval("m(\"hi\");", "3");
112        assertEval("m(7, 8);", "4");
113        assertEval("m(7, \"eight\");", "5");
114        assertEval("m(7, \"eight\", 9L);", "6");
115        assertActiveKeys();
116    }
117
118    /***
119    public void methodOverloadDependent() {
120        assertEval("String m(String s) { return s + s; }");
121        assertEval("String m(double d) { return m(\"#\" + d); }");
122        assertEval("String m(int x) { return m(2.25 * x); }");
123        assertEval("String m() { return m(3); }");
124        assertMethods(
125                method("(String)String", "m"),
126                method("(double)String", "m"),
127                method("(int)String", "m"),
128                method("()String", "m")
129        );
130        assertEval("m();", "\"#6.75#6.75\"");
131        assertEval("m(2);", "\"#4.5#4.5\"");
132        assertEval("m(3.14);", "\"#3.14#3.14\"");
133        assertEval("m(\"hi\");", "\"hihi\"");
134        assertActiveKeys();
135    }
136    ***/
137
138    public void methodsRedeclaration1() {
139        Snippet x = methodKey(assertEval("int x() { return 10; }"));
140        Snippet y = methodKey(assertEval("String y() { return \"\"; }"));
141        assertMethods(method("()int", "x"), method("()String", "y"));
142        assertActiveKeys();
143
144        assertEval("long x() { return 0; }",
145                ste(MAIN_SNIPPET, VALID, VALID, true, null),
146                ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
147        assertMethods(method("()long", "x"), method("()String", "y"));
148        assertActiveKeys();
149
150        assertEval("String y() { return null; }",
151                ste(MAIN_SNIPPET, VALID, VALID, false, null),
152                ste(y, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
153        assertMethods(method("()long", "x"), method("()String", "y"));
154        assertActiveKeys();
155    }
156
157    public void methodsRedeclaration2() {
158        assertEval("int a() { return 1; }");
159        assertMethods(method("()int", "a"));
160        assertActiveKeys();
161
162        Snippet b = methodKey(assertEval("Integer b() { return a(); }"));
163        assertMethods(method("()int", "a"), method("()Integer", "b"));
164        assertActiveKeys();
165
166        Snippet c = methodKey(assertEval("double c() { return b(); }"));
167        assertMethods(method("()int", "a"), method("()Integer", "b"), method("()double", "c"));
168        assertActiveKeys();
169
170        assertEval("double b() { return 3.14159; }",
171                ste(MAIN_SNIPPET, VALID, VALID, true, null),
172                ste(b, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
173        assertMethods(method("()int", "a"), method("()double", "b"), method("()double", "c"));
174        assertEval("c();", "3.14159");
175        assertActiveKeys();
176    }
177
178    public void methodsRedeclaration3() {
179        Snippet x = methodKey(assertEval("int x(Object...a) { return 10; }"));
180        assertMethods(method("(Object...)int", "x"));
181        assertActiveKeys();
182
183        assertEval("int x(Object[]a) { return 10; }",
184                ste(MAIN_SNIPPET, VALID, VALID, true, null),
185                ste(x, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
186        assertMethods(method("(Object[])int", "x"));
187        assertActiveKeys();
188    }
189
190
191    public void methodsRedeclaration4() {
192        Snippet a = methodKey(assertEval("int foo(int a) { return a; }"));
193        assertEval("int x = foo(10);");
194        assertActiveKeys();
195        assertMethods(method("(int)int", "foo"));
196        assertEval("int foo(int a) { return a * a; }",
197                ste(MAIN_SNIPPET, VALID, VALID, false, null),
198                ste(a, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
199        assertActiveKeys();
200    }
201
202    public void methodsErrors() {
203        assertDeclareFail("String f();",
204                new ExpectedDiagnostic("compiler.err.missing.meth.body.or.decl.abstract", 0, 11, 7, -1, -1, Diagnostic.Kind.ERROR));
205        assertNumberOfActiveMethods(0);
206        assertActiveKeys();
207
208        assertDeclareFail("abstract String f();",
209                new ExpectedDiagnostic("jdk.eval.error.illegal.modifiers", 0, 8, 0, -1, -1, Diagnostic.Kind.ERROR));
210        assertNumberOfActiveMethods(0);
211        assertActiveKeys();
212
213        assertDeclareFail("native String f();",
214                new ExpectedDiagnostic("jdk.eval.error.illegal.modifiers", 0, 6, 0, -1, -1, Diagnostic.Kind.ERROR));
215        assertNumberOfActiveMethods(0);
216        assertActiveKeys();
217
218        assertDeclareFail("synchronized String f() {return null;}",
219                new ExpectedDiagnostic("jdk.eval.error.illegal.modifiers", 0, 12, 0, -1, -1, Diagnostic.Kind.ERROR));
220        assertNumberOfActiveMethods(0);
221        assertActiveKeys();
222
223        assertDeclareFail("int f() {}", "compiler.err.missing.ret.stmt",
224                added(REJECTED));
225        assertNumberOfActiveMethods(0);
226        assertActiveKeys();
227
228        assertEval("String x() { return \"\"; };");
229        assertMethods(method("()String", "x"));
230        assertActiveKeys();
231    }
232
233
234    public void methodsAccessModifierIgnored() {
235        Snippet f = methodKey(assertEval("public String f() {return null;}",
236                added(VALID)));
237        assertNumberOfActiveMethods(1);
238        assertActiveKeys();
239
240        f = methodKey(assertEval("protected String f() {return null;}",
241                ste(MAIN_SNIPPET, VALID, VALID, false, null),
242                ste(f, VALID, OVERWRITTEN, false, MAIN_SNIPPET)));
243        assertNumberOfActiveMethods(1);
244        assertActiveKeys();
245
246        assertEval("private String f() {return null;}",
247                ste(MAIN_SNIPPET, VALID, VALID, false, null),
248                ste(f, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
249        assertNumberOfActiveMethods(1);
250        assertActiveKeys();
251    }
252
253    public void methodsWarn() {
254        Snippet f = assertDeclareWarn1("static String f() {return null;}",
255                new ExpectedDiagnostic("jdk.eval.warn.illegal.modifiers", 0, 6, 0, -1, -1, Diagnostic.Kind.WARNING),
256                added(VALID));
257        assertNumberOfActiveMethods(1);
258        assertActiveKeys();
259
260        assertDeclareWarn1("final String f() {return null;}",
261                new ExpectedDiagnostic("jdk.eval.warn.illegal.modifiers", 0, 5, 0, -1, -1, Diagnostic.Kind.WARNING),
262                ste(MAIN_SNIPPET, VALID, VALID, false, null),
263                ste(f, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
264        assertNumberOfActiveMethods(1);
265        assertActiveKeys();
266    }
267
268    public void methodSignatureUnresolved() {
269        MethodSnippet key = (MethodSnippet) methodKey(assertEval("und m() { return new und(); }", added(RECOVERABLE_NOT_DEFINED)));
270        assertMethodDeclSnippet(key, "m", "()und", RECOVERABLE_NOT_DEFINED, 1, 0);
271        assertUnresolvedDependencies1(key, Status.RECOVERABLE_NOT_DEFINED, "class und");
272        assertEval("class und {}",
273                added(VALID),
274                ste(key, RECOVERABLE_NOT_DEFINED, VALID, true, null));
275        assertMethodDeclSnippet(key, "m", "()und", Status.VALID, 0, 0);
276        assertNumberOfActiveMethods(1);
277        assertActiveKeys();
278    }
279
280    @Test(enabled = false) // TODO 8081689
281    public void classMethodsAreNotVisible() {
282        assertEval(
283            "class A {" +
284                "int foo() {" +
285                    "int x = 10;" +
286                    "int y = 2 * x;" +
287                    "return x * y;" +
288                "}" +
289            "}");
290        assertNumberOfActiveMethods(0);
291        assertEval("int x = 10;", "10");
292        assertEval("int foo() {" +
293                        "int y = 2 * x;" +
294                        "return x * y;" +
295                        "}");
296        assertMethods(method("()int", "foo"));
297        assertEval("foo();", "200");
298        assertActiveKeys();
299    }
300
301    public void lambdas() {
302        assertEval("class Inner1 implements Runnable {" +
303                "public Runnable lambda1 = () -> {};" +
304                "public void function() {}" +
305                "public void run() {}" +
306                "}");
307
308        assertEval("class Inner2 {" +
309                "private Runnable lambda1 = () -> {};" +
310                "private static void staticFunction() {}" +
311                "}");
312
313        // the following method references and lambda functions
314        // generate synthetic methods
315        assertEval("Runnable run = () -> {};");
316        assertEval("Inner1 inner = new Inner1();");
317        assertEval("Runnable l1 = inner::function;");
318        assertEval("Runnable l2 = Inner1::new;");
319        assertEval("inner.lambda1 = inner::function;");
320        assertEval("java.util.stream.IntStream.of(2).mapToObj(int[]::new);");
321        assertNumberOfActiveMethods(0);
322        assertActiveKeys();
323    }
324}
325