33362
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
* @summary Tests for modifiers
|
|
27 |
* @build KullaTesting TestingInputStream ExpectedDiagnostic
|
|
28 |
* @run testng ModifiersTest
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.List;
|
|
33 |
|
|
34 |
import javax.tools.Diagnostic;
|
|
35 |
|
|
36 |
import org.testng.annotations.DataProvider;
|
|
37 |
import org.testng.annotations.Test;
|
|
38 |
|
|
39 |
@Test
|
|
40 |
public class ModifiersTest extends KullaTesting {
|
|
41 |
|
|
42 |
@DataProvider(name = "ignoredModifiers")
|
|
43 |
public Object[][] getTestCases() {
|
|
44 |
List<Object[]> testCases = new ArrayList<>();
|
|
45 |
String[] ignoredModifiers = new String[] {
|
|
46 |
"public", "protected", "private", "static", "final"
|
|
47 |
};
|
|
48 |
for (String ignoredModifier : ignoredModifiers) {
|
|
49 |
for (ClassType classType : ClassType.values()) {
|
|
50 |
testCases.add(new Object[] { ignoredModifier, classType });
|
|
51 |
}
|
|
52 |
}
|
|
53 |
return testCases.toArray(new Object[testCases.size()][]);
|
|
54 |
}
|
|
55 |
|
|
56 |
@Test(dataProvider = "ignoredModifiers")
|
|
57 |
public void ignoredModifiers(String modifier, ClassType classType) {
|
|
58 |
assertDeclareWarn1(
|
|
59 |
String.format("%s %s A {}", modifier, classType), "jdk.eval.warn.illegal.modifiers");
|
|
60 |
assertNumberOfActiveClasses(1);
|
|
61 |
assertClasses(clazz(classType, "A"));
|
|
62 |
assertActiveKeys();
|
|
63 |
}
|
|
64 |
|
|
65 |
public void accessToStaticFieldsOfClass() {
|
|
66 |
assertEval("class A {" +
|
|
67 |
"int x = 14;" +
|
|
68 |
"static int y = 18;" +
|
|
69 |
" }");
|
|
70 |
assertDeclareFail("A.x;",
|
|
71 |
new ExpectedDiagnostic("compiler.err.non-static.cant.be.ref", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
|
|
72 |
assertEval("A.y;", "18");
|
|
73 |
assertEval("new A().x;", "14");
|
|
74 |
assertEval("A.y = 88;", "88");
|
|
75 |
assertActiveKeys();
|
|
76 |
}
|
|
77 |
|
|
78 |
public void accessToStaticMethodsOfClass() {
|
|
79 |
assertEval("class A {" +
|
|
80 |
"void x() {}" +
|
|
81 |
"static void y() {}" +
|
|
82 |
" }");
|
|
83 |
assertDeclareFail("A.x();",
|
|
84 |
new ExpectedDiagnostic("compiler.err.non-static.cant.be.ref", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
|
|
85 |
assertEval("A.y();");
|
|
86 |
assertActiveKeys();
|
|
87 |
}
|
|
88 |
|
|
89 |
public void accessToStaticFieldsOfInterface() {
|
|
90 |
assertEval("interface A {" +
|
|
91 |
"int x = 14;" +
|
|
92 |
"static int y = 18;" +
|
|
93 |
" }");
|
|
94 |
assertEval("A.x;", "14");
|
|
95 |
assertEval("A.y;", "18");
|
|
96 |
|
|
97 |
assertDeclareFail("A.x = 18;",
|
|
98 |
new ExpectedDiagnostic("compiler.err.cant.assign.val.to.final.var", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
|
|
99 |
assertDeclareFail("A.y = 88;",
|
|
100 |
new ExpectedDiagnostic("compiler.err.cant.assign.val.to.final.var", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
|
|
101 |
assertActiveKeys();
|
|
102 |
}
|
|
103 |
|
|
104 |
public void accessToStaticMethodsOfInterface() {
|
|
105 |
assertEval("interface A { static void x() {} }");
|
|
106 |
assertEval("A.x();");
|
|
107 |
assertActiveKeys();
|
|
108 |
}
|
|
109 |
|
|
110 |
public void finalMethod() {
|
|
111 |
assertEval("class A { final void f() {} }");
|
|
112 |
assertDeclareFail("class B extends A { void f() {} }",
|
|
113 |
new ExpectedDiagnostic("compiler.err.override.meth", 20, 31, 25, -1, -1, Diagnostic.Kind.ERROR));
|
|
114 |
assertActiveKeys();
|
|
115 |
}
|
|
116 |
|
|
117 |
//TODO: is this the right semantics?
|
|
118 |
public void finalConstructor() {
|
|
119 |
assertDeclareFail("class A { final A() {} }",
|
|
120 |
new ExpectedDiagnostic("compiler.err.mod.not.allowed.here", 10, 22, 16, -1, -1, Diagnostic.Kind.ERROR));
|
|
121 |
assertActiveKeys();
|
|
122 |
}
|
|
123 |
|
|
124 |
//TODO: is this the right semantics?
|
|
125 |
public void finalDefaultMethod() {
|
|
126 |
assertDeclareFail("interface A { final default void a() {} }",
|
|
127 |
new ExpectedDiagnostic("compiler.err.mod.not.allowed.here", 14, 39, 33, -1, -1, Diagnostic.Kind.ERROR));
|
|
128 |
assertActiveKeys();
|
|
129 |
}
|
|
130 |
}
|